【问题标题】:AJV validator always true, even with garbage inputAJV 验证器始终为真,即使有垃圾输入
【发布时间】:2019-02-26 01:57:06
【问题描述】:

我有三个架构

{
  "$id": "app.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "app",
  "description": "Root configuration object",
  "properties": {
    "views" : {
      "description": "The pages in the application",
      "type" : "object",
      "properties" : {
        "summary" : {
          "$ref": "view.schema.json"
        }
      },
      "additionalItems": {
        "$ref": "view.schema.json"
      },
      "required" : ["summary"]
    }
  },
  "required": ["views"]
}
{
  "$id" : "component.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title" : "component",
  "properties": {
    "type" : {
      "type" : "string",
      "enum" : ["container"]
    }
  },
  "anyOf": [
    {
      "properties": {
        "type" : {
          "enum" : ["container"]
        },
        "direction" : {
          "enum" : ["horizontal", "vertical"]
        }
      }
    }
  ]
}
{
  "$id" : "view.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title" : "view",
  "additionalProperties" : {
    "description": "The components in this view",
    "$ref" : "component.schema.json"
  }
}

我成功加载了这些文件并将它们添加到带有addSchema 的ajv 实例。但是,每当我将任何数据传递给 validate 时,即使是无效的乱码,验证也总是返回 true。

当我检查实例上的 _schemas 属性时,所有模式都存在并且看起来像它们的 json 文件定义的那样。

使用ajv-cli (ajv -s schemas/app.schema -r schemas/view.schema -r schemas/component.schema -d plugin.json) 测试等效项 失败并出现错误。我将验证器函数的主体复制粘贴到我的 IDE 中的临时文件和 jsfiddle 中,它在这两种环境中都可以工作。

【问题讨论】:

    标签: json ajv


    【解决方案1】:

    在将数据传递给验证函数之前,您需要对数据进行 JSON 解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2019-05-30
      • 2023-02-14
      相关资源
      最近更新 更多