-
-
Notifications
You must be signed in to change notification settings - Fork 878
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
Option to merge defaults #42
Comments
I agree that it's a useful feature. The reason it is absent is the number of complex and/or contradictory scenarios that should be covered. I am sure that there will be other complications that we don't immediately see. The additional problem with I will probably implement it at some point, but it's not a simple task. PR is welcome of course :) |
One could probably forbid Or you define that you choose the first |
The first valid |
Roll back seems hard.. Perhaps it's simplest (and possibly cheapest) to create a new object and |
@jonasfj you might be interested in taking a look at the defaults implementation in Themis. One of the main reasons I wrote it was because of the lack of support for defaults in most existing validators. We've been using it in production quite successfully for quite a while. @epoberezkin, It is not a simple solution to implement and it does have a significant performance impact but ultimately it provides a level of convenience which makes up for the shortcomings. |
@atrniv, thanks. I was actually thinking about implementation in themis. |
The implementation uses
|
@epoberezkin does For example:
If it doesn't then it would be very useful |
JSON schema is a JSON-document. It should be serialisable. You can do it with custom keyword on the parent object though, e.g.:
|
@epoberezkin thanks! |
actually, with "inline" keyword you can do a more sane thing on the property itself as there you will have access to the parent object and the current property name (check the implementation of default for that):
|
@epoberezkin would you mind pointing me to the implementation of I came up with this but it's probably wrong:
|
file defaults.def |
@paglias this doc about inline keywords can be useful |
@paglias I have the exact same need to generate a |
@ngryman sorry but it's been a long time and I don't remember if I found the solution. I'm not using ajv now |
Hello @epoberezkin, I just ran into this problem ...
I don't quite understand. I am dealing with a property that can either be false or an object. If it is an object, AJV should check its child properties and use a default in case that a child property is missing. This is currently not working. Since AJV is treating both cases well during validation, the code that selects the correct schema subbranch must already be there and properly working. Shouldn't it then also be possible to load the default value from this particular schema subbranch and use it if no actual value is present? From your comment, I conclude that defaults are currently implemented elsewhere, but then my naive assumption would be that the problem could be solved by moving the "default picking" code to run right after the branch selection? |
@rondonjon that is not how Ajv works, it has no global awareness about branches, it just validates data against all of them and defaults have to be applied before validation. There is no way Ajv can decide which is the right branch without validating against it. But then if it is not valid, defaults have to be removed. So there is no "branch selection". The suggestion to work around is exactly that - to explicitly select the branch using some kind of conditionals: "if/then/else" (will be in draft 7) or "select" (both are in ajv-keywords). It both solves defaults problem and excessive error reporting problem (in case of using oneOf/anyOf when all branches are invalid and Ajv has no idea what is the "right" branch). |
Thanks for the clarification, @epoberezkin ! |
Would it not make more sense to implement this in a similar fashion to "key": {
"type": "string",
"title": "Key",
"default": {"$uuid": "v4"}
} |
I recently have a problem with defaults value remaining in data even though the validation is failed, |
In JSON schema it's often useful for declare
defaults
, with the default value for an optional property. This is powerful for documentation, and many validators supports inserting the default into the validated object, this way you don't hardcode defaults into your code, but declare the defaults in your input schema.Example A)
This would be extremely useful, and doing this outside of the schema validation is hard, as you want to do this on all sub-objects as well, some of which may exist under an
anyOf
, so you can't insert the defaults until you're sure whichanyOf
branch is satisfied.It's quite possible that this is a post processing step, as nested
anyOf
branches means you sometimes can't do this until after everything is validated. A possible work around might be to insert the defaults into a clone of the existing object, that way multipleanyOf
branches shouldn't be a problem.Note, this only really relevant for objects, but could also be done for arrays which has
items: [{default: ...}, {default: ...}]
(though this is a corner case). It doesn't really make sense to do this when the top-level schema type is a value type likeinteger
as the input is either valid (ie. an integer) or invalid (ie. another type ornull
).Remark, after inserting a default value, validator should validate the default value too. This should ideally be done a schema compile-time. It is important to do this, because the default value of a property which has
type: 'object'
may be{}
and the schema for that object may specify properties for this that has additional default values. It would also be nice to get errors about inconsistencies at compile-time.The text was updated successfully, but these errors were encountered: