ajv-validator / ajv-validator/ajv

Add metadata annotations to UncheckedJSONSchemaType

Offen
#1,772 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
TypeScript
Sterne
14.8k
Forks
1k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

**What version of Ajv you are you using?**
8.6.3

**What problem do you want to solve?**
The `JSONSchemaType` does not check the types of [metadata annotations](https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#page-23).

This means that incorrect types can be assigned to keywords, which will then fail validation at runtime when compiled by Ajv.

e.g.
```ts
const testSchema: JSONSchemaType<{}> = { description: 4 }
```
This is valid for `JSONSchemaType`, despite it being invalid per the spec, as `description` must be a `string`.

The fact that these keywords are not typed seems inconsistent with the approach to other keywords in the validation spec (e.g. `minLength`), which are typed.

**What do you think is the correct solution to problem?**
Add the following to `UncheckedJSONSchemaType` - either after `$defs`, `definitions` etc, or by adding a new `MetadataKeywords` type:

```ts
{
default?: any;
description?: string;
examples?: any[];
readOnly?: boolean;
title?: string;
writeOnly?: boolean;
}
```
Notes on this:
1. `deprecated` is omitted as it was added in 2019-09 (not sure if this type is supposed to capture post-07 things or not)
2. unrestricted values are typed as `any`, which permits invalid JSON values, but is the approach taken elsewhere in the types for unrestricted values.

**Will you be able to implement it?**
Yes.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.