-
Notifications
You must be signed in to change notification settings - Fork 325
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
Support for default and new function applyDefaultsAndValidate #458
Comments
There are two main functions for this library: Validating and Walking. From a validation perspective, it is following the specification and the test cases were in default.json for each spec version. For walker listeners, I don't know how the default keyword is handled. I am guessing you are talking about the walker enhancement to apply the default values for your JSON input based on the specification. If this is the case, I would recommend exploring the walker code and add the future there. Let me know if I am off track. |
Yeah, that's correct. The use case is something like this. Someone changes the schema to add a new attribute with default value. Then if you validate an existing json object that does not have this new attribute, it ought to pass. Furthermore, it would be nice to get an updated json object that has the default values filled in. The other use case is that you already have json objects that passed the schema and saved to the database, and now you add a new attribute with default to the schema. Unless you re-validate the json object with the new schema, it will not get the defaults. So my approach of filling in default values will not help in that use case. There's talk about this at https://python-jsonschema.readthedocs.io/en/stable/faq/ "Why doesn’t my schema’s default property set the default on my instance?". Though I want to default validator to be the first one, so that the default value itself gets validated. So my plan is something like this:
It may be nice to do this in a way of hooks, so that customers can add this behavior. |
@SiemelNaran Your use cases make sense and I think your ideas should work. However, we shouldn't enable this applyDefault feature by default as it is against the specification and most users will want to see the error messages in the case data is not validate after the schema is changed. In the library, there is a configuration class that you can set a flag to enable this feature on demand. Please make sure the flag is false by default and only be enabled when you need this feature. Thanks. |
There are two ways to implements this -- one is to bake it into the validate function, and another is the tree walker approach. I tried the latter as you suggested it, writing a test that installs a keyword listener on "default" and run into the following problems:
The other way is to somehow have a DefaultValidator. Which is the better way in your opinion? |
@SiemelNaran I am not familiar with the walker feature and I hope @prashanthjos can help to validate your ideas and give you some advice. |
It seems easier to do without creating a listener on the "default". Instead I added a variable shouldApplyDefaults (but then turned it into a class so that you can apply property defaults or array defaults). See https://github.com/networknt/json-schema-validator/pull/468/files. |
This was resolved in pull-request #477 |
Looks like json-schema-validator does not support the "default" keyword. Is it possible to have a new function applyDefaultsAndValidate that applies the default and validates the schema?
As for tests we may need
There is one thing though. Suppose we are applying defaults and validating a big json and somewhere in the middle it fails fast. Then the json is partially changed. Maybe then we can just Javadoc this behavior and provide support for a listener.
I'm willing to work on this.
Thoughts?
The text was updated successfully, but these errors were encountered: