Closed emab closed 1 year ago
Having discussed this with people - something that came up that may be more useful is a mapping of TypeName
-> FieldName
-> Custom generator
to provide even more granular control over generation. This assumes all fields with the same name will have the same generation, which is fine for some cases but not all.
Having discussed this with people - something that came up that may be more useful is a mapping of
TypeName
->FieldName
->Custom generator
to provide even more granular control over generation. This assumes all fields with the same name will have the same generation, which is fine for some cases but not all.
Have implemented this in the PR
Very good idea ! I was looking for a solution to mock fields individually and I was supprised not to be able to do it easilly. Will try this PR
Very good idea ! I was looking for a solution to mock fields individually and I was supprised not to be able to do it easilly.
Will try this PR
Let me know how it goes and if you encounter any issues! Happy to modify it further if needed.
Very good idea ! I was looking for a solution to mock fields individually and I was supprised not to be able to do it easilly. Will try this PR
Let me know how it goes and if you encounter any issues! Happy to modify it further if needed.
Well it worked well. This feature is very usefull to me. Thanks.
@emab I did some changes on your PR:
extra.args
to extra.arguments
for consistencyThanks again for your contribution!
This PR adds something that would come in super handy for a project I'm working on - in summary:
Type
faker
packageAlong with adding the above functionality, I've added some tests to verify the behaviour. I'm not entirely sure how much this will affect performance - I've run it against a schema of around 2000 lines on a laptop and the time went from ~19 seconds to ~22 seconds for a full generation (starting from zero files, generating type file then generating mock data file).
I'll go through each step and explain a bit more about why I think it's useful:
Providing generator for a specific field
Currently you can provide generators to scalars only. This is fine - however I'm not sure how scalable it is to provide a scalar for values that you want to generate a more specific value for. Take for example a field named
email
. It's safe to assume you'll always want an email here - so instead of it being aString
which gets a single word generated, you can specify that an email is generated instead.This PR allows you to specify a generator for a field with a given name using the following:
This would generate an email for any field called
email
.If you want to be more specific, you can provide the
typeName
of a given field. For example you might not want allstartDate
fields to give a date in the future. If you have a typeUser
and wanted to give a specific generator to thestartDate
field you can do the following:It gives finer control over generated values, whilst still allowing you to define scalar generation if you don't want to provide individual generation options.
Passing arguments to the generator
We have the ability to do this in the scalar map. The same type has been used again so that the user can provide any number of arguments to the function.
Call another function (with arguments) on result
For me this was necessary when I needed to generate a date in a specific format. Casual does a good job of this by allowing you to pass
format
- however the faker library has more options but spits out aDate
object. Being able to calltoISOString()
is really useful in this situation.Provide locale
This allows you to change the output of
faker
so thatzipCode
can output a UK postcode - something that's useful for projects based in the UK.Example configuration