ajv-validator / ajv-validator/ajv
typescript `JSONSchemaType`
- Vorherrschende Sprache
- TypeScript
- Sterne
- 14.8k
- Forks
- 1k
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
**What version of Ajv are you using? Does the issue happen if you use the latest version?**
**Ajv version**
8.11.0
**Ajv options object**
```javascript
```
**JSON Schema**
```json
```
**Sample data**
```json
```
**Your code**
```typescript
import type { JSONSchemaType } from 'ajv'
import Ajv from 'ajv'
type MyData = {
[id: number]: 42;
}
// generated from `typescript-json-schema tsconfig.json MyData`
const schemaMyData: JSONSchemaType = {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"patternProperties": {
"^[0-9]+$": {
"enum": [
42
],
"type": "number"
}
},
"type": "object"
}
const ajv = new Ajv()
const validateMyData = ajv.compile(schemaMyData)
validateMyData({ "a1": 42 }) // false
validateMyData({ "11": 24 }) // false
validateMyData({ "11": 42 }) // true
```
**Validation result, data AFTER validation, error messages**
```shell
$ typescript-json-schema tsconfig.json MyData
```
schema
```typescript
const schemaMyData: JSONSchemaType = {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"patternProperties": {
"^[0-9]+$": {
"enum": [
42
],
"type": "number"
}
},
"type": "object"
}
```
got ts error
```
Type '{ $schema: string; additionalProperties: false; patternProperties: { "^[0-9]+$": { enum: number[]; type: string; }; }; type: "object"; }' is not assignable to type 'UncheckedJSONSchemaType'.
Types of property '"patternProperties"' are incompatible.
Type '{ "^[0-9]+$": { enum: number[]; type: string; }; }' is not assignable to type 'Record>'.
Property '"^[0-9]+$"' is incompatible with index signature.
Type '{ enum: number[]; type: string; }' is not assignable to type 'UncheckedJSONSchemaType'.
Type '{ enum: number[]; type: string; }' is not assignable to type '{ type: readonly never[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }'.
Type '{ enum: number[]; type: string; }' is not assignable to type '{ type: readonly never[]; }'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type 'readonly never[]'.ts(2322)
```
**What results did you expect?**
no typescript error
**Are you going to resolve the issue?**
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.