ajv-validator / ajv-validator/ajv

Array `items` property should not be required in all array schemas

Aperta
#1,887 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub
typescript
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

ajv@8.9.0

**Your typescript code**

```typescript
const schema: JSONSchemaType = { type: 'array' }
```

OR

```typescript
const schema: JSONSchemaType = { type: 'array', contains: { type: 'number' } }
```

**Typescript compiler error messages**

```
TS2322: Type '{ type: "array"; }' is not assignable to type 'UncheckedJSONSchemaType'.   Type '{ type: "array"; }' is not assignable to type '{ type: "array"; items: UncheckedJSONSchemaType; contains?: UncheckedPartialSchema | undefined; minItems?: number | undefined; ... 4 more ...; additionalItems?: undefined; } & { ...; } & { ...; }'.     Property 'items' is missing in type '{ type: "array"; }' but required in type '{ type: "array"; items: UncheckedJSONSchemaType; contains?: UncheckedPartialSchema | 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

```typescript
type UncheckedJSONSchemaType = (
...
: T extends readonly any[]
? {
type: JSONType<"array", IsPartial>
items: UncheckedJSONSchemaType
// ^^^^ Should be made optional
contains?: UncheckedPartialSchema
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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.