-
-
Notifications
You must be signed in to change notification settings - Fork 887
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
defaults don't work inside anyOf
#276
Comments
That is the way they are implemented. See Assigning defaults:
|
Is that just underspecified in the spec? It seems a natural assumption that defaults would apply wherever they are defined |
Taking into account defaults in subschemas of compound keywords inside properties is both difficult to implement and in some cases ambiguous. The existing implementation covers 80%+ of real use cases. If you need multiple defaults depending on the valid schema branch, you just have to use v5 What is the objection to refactoring the schema in the way that Ajv supports (i.e. putting default keyword on the same level as allOf)? |
Absolutely understandable. Could you expand on the proposed solution at the end there? Maybe I'm just not seeing how to do this in a supported fashion. |
In your case, this schema would produce the result you want (defaults are assigned before schema is validated): {
"type": "object",
"properties": {
"num": {
"default": 2,
"allOf": [ { "type": "number" }, { "acceptTerms": true } ]
}
},
"required": [ "num" ]
} Another common use case that would produce confusing results if it were supported is this: {
"type": "object",
"anyOf": [
{
"properties": {
"type": { "enum": [ "foo" ] },
"foo": { "type": "integer", "default": 1 }
},
"additionalProperties": false
},
{
"properties": {
"type": { "enum": [ "bar" ] },
"bar": { "type": "string", "default": "abc" }
},
"additionalProperties": false
}
]
} The above wouldn't work without implementing complex rollback logic and that's the reason why defaults inside "anyOf"/"oneOf" (at any depth) are ignored by Ajv. The same logic can be implemented with this schema: {
"type": "object",
"switch": [
{
"if": { "properties": { "type": { "constant": "foo" } } },
"then": {
"properties": {
"type": {},
"foo": { "type": "integer", "default": 1 }
},
"additionalProperties": false
}
},
{
"if": { "properties": { "type": { "constant": "bar" } } },
"then": {
"properties": {
"type": {},
"bar": { "type": "string", "default": "abc" }
},
"additionalProperties": false
}
},
{ "then": false }
]
} The above would assign defaults in the expected way because Ajv knows which branch should be validated based on "if" conditions. Defaults inside "if" schemas are still ignored. |
anyOf
anyOf
@epoberezkin you should document this (declare default next to anyOf/allOf/oneOf instead of inside), it's a life saver. |
@kapouer it is in docs here: https://github.com/epoberezkin/ajv#assigning-defaults |
I meant the way you use v5 |
@kapouer got it. Will do. |
What version of Ajv are you using? Does the issue happen if you use the latest version?
4.4.0, yes
Ajv options object (see https://github.com/epoberezkin/ajv#options):
JSON Schema (please make it as small as possible to reproduce the issue):
not working:
working:
Data (please make it as small as posssible to reproduce the issue):
Your code (please use
options
,schema
anddata
as variables):Validation result, data AFTER validation, error messages:
What results did you expect?
no errors, the default of
2
would be usedDo you intend to resolve the issue?
The text was updated successfully, but these errors were encountered: