Array `items` property should not be required in all array schemas
What version of Ajv are you using? Does the issue happen if you use the latest version?
Your typescript code
const schema: JSONSchemaType<any[]> = { type: 'array' }
OR
const schema: JSONSchemaType<any[]> = { type: 'array', contains: { type: 'number' } }
Typescript compiler error messages
TS2322: Type '{ type: "array"; }' is not assignable to type 'UncheckedJSONSchemaType<any[], false>'. Type '{ type: "array"; }' is not assignable to type '{ type: "array"; items: UncheckedJSONSchemaType<any, false>; contains?: UncheckedPartialSchema<any> | undefined; minItems?: number | undefined; ... 4 more ...; additionalItems?: undefined; } & { ...; } & { ...; }'. Property 'items' is missing in type '{ type: "array"; }' but required in type '{ type: "array"; items: UncheckedJSONSchemaType<any, false>; contains?: UncheckedPartialSchema<any> | undefined; minItems?: number | undefined; ... 4 more ...; additionalItems?: undefined; }'.
Describe the change that should be made to address the issue?
When using the contains keyword, the items property should not be defined.
Json Schema Docs: https://json-schema.org/understanding-json-schema/reference/array.html#contains
type UncheckedJSONSchemaType<T, IsPartial extends boolean> = (
...
: T extends readonly any[]
? {
type: JSONType<"array", IsPartial>
items: UncheckedJSONSchemaType<T[0], false>
// ^^^^ Should be made optional
contains?: UncheckedPartialSchema<T[0]>
minItems?: number
maxItems?: number
minContains?: number
maxContains?: number
uniqueItems?: true
additionalItems?: never
}
...
)
Are you going to resolve the issue?
This could be as simple as making the items property optional. This is the simplest and probably the most accurate solution.
PR https://github.com/ajv-validator/ajv/pull/1888
Possibly... It might be a breaking change, need to think.
Did you test if above examples compile with this change? I don't think any is adequately supported, but I might be wrong here...
I've made the change in my node_modules/ajv/dist/types/json-schema.d.ts and the error goes away for both the examples above.
I also tried it with the type as JSONSchemaType<any[]> and JSONSchemaType<number[]> and didn't have any compile errors.