ajv
ajv copied to clipboard
[Proposal] no need to check `type`, if `enum` is provided
Given:
example: {
type: `string`,
enum: [`0`, `1`]
}
The following shouldn't be generated/checked:
- as it will be further checked as part of the
enum
check for the exact values
if (typeof data1 !== `string`) {
const err0 = {
instancePath: instancePath + `/example`,
schemaPath: `#/properties/example`,
keyword: `type`,
params: { type: `string` },
}
if (vErrors === null) {
vErrors = [err0]
} else {
vErrors.push(err0)
}
errors++
}
What version of Ajv you are you using? 8.11.0
What problem do you want to solve? To have less generated code.
What do you think is the correct solution to problem? Not to check type
, if enum
is provided.
Will you be able to implement it? Yes
Arguably, in general case it might be more efficient to check for type first, as enum values can have different types, or they can be long strings, so removing type can be at best a micro-optimisation in some cases, but can slow things down in some other cases...
In any case, you can just not include type in the schema if you don't want it compiled - or am I missing something?
in general case it might be more efficient to check for type first
In general case, the validation would actually pass, so on average the performance would also increase, besides previously mentioned smaller output, hence shorter initialization time.
- so I already named three benefits
Yes, the type can be omitted, but it means the user-developer needs to add a custom post-processing.
- I believe the library should produce a better output by default, not requiring the user-developer to search for options to optimize the output
In general case, the validation would actually pass
excluding any attack scenarios...
Need to think about it.