-
-
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
default not working in anyOf and oneOf but allOf #127
Comments
That is how it is implemented: see docs Assigning defaults, the end of the section. The reason for ignoring defaults in compound keywords is that the possible algorithm to decide which branch to take the default from is
Obviously in the case like in your example the default could have been applied, but you don't really need anyOf with a single branch. If anyOf/oneOf has multiple branches it becomes very messy. There is some discussion on the subject in #42. |
The solution I can suggest is to define a custom keyword say Given that from version 3.7.0 you have access to the parent data object and the property in this object that points to the current data you can easily check if the current data is defined and if it is not assign the default value. You'll just have to modify the schema to make the property not-required (it is kind of redundant in cases you have default) - this var schema = {
"anyOf": [{
"type" : "object",
"properties": {
"key1": {"type": "string", "applyDefault": "A"}
}
}]
};
ajv.addKeyword('applyDefault', { compile: function(schema) {
if (!this._opts.useDefaults) return function() { return true; }
var default = schema.applyDefault;
return function(data, dataPath, parentData, parentDataProperty) {
if (data === undefined) parentData[parentDataProperty] = default;
return true;
};
}}) |
above won't work :) but something along these lines will :) e.g. this: {
"anyOf": [{
"type" : "object",
"properties": {
"key1": {"type": "string"}
},
"defaultProperties": {
"key1": "A"
}
}]
} |
Schema:
input-data:
should result in:
But it produces errors if default is used. This happens in case of "anyOf" and "oneOf". "allOf" works.
Tested with ajv version 3.4.0 on Node.js v4.2.6 on 64bit Linux.
Option
useDefault: true
is usedThe text was updated successfully, but these errors were encountered: