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.

JSONSchemaType requires "required" and "additionalProperties", unless it is oneOf schema

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version? 8.0.1, yes

Ajv options object

{}

Your code

type Test = {
  some: string;
  other: boolean;
};

const t: JSONSchemaType<Test> = {
  type: 'object',
  properties: {
    other: {
      type: 'boolean',
    },
    some: {
      type: 'string',
    },
  },
};

What results did you expect? Should be a valid type, instead the compiler throws

TS2322: Type '{ type: "object"; properties: { other: { type: "boolean"; }; some: { type: "string"; }; }; }' is not assignable to type 'JSONSchemaType<test, false>'.   Type '{ type: "object"; properties: { other: { type: "boolean"; }; some: { type: "string"; }; }; }' is not assignable to type '{ oneOf: readonly JSONSchemaType<test, false>[]; } & { [keyword: string]: any; $id?: string; $ref?: string; $defs?: { [x: string]: JSONSchemaType<Known, true>; }; definitions?: { ...; }; }'.     Property 'oneOf' is missing in type '{ type: "object"; properties: { other: { type: "boolean"; }; some: { type: "string"; }; }; }' but required in type '{ oneOf: readonly JSONSchemaType<test, false>[]; }'

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:30 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
epoberezkincommented, Apr 1, 2021

if this is strictNullCheck - then yes, it’s required to be true for this type to work correctly. It was in the docs previously but looks like with the site migration it was lost - needs to be added.

Btw, using this type is completely optional - it’s only needed for your validation function to be type guard, but you can also pass type parameter directly to ajv.compile - needs to be better documented.

4reactions
BenjD90commented, Apr 1, 2021

With version 8.0.3 and TypeScript 4.2.3, I get a wired error Type 'string' is not assignable to type 'never' (same as comment https://github.com/ajv-validator/ajv/issues/1521#issuecomment-810432163) if I set the strict mode of TypeScript to false 🤔

Here an exemple to reproduce the error : https://replit.com/@BenjaminDaniel1/AJV-issue-1521

Does any one has any idea about that ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Require one of several other properties in a JSON Schema
The required keyword validates to true when the property exists, and is false when it does not. Therefore it can be used in...
Read more >
ajv-validator/ajv - Gitter
You need to validate the schema against a modified meta-schema, that will be the same as draft-07 metaschema but with added additionalProperties: false...
Read more >
draft 2020-12 - Ajv JSON schema validator
All examples above are JSON Schemas that only require data to be of certain type to be valid. Most other keywords apply only...
Read more >
Applying Subschemas Conditionally - JSON Schema
The dependentRequired keyword conditionally requires that certain properties must be present if a given property is present in an object. For example, suppose ......
Read more >
AllOf, AnyOf, OneOf - Liquid Technologies
Notice in the OneOf schemas the ZipCode & PostCode are required. ... to the Additional Properties not), and if both of them validated...
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