ajv-validator / ajv-validator/ajv

JSONSchemaType fails wiht type when a field is declared nullable

Open
#2,283 3 comments 0 reactions 0 assignees View on GitHub
limitation typescript usage
Dominant language
TypeScript
Stars
14.8k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

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

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.