NodeJS Ajv 模块总是记录消息“$ref: keywords ignored in schema at path "#" '

NodeJS Ajv module alway log message ' $ref: keywords ignored in schema at path "#" '

我正在使用 ajv 来验证正文请求。随着每个请求的到来,ajv 工作正常,但它总是记录消息 ' $ref: keywords ignored in schema at path "#" '

我有 2 个模式,login.json & login.defs.json

login.defs.json定义一个通用的模式定义,login.json引用它。

login.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
    }
  }
}