首页 > 解决方案 > NodeJS Ajv 模块总是记录消息'$ref:路径“#”的架构中忽略的关键字'

问题描述

我正在使用 ajv 来验证正文请求。随着每一个请求的到来,ajv 工作正常,但它总是记录消息' $ref:在路径“#”的架构中忽略的关键字'

我有 2 个架构,login.jsonlogin.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
        }
      }
    }
  }
}

请告诉我我做错了什么?

标签: javascriptnode.jsjsonschemaajv

解决方案


我认为这是因为您将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
    }
  }
}

推荐阅读