Cannot use typescript validation with const fields in schema
See original GitHub issueWhat version of Ajv are you using? Does the issue happen if you use the latest version? 8.11.0, yes in case it’s relevant, I’m using typescript 4.5.2
Your code
import Ajv, {JSONSchemaType} from "ajv"
const ajv = new Ajv()
interface Foo {
constantValue: 'FOO'
}
const schema: JSONSchemaType<Foo> = {
type: 'object',
properties: {
constantValue: {
const: 'FOO'
}
}
}
This will cause a typescript compilation error:
Type '{ type: "object"; properties: { name: { const: "FOO"; }; }; }' is not assignable to type 'UncheckedJSONSchemaType<Foo, false>'.
The types of 'properties.name' are incompatible between these types.
Type '{ const: "FOO"; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<"FOO", false> & { const?: "FOO" | undefined; enum?: readonly "FOO"[] | undefined; default?: "FOO" | undefined; })'.
Type '{ const: "FOO"; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly UncheckedPartialSchema<"FOO">[] | undefined; anyOf?: readonly UncheckedPartialSchema<"FOO">[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
Property 'type' is missing in type '{ const: "FOO"; }' but required in type '{ type: "string"; }'.
What results did you expect? Typescript would compile without issues.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Dynamic type validation in TypeScript - LogRocket Blog
The repo description states that is goal is to “automatically generate a validator using JSON Schema and AJV for any TypeScript type.”.
Read more >Using with TypeScript - Ajv JSON schema validator
when utility type is used, compiled JTD serializers only accept data of correct type (as they do not validate that the data is...
Read more >Refine validations on object definitions don't get triggered until ...
To avoid duplication, I created a function to add them to the base schema, but I cannot get the TypeScript typing to play...
Read more >Require none or both fields with Zod - Stack Overflow
I.e. if only startDate or only endDate is set, parsing will fail. The schema looks like: export const MediumSchema = z.object({ ImageSetID: z....
Read more >Better data validation in TypeScript | Altostra
It's a fact that user input cannot be trusted and must always be validated. Yet, code like in the example below is too...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
constantValue: { type: 'string', const: 'FOO' }
That did it. Thanks so much!