Home > front end >  Generate Valid Random Faker Values in Go
Generate Valid Random Faker Values in Go

Time:08-05

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?

CodePudding user response:

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.

CodePudding user response:

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
  • Related