ajv-validator / ajv-validator/ajv
JSONSchemaType incorrectly requires optional properties to be nullable
- 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?**
Ajv 8.6.0, Typescript 4.3.4
**Your typescript code**
```typescript
import { JSONSchemaType } from 'ajv/dist/2020';
interface Example {
foo: string;
bar?: string;
}
const schema: JSONSchemaType = {
additionalProperties: false,
type: 'object',
properties: {
foo: {
type: 'string'
},
bar: {
type: 'string'
}
},
required: ['foo']
};
```
**Typescript compiler error messages**
```
TS2322: Type '{ additionalProperties: false; type: "object"; properties: { foo: { type: "string"; }; bar: { type: "string"; }; }; required: "foo"[]; }' is not assignable to type 'UncheckedJSONSchemaType'.
Type '{ additionalProperties: false; type: "object"; properties: { foo: { type: "string"; }; bar: { type: "string"; }; }; required: "foo"[]; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
Type '{ additionalProperties: false; type: "object"; properties: { foo: { type: "string"; }; bar: { type: "string"; }; }; required: "foo"[]; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
The types of 'properties.bar' are incompatible between these types.
Type '{ type: "string"; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType & { nullable: true; const?: undefined; enum?: readonly (string | null | undefined)[] | undefined; default?: string | ... 1 more ... | undefined; })'.
Type '{ type: "string"; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly UncheckedPartialSchema[] | undefined; anyOf?: readonly UncheckedPartialSchema[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
Property 'nullable' is missing in type '{ type: "string"; }' but required in type '{ nullable: true; const?: undefined; enum?: readonly (string | null | undefined)[] | undefined; default?: string | null | undefined; }'.
```
**Describe the change that should be made to address the issue?**
This code will not compile unless bar is marked as `nullable` in the schema. However, I don't want to accept `null` as a value for bar if it is present. It appears that `JSONSchemaType` expects all properties not mentioned in the `required` array to be marked as `nullable`. `null` and `undefined` are different values and should not be conflated.
**Are you going to resolve the issue?**
🤷
Contributor guide
Assessment
This issue has not been assessed yet.