ajv-validator / ajv-validator/ajv

JSONSchemaType with TypeScript is broken

未关闭
#2,558 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
bug report
主要语言
TypeScript
星标
14.8k
派生
1k
PR 合并指标
30 天内没有已合并 PR

描述

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

8.17.1

**TypeScript version**

5.8.3

**Ajv options object**

```javascript
// none
```

**JSON Schema**

For the bug report, I used the JSON from the documentation https://ajv.js.org/guide/typescript.html#utility-types-for-schemas

```json
{
"type": "object",
"properties": {
"foo": { "type": "integer" },
"bar": { "type": "string", "nullable": true }
},
"required": ["foo"],
"additionalProperties": false
}
```

**Sample data**

It's only about typing.

**Your code**

It's similar to the code in the documentation https://ajv.js.org/guide/typescript.html#utility-types-for-schemas, except that the JSON string is not written as string literal as part of the initialization, but separately

```typescript
const schemaFromObject = {
"type": "object",
"properties": {
"foo": { "type": "integer" },
"bar": { "type": "string", "nullable": true }
},
"required": ["foo"],
"additionalProperties": false
}
const schema: JSONSchemaType = schemaFromObject;
```

I also have variants reading the JSON from other files etc., which are also not working. But this is the smallest change compared to the documentation.

**Expected Result**

This code should work similar to https://ajv.js.org/guide/typescript.html#utility-types-for-schemas

**Actual Result**

A type error is emitted (by tsc):

```markdown
Type '{ type: string; properties: { foo: { type: string; }; bar: { type: string; nullable: boolean; }; }; required: string[]; additionalProperties: boolean; }' is not assignable to type 'JSONSchemaType'.
Type '{ type: string; properties: { foo: { type: string; }; bar: { type: string; nullable: boolean; }; }; required: string[]; additionalProperties: boolean; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; })'.
Type '{ type: string; properties: { foo: { type: string; }; bar: { type: string; nullable: boolean; }; }; required: string[]; additionalProperties: boolean; }' is not assignable to type '{ oneOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }'.
Property 'oneOf' is missing in type '{ type: string; properties: { foo: { type: string; }; bar: { type: string; nullable: boolean; }; }; required: string[]; additionalProperties: boolean; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType[]; }'.ts(2322)
json-schema.d.ts(25, 5): 'oneOf' is declared here.
---
const schema: JSONSchemaType
```

**Remarks**

There are a lot of related reports, here is only a list of some of them:
- #2521
- #2518
- #2283
- #2227
- #2043
- #2040
- #1988
- #1845
- #1664
- #1521

While one can argue in some cases that the schema has to be defined differently, I would say due to the large number of reports and even the example in the documentation failing, there is a fundamental problem in the type definition somewhere.

Fortunately there is a simple workaround:
```typescript
const schema = schemaFromObject as JSONSchemaType;
```
Of course, this does not check the schema at all.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。