Duniel Mesa
Duniel Mesa

Reputation: 29

Generate Valid Random Faker Values in Go

I am using Validator package. I have the next Struct:

type User struct {
    Name       string `validate:"required"`
    Email      string `validate:"required,email"`
    CreditCard string `validate:"credit_card"`
    Password   string `validate:"min=8,max=255"`
}

What can I do to generate valid random values for thats fields?

Upvotes: 0

Views: 1398

Answers (2)

Leonardo Lima
Leonardo Lima

Reputation: 413

If you're writing tests, you can use Fuzzing (implemented in Go 1.18).

Would look like this:

import "testing"

func TestHello(f *testing.F) {
    // Add your seeds here
    f.add("John", "[email protected]", "1234-5678-1234-5678", "@HelloWorld123")
    
    f.Fuzz(func(t *testing.T, name, email, creditCard, pass string)  {
        // Write your test here
    }))
}

Then run:

$ go test -fuzz

Upvotes: 1

Gleb Beskhlebnyy
Gleb Beskhlebnyy

Reputation: 156

You can use faker package. It has functions like FirstName(), Email(), CCNumber(). You can also use tags (one, two) with this package in your struct.

Upvotes: 3

Related Questions