-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
removeAdditional doesn't work well with anyOf, oneOf or allOf #129
Comments
It works as it should in this situation. Look at this working example. The first instance of ajv does not have removeAdditional option. And it correctly fails on obj3. The second instance has removeAdditional option as true. When it validates obj1 it passes because the first schema in anyOf has 'a' property in properties. When it validates obj2 using the first schema in anyOf property 'b' is additional there. So it gets removed, as you can see from the log. But the first schema in anyOf fails and it goes on to try to validate it with the second schema. It fails too because there is no property 'b' (it was removed).
I don't think there is a real need to define the concept of "unknown property" because usually there are ways to refactor your schema to make it work (and remove what you need). Difficult to say how exactly of course without seeing the actual schema, but usually you just need to take out properties from keywords anyOf, oneOf. The example above doesn't need to have properties inside anyOf. The equivalent (and faster) refactored schema is: {
type: 'object',
additionalProperties: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'string' }
},
additionalProperties: false,
anyOf: [
{ required: ['a'] },
{ required: ['b'] }
]
}
} With this schema it will correctly fail on obj3 if you pass |
Thanks for the suggestion, I didn't realize I could combine I'm not sure if this is just me, but took me quite a while to understand this behavior of the But again, thanks for your quick response! |
It just that it uses the same definition of "additional" as JSON-schema standard - it means "additional" according to the current (sub)schema, i.e. not present in "properties" and not matching "patternProperties" of the same (sub)schema, and it does not mean "unknown" according to the whole schema. For example, this schema should fail on any data because of that:
This would alway fail too:
But it definitely not just you, it takes some time to understand how the standard works... The main thing here is that all the keywords are shallow, they can only be affected by siblings in the same object. I will think again about implementing this "unknown" properties concept - both to fail on unknown properties and to filter them out. There are cases when it is either difficult to refactor schema (I haven't come across the cases where it is impossible though...) or undesirable, because it makes schema lose it's meaning from the application domain point of view. |
Closing this issue, please let mw know if there are any questions. |
@epoberezkin would you accept a PR that removed the additional properties only after passing validation of the schema? That way, if a subschema fails validation for some other reason, the properties aren't removed. Something like |
@sberan so rather than removing them straight away, |
Firstly, thank you for AJV and the continual support you provide for AJV and for the JSON-schema standard in general. I read both this thread and #134, and I must say, I still disagree that this behavior makes sense. Furthermore, the compound anyOf/allOf/oneOf keywords specifically modify the validation result of the (parent) schema, and it would make more sense to validate the parent schema, and then to remove additional properties by using the 'correct path' of the validated schema. Of course the current implementation does not have this notion, but consider for a moment the annotations feature in the JSON schema specification: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.7.7 Let's assume for a moment that we implemented the non-standard removeAdditional as an annotation on the schemas. Now this behavior is defined as an annotation on the schema which different validators can implement. In this instance, I would fully expect the annotation to be resolved according to JSON-schema specification - namely, that the 'valid' instance from anyOf would collect the annotation + JSON pointer from the valid subschema, as specified here: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.1.2 This feature would still be working in a standard fashion according to how JSON-schema rules apply, but in this case it would also be aligned with the myriad developers who thought this is how it should work to begin with. Basically, the fact that the implementation of non-standard removeAdditional is aligned with the standard validation specification, does not mean it is expected nor correct. We can easily align it with another standard specification, whether or not the actual implementation uses that feature (annotations - which it could have under the hood, by modifying schemas as they are added, for example) What do you think? |
I think it’s better to keep removeAdditional as is and add removeUnevaluated as proposed in #1346 |
I do agree that Ajv doesn’t need to align with spec 100% (and in this case it’s indeed non-standard) - strict mode is the step in that direction - but there are other considerations here. Your argument would apply if it was a new feature, but now that it’s used by lots of people it would be more confusing if it’s changed. |
I’m probably going to steal the idea from svelte.dev website - the new website will have space for logos of all users. |
The setting already has 4 different modes. I'm sure the existing users wouldn't care if a fifth mode was added. |
It seems like const ajv = new Ajv({removeAdditional: 'all'})
const validate = ajv.compile({
type: "object",
allOf: [
{
type: "object",
properties: {
a: { type: "string" },
},
required: ["a"],
},
{
type: "object",
properties: {
b: { type: "number" },
},
required: ["b"],
},
],
});
const data = { a: "hello", b: 3000, c: true }
const success = validate(data);
console.log(data);
console.log(validate.errors); Outputs
Seems like this is another use case for |
allOf actually works as it should, but you probably want anyOf? Edit: sorry, no, it's removeAdditional. This option defines "additional" locally, with regards to the current schema object – so it removes additional b and c properties when it evaluates the first subschema |
@epoberezkin,
For our use case, we have a similar schema where we have |
Hi, first of all, awesome work with this validator! Very handy!
Now here is the problem I'm facing, I'm not sure if this is the expected behavior, but if it is, it's not very intuitive... my scenario is a bit more complex, but I'll simplify in the following example.
I'm initializing ajv with:
This is the sample schema:
It should accept an object with as many objects as we want, and each of these objects should have at least one property called
a
, or one calledb
. Ifa
orb
are not present, it should fail. It should also discard any other property that is nota
orb
.Now, with this data:
It should fail only on
obj3
, but that's only the case whenremoveAdditional
isfalse
. IfremoveAdditional
istrue
, it seems like as soon as it evaluates the first option of theanyOf
, it removes the additional properties of the object before trying the second option.the removeAdditional is very useful, but it needs to wait until all possibilities of oneOf to be evaluated before removing anything.
This is the error I get by running the code above:
As you can see,
obj2
is failing because it's missing propertyb
, which is not true.I called validate with
The text was updated successfully, but these errors were encountered: