faker icon indicating copy to clipboard operation
faker copied to clipboard

Is there a way to make generated data optional?

Open lexuzieel opened this issue 2 years ago • 1 comments

Sometimes it is useful to generate randomly optional fields, such as the last name (but really - almost any field).

Is it possible to do something like this:

firstName := faker.FirstName()
lastName := faker.Optional(.5).LastName() // <-- Generate non-empty last name 50% of the time

fullName := firstName
if lastName != "" {
    fullName += " " + lastName
}

println("Hello, " + fullName + "!")

Here faker.LastName() would return empty string 50% of the time.

lexuzieel avatar Jul 12 '22 21:07 lexuzieel

Is it possible to use a custom generator faker for that?

ref https://github.com/bxcodec/faker/blob/72e522332c49399d4c52a9ad8086244df95f38a6/faker.go#L405-L456

bxcodec avatar Aug 11 '22 15:08 bxcodec