You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am defining a schema for an interface that has a property of type Date. I use the type safety provided by AJV and Typescript by specifying that the type of my schema is JSONSchemaType<MyInterface>.
Expected behaviour
I can define the date property in the schema like this
When defining my schema like this i get this (typescript) error:
Type '{ type: "object"; additionalProperties: false; required: "dateProperty"[]; properties: { dateProperty: { type: string; format: string; }; }; }' is not assignable to type 'JSONSchemaType<TypeWithDate>'.
Type '{ type: "object"; additionalProperties: false; required: "dateProperty"[]; properties: { dateProperty: { type: string; format: string; }; }; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<TypeWithDate, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; }) | ({ ...; } & ... 2 more ... & { ...; })'.
Type '{ type: "object"; additionalProperties: false; required: "dateProperty"[]; properties: { dateProperty: { type: string; format: string; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
Type '{ type: "object"; additionalProperties: false; required: "dateProperty"[]; properties: { dateProperty: { type: string; format: string; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
The types of 'properties.dateProperty' are incompatible between these types.
Type '{ type: string; format: string; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<Date, false> & { nullable?: false | undefined; const?: Date | undefined; enum?: readonly Date[] | undefined; default?: Date | undefined; })'.
Type '{ type: string; format: string; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Date, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<...>> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; })'.
Type '{ type: string; format: string; }' is not assignable to type '{ oneOf: readonly UncheckedJSONSchemaType<Date, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<...>> | undefined; definitions?: Record<...> | undefined; } & { ...; }'.
Property 'oneOf' is missing in type '{ type: string; format: string; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType<Date, false>[]; }'.ts(2322)
The important line being this one:
Property 'oneOf' is missing in type '{ type: string; format: string; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType<Date, false>[]; }'
Minimal Reproducible Example
Setup any project with typescript and AJV then paste this code:
constAJV_TYPESAFE_SCHEMA_DATETIME_BUGFIX={oneOf: [],};// Then when defining the schemaconstSCHEMA: JSONSchemaType<TypeWithDate>={// ...properties: {dateProperty: {type: "string",format: "date-time", ...AJV_TYPESAFE_SCHEMA_DATETIME_BUGFIX},},}
Context
I am defining a schema for an interface that has a property of type
Date
. I use the type safety provided by AJV and Typescript by specifying that the type of my schema isJSONSchemaType<MyInterface>
.Expected behaviour
I can define the date property in the schema like this
This is how it is supposed to work according to json-schema.org, e.g. see: https://json-schema.org/learn/json-schema-examples#blog-post
Actual behaviour and Error Message
When defining my schema like this i get this (typescript) error:
The important line being this one:
Minimal Reproducible Example
Setup any project with typescript and AJV then paste this code:
Workaround
Versions
Typescript
AJV
Cause and Fix
Im quite new to AJV but i think the bug is caused by the code in
ajv/dist/types/json-schema.d.ts (line 21)
Edit: fixed cause and fix heading
The text was updated successfully, but these errors were encountered: