jsf icon indicating copy to clipboard operation
jsf copied to clipboard

Use default values

Open pinin4fjords opened this issue 2 years ago • 4 comments

Thanks for the tool!

Is there way to have it use default values from the schema where present, rather than faking values?

pinin4fjords avatar Jul 06 '22 11:07 pinin4fjords

The only way to do this at the moment is to add

"$provider": "lambda: 'your default here'"

How would you ideally have it? Default would only be filled in if it was null so default gets set?

ghandic avatar Jul 06 '22 12:07 ghandic

Great- thanks, I'll do that.

I'd like to be able to have default override the faking in all cases (even if the default is set to an empty value). Probably an atypical case though, maybe it could be an optional behaviour?

pinin4fjords avatar Jul 06 '22 12:07 pinin4fjords

Happy to hear some proposals for the API to achieve it and what the behavior should achieve. If you could give some pseudo code examples and example schema + expected output?

ghandic avatar Jul 06 '22 12:07 ghandic

Okay, so behaviour right now is like:

>>> from jsf import JSF
>>> test_schema
{'type': 'object', 'properties': {'test_field': {'type': 'string', 'default': 'default value'}}, 'required': ['test_field']}
>>> JSF(test_schema).generate()
{'test_field': 'quas placeat ipsum, libero culpa! esse officiis'}

So maybe what would be good would be something like:

>>> JSF(test_schema).generate(use_defaults = True)
{'test_field': 'default value'}

pinin4fjords avatar Jul 06 '22 12:07 pinin4fjords

This is hard as the schema doesnt constrain it to only using the "default" otherwise it should be "const". This is because the "default" is a non restrictive keyword, unlike "type" or "enum"

Agree with the concept, but I'm not sure it will work for the overall package and peoples expectations.

Going to close this issue, but happy to consider other prooosals

ghandic avatar Jan 08 '23 06:01 ghandic