对象数组的 JSON 模式定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36757949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:23:59  来源:igfitidea点击:

JSON Schema definition for array of objects

jsonjsonschemajson-schema-validator

提问by Kyle Falconer

I've seen this other questionbut it's not quite the same, and I feel like my issue is simpler, but just isn't working.

我看过另一个问题,但并不完全相同,我觉得我的问题更简单,但不起作用。

My data would look like this:

我的数据看起来像这样:

[
    { "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},
    { "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null}
]

I'm wanting to use AJVfor JSON validation in a Node.js project and I've tried several schemas to try to describe my data, but I always get this as the error:

我想在 Node.js 项目中使用AJV进行 JSON 验证,并且我尝试了几种模式来尝试描述我的数据,但我总是将其视为错误:

[ { keyword: 'type',
    dataPath: '',
    schemaPath: '#/type',
    params: { type: 'array' },
    message: 'should be array' } ]

The schema I've tried looks like this:

我尝试过的架构如下所示:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "loc": {
        "type": "string"
      },
      "toll": {
        "type": "string"
      },
      "message": {
        "type": "string"
      }
    },
    "required": [
      "loc"
    ]
  }
}

I've also tried to generate the schema using this online toolbut that also doesn't work, and to verify that that should output the correct result, I've tried validating that output against jsonschemavalidator.net, but that also gives me a similar error:

我还尝试使用此在线工具生成架构,但这也不起作用,并验证是否应该输出正确的结果,我尝试针对jsonschemavalidator.net验证该输出,但这也给了我一个类似错误:

Found 1 error(s)
 Message:
 Invalid type. Expected Array but got Object.
 Schema path:
 #/type

回答by Jason Desrosiers

You have defined your schema correctly, except that it doesn't match the data you say you are validating. If you change the property names to match the schema, you still have one issue. If you want to allow "toll" and "message" to be null, you can do the following.

您已正确定义架构,只是它与您所说的正在验证的数据不匹配。如果您更改属性名称以匹配架构,您仍然会遇到一个问题。如果要允许“toll”和“message”为空,可以执行以下操作。

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "loc": {
        "type": "string"
      },
      "toll": {
        "type": ["string", "null"]
      },
      "message": {
        "type": ["string", "null"]
      }
    },
    "required": [
      "loc"
    ]
  }
}

However, that isn't related to the error message you are getting. That message means that data you are validating is not an array. The example data you posted should not result in this error. Are you running the validator on some data other than what is posted in the question?

但是,这与您收到的错误消息无关。该消息意味着您正在验证的数据不是数组。您发布的示例数据不应导致此错误。除了问题中发布的数据之外,您是否正在对某些数据运行验证器?