New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pre-populate default values from schema (command-line option) #200
Comments
I'm perfectly happy to add this as a feature. As the doc you cite shows, it's pretty easy. I think there are hidden dangers here, but as long as it's gated behind a flag, I don't have a huge problem exposing people to those dangers. Detail below. Bad case 1: default works, but meaning is unclearIf you have a schema like so: {
"type": "object",
"properties": {
"foo": {
"default": "bar",
"type": "string"
}
},
"required": ["foo"]
} then a file containing The duality could lead to confusion. Bad case 2: invalid defaultsConsider this schema: {
"type": "object",
"properties": {
"foo": {
"default": 42,
"type": "string"
}
}
} Applied to Because the schema specifies something odd, a valid document can't pass validation with this behavior. As I said, I'm happy to add this, as it's "use at your own risk". But I felt it was only responsible to point out some of the odd cases (which you might think of as corner cases). |
Yeah, that's an interesting discussion. I agree it could be some use cases where is unclear the expected behaviour. In my opinion (which is just my opinion right now) I consider that de case 2 should be considered as an application bug. The developer has set as default value something that can't be a valid value. However, I understand that is context dependant. In case 1, I can argue that: A purpose of declaring default values is to free the developer of setting every required value. Making it simple. So having a {} document could be perfectly valid for the application because it will know the default values. Despite the unclear expected behaviour, I really think it could be a good feature. Thanks for taking it into account |
Just a suggestion that may help to clarify case 2 scenario. If the property is not required, then the default value will not be populated. |
I've just finished adding this and it will be included in the next release.
I understand that you're trying to come up with ways around these issues, and maybe in some cases it's possible. However, I primarily aimed to share that I don't think this is a solvable problem in the general case. For a few more examples, if an As long as the docs clarify that there are known-undefined behaviors in such special cases, I'm okay with |
Hi,
I think it would be very useful for validating json documents against schemas a command-line option that would allow to pre-populate defaults into the document like python-jsonshema docs suggest here:
https://python-jsonschema.readthedocs.io/en/stable/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
It would make easy to validate documents like it is suggested just from the command-line tool
The text was updated successfully, but these errors were encountered: