ajv-validator / ajv-validator/ajv

JSONSchemaType fails wiht type when a field is declared nullable

Aberta
#2,283 3 comentários 0 reações 0 responsáveis Ver no GitHub
limitation typescript usage
Linguagem predominante
TypeScript
Estrelas
14.8k
Forks
1k
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

**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 = {

```

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.