question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use typescript validation with const fields in schema

See original GitHub issue

What 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:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Ungacommented, May 17, 2022

constantValue: { type: 'string', const: 'FOO' }

0reactions
theahuracommented, May 19, 2022

That did it. Thanks so much!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found