ajv-validator / ajv-validator/ajv

JSONSchemaType fails wiht type when a field is declared nullable

Abierto
#2,283 3 comentarios 0 reacciones 0 asignados Ver en GitHub
limitation typescript usage
Lenguaje dominante
TypeScript
Estrellas
14.8k
Forks
1k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

8.12.0

**Ajv options object**

export const ajv = new Ajv({ strict: true })

I'm using Typescript

```typescript
interface MyType {
id: string
value: string | null
}

const MySchema: JSONSchemaType = {
type: "object",
properties: {
id: { type: "string" },
value: { type: "string", nullable: true },
},
}
export const validateMySchema = ajv.compile(MySchema)
```

I'm getting this issue:

```
src/user/validation.ts:13:7 - error TS2322: Type '{ type: "object"; properties: { id: { type: "string"; }; value: { type: "string"; nullable: true; }; }; }' is not assignable to type 'JSONSchemaType'.
The types of 'properties.value' are incompatible between these types.
Type '{ type: "string"; nullable: true; }' is not assignable to type '{ $ref: string; } | ({ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } & ... 2...'.
Types of property 'nullable' are incompatible.
Type 'true' is not assignable to type 'false'.

13 const MySchema: JSONSchemaType = {
```

Also adding null as type:

```typescript
const MySchema: JSONSchemaType = {
type: "object",
properties: {
id: { type: "string" },
value: { type: ["string", "null"], nullable: true },
},
}
export const validateMySchema = ajv.compile(MySchema)

```

I have the same error:

```
src/user/validation.ts:13:7 - error TS2322: Type '{ type: "object"; properties: { id: { type: "string"; }; value: { type: ("string" | "null")[]; nullable: true; }; }; }' is not assignable to type 'JSONSchemaType'.
The types of 'properties.value' are incompatible between these types.
Type '{ type: ("string" | "null")[]; nullable: true; }' is not assignable to type '{ $ref: string; } | ({ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } & ... 2...'.
Types of property 'nullable' are incompatible.
Type 'true' is not assignable to type 'false'.

13 const MySchema: JSONSchemaType = {
```

also removing the `nullable`, same issue:

```typescript
const MySchema: JSONSchemaType = {
type: "object",
properties: {
id: { type: "string" },
value: { type: ["string", "null"] },
},
}
export const validateMySchema = ajv.compile(MySchema)
```

```
Type '{ type: "object"; properties: { id: { type: "string"; }; value: { type: ("string" | "null")[]; }; }; }' is not assignable to type 'JSONSchemaType'.
The types of 'properties.value' are incompatible between these types.
Type '{ type: ("string" | "null")[]; }' is not assignable to type '{ $ref: string; } | ({ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } & ... 2...'.
Type '{ type: ("string" | "null")[]; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly UncheckedPartialSchema[] | undefined; anyOf?: readonly UncheckedPartialSchema[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
Type '{ type: ("string" | "null")[]; }' is not assignable to type '{ type: "string"; }'.
Types of property 'type' are incompatible.
Type '("string" | "null")[]' is not assignable to type '"string"'.

13 const MySchema: JSONSchemaType = {

```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.