使用ajv了解JSON模式错误

问题描述 投票:0回答:1

我具有以下架构和json以使用ajv进行验证。

const schema = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [ "countries" ],
  "definitions": {
    "europeDef": {
      "type": "object",
      "required": ["type"],
      "properties": { "type": {"const": "europe"} }
    },
    "asiaDef": {
      "type": "object",
      "required": ["type"],
      "properties": { "type": {"const": "asia"} }
    }
  },
  "properties": {
    "countries": {
      "type": "array",
      "items": {
        "oneOf":[
          { "$ref": "#/definitions/europeDef" },
          { "$ref": "#/definitions/asiaDef"}
        ]
      }
    }
  }
}
const data = {
  "countries":[
    {"type": "asia"},
    {"type": "europe1"}
  ]
}
const isValid = ajv.validate(schema, data); //schema, data
if(! isValid){
  console.log(ajv.errors);
}

错误是:

[
  {
    keyword: 'const',
    dataPath: '/countries/1/type',
    schemaPath: '#/definitions/europeDef/properties/type/const',
    params: { allowedValue: 'europe' },
    message: 'should be equal to constant'
  },
  {
    keyword: 'const',
    dataPath: '/countries/1/type',
    schemaPath: '#/definitions/asiaDef/properties/type/const',
    params: { allowedValue: 'asia' },
    message: 'should be equal to constant'
  },
  {
    keyword: 'oneOf',
    dataPath: '/countries/1',
    schemaPath: '#/properties/countries/items/oneOf',
    params: { passingSchemas: null },
    message: 'should match exactly one schema in oneOf'
  }
]

我知道为什么会出现该错误(原因:因为我使用了'europe1'并且它不符合架构标准)] >>

由于上述错误情况,我有以下问题:

  1. 是的,我已经提供了'asia'作为有效的const,错误仍然提到'asia'作为数组第二个条目的一部分。从亚洲的角度来看,尽管有模式,但为何仍显示为错误,这是绝对好的。这是因为'oneOf'被使用了吗?换句话说,很难理解,什么是错误,什么是错误,什么不是错误?

  2. 对于亚洲,'消息:'应等于常数'(数组的第二项)误导了imo。它给人的印象是“亚洲”仍然存在一些问题。

  3. 如何解析此错误:基于schemaPath或dataPath?无论如何,它仍然会给人以“亚洲”(实际上不是)的印象。

  4. << [[此处为示例亚洲)
  5. 是否有任何理论/文献/指南可以更好地理解错误?

  • 我具有以下架构和json以使用ajv进行验证。 const schema = {“ $ schema”:“ http://json-schema.org/draft-07/schema#”,“ type”:“ object”,“ required”:[“ countries”],“ definitions”。 ..
  • jsonschema json-schema-validator ajv
    1个回答
    0
    投票
    我想知道这也可以回答您所有其他问题。

    [我认为您可能已经假设数组从1开始,并且数据路径实际上是在以asia对象为目标时,它是在引用europe1对象。

    如果我缺少某些东西,或者您对此仍然感到困惑,请发表评论。

    © www.soinside.com 2019 - 2024. All rights reserved.