我正在使用 ajv 来验证正文请求。随着每个请求的到来,ajv 工作正常,但它总是记录消息 '$ref: keywords ignored in schema at path "#" '
我有 2 个模式,login.json 和 login.defs.json
login.defs.json 定义通用模式定义,login.json 引用它。
登录.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"$id": "http://blog-js.com/login.schema#",
"$ref": "login.defs#/definitions/login"
}
login.defs.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://blog-js.com/login.defs#",
"additionalProperties": false,
"definitions": {
"login": {
"type": "object",
"required": [
"account",
"password"
],
"properties": {
"account": {
"description": "The account or email of user",
"type": "string",
"minLength": 1,
"maxLength": 255
},
"password": {
"description": "The password of user",
"type": "string",
"minLength": 1,
"maxLength": 32
}
}
}
}
}
请告诉我我做错了什么?
最佳答案
我认为这是因为您在错误的位置设置了 additionalProperties
关键字,而 Ajv 只是告诉了您这一点。
如果那是 login.json
的架构,您不应该看到该消息。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://blog-js.com/login.schema#",
"$ref": "login.defs#/definitions/login"
}
对于 login.defs.json
,该关键字应属于 login
的架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://blog-js.com/login.defs#",
"definitions": {
"login": {
"type": "object",
"required": [
"account",
"password"
],
"properties": {
"account": {
"description": "The account or email of user",
"type": "string",
"minLength": 1,
"maxLength": 255
},
"password": {
"description": "The password of user",
"type": "string",
"minLength": 1,
"maxLength": 32
}
},
"additionalProperties": false
}
}
}
关于javascript - NodeJS Ajv 模块总是记录消息 ' $ref: keywords ignored in schema at path "#"',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766764/