ajv-validator / ajv-validator/ajv

OAS 3.0 schemas cannot make non-nullable references nullable

未关闭
#1,882 8 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
usage
主要语言
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.9.0

**Ajv options object**

```javascript
{allErrors: true}
```

**JSON Schema**

```json
{
"$defs": {
"Fork": {
"type": "object",
"properties": {
"id": {"type": "string"}
}
}
},
"type": "object",
"properties": {
"id": {"type": "string"},
"dataFork": {"$ref": "#/$defs/Fork"},
"resourceFork": {
"nullable": true,
"anyOf": [
{"$ref": "#/$defs/Fork"}
]
}
}
}
```

> **Note:** The issue relates to use of OAS 3.0's `nullable` keyword, but I replicated without using a complete OAS schema by using JSON schema's `$defs` instead.

**Sample data**

```json
{
"id": "ffff",
"dataFork": {"id": "aaaa"},
"resourceFork": null
}
```

**Your code**

```javascript
const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true})

const schema = {
"$defs": {
"Fork": {
"type": "object",
"properties": {
"id": {"type": "string"}
}
}
},
"type": "object",
"properties": {
"id": {"type": "string"},
"dataFork": {"$ref": "#/$defs/Fork"},
"resourceFork": {
"nullable": true,
"anyOf": [
{"$ref": "#/$defs/Fork"}
]
}
}
}

const validate = ajv.compile(schema)

test({
"id": "ffff",
"dataFork": {"id": "aaaa"},
"resourceFork": null
})

function test(data) {
const valid = validate(data)
if (valid) console.log("Valid!")
else console.log("Invalid: " + ajv.errorsText(validate.errors))
}
```

RunKit link - https://runkit.com/liamnichols/61ed680062e12c0008e66e2a

**Validation result, data AFTER validation, error messages**

```
error: "nullable" cannot be used without "type"
```

After adding `"type": "object"`:

```
"Invalid: data/resourceFork must be object, data/resourceFork must match a schema in anyOf"
```

**What results did you expect?**

Sample data to validate without any errors

**Are you going to resolve the issue?**

I dug into the code, but I got stuck and I need guidance on how best to approach the problem.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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