You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TL;DR: I want a property to support either a boolean OR a number value. With coercion enabled, true will be coerced to 1 or 1 will be coerced to true depending on the order of the types in the anyOf.
What version of Ajv are you using? Does the issue happen if you use the latest version?
constboolData={foo: true,bar: true}// Results in { foo: true, bar: 1 } constnumData={foo: 1,bar: 1}// results in { foo: true, bar: 1 }
What results did you expect?
constboolData={foo: true,bar: true}// I expect { foo: true, bar: true } constnumData={foo: 1,bar: 1}// I expect { foo: 1, bar: 1 }
Since coercion is done according to the order of the types in the schema, the resulting coerced type depends on whether boolean or number comes first, BUT I believe both results are incorrect.
The suggestion from this issue: #399 would have allowed us to fine tune coercion for this use case, but unfortunately I don't believe the suggestion to use keywords helps us in this case. I tried something similar to what was suggested in the comment in that issue but AJV still coerces the value after the keyword.
There must be something I'm missing here. This seems like a fairly common use case that seems broken. Please advise.
The text was updated successfully, but these errors were encountered:
Type coercion is an attempt to apply imperative feature, that depends on order, to declarative schema. So you will indeed be getting different results for different order of coercions, and the options are to expect it or to use type with array of strings instead of anyOf.
Hi @epoberezkin. Thanks for the response. My example here was purposefully simple just to get the point across but our implementation is quite complex, so doing something with an array of strings wouldn't really work. I mostly just wanted validation that keywords do not allow customizing coercion (as suggested in #399) as coercion will still be run after the keyword logic. Our solution for now is to switch to any in our fastify request schema, then later check what the type is supposed to be and do manual validation/coercion against a single type.
So we have found a solution for us, but I would still argue that having overlapping coercion rules with no way to customize the coercion is an issue.
TL;DR: I want a property to support either a boolean OR a number value. With coercion enabled,
true
will be coerced to1
or1
will be coerced totrue
depending on the order of the types in theanyOf
.What version of Ajv are you using? Does the issue happen if you use the latest version?
0.8.12
Ajv options object
JSON Schema
Sample data
What results did you expect?
Since coercion is done according to the order of the types in the schema, the resulting coerced type depends on whether boolean or number comes first, BUT I believe both results are incorrect.
The suggestion from this issue: #399 would have allowed us to fine tune coercion for this use case, but unfortunately I don't believe the suggestion to use keywords helps us in this case. I tried something similar to what was suggested in the comment in that issue but AJV still coerces the value after the keyword.
There must be something I'm missing here. This seems like a fairly common use case that seems broken. Please advise.
The text was updated successfully, but these errors were encountered: