【问题标题】:JSON schema validation with 'required' property具有“必需”属性的 JSON 模式验证
【发布时间】:2019-01-09 15:02:23
【问题描述】:

我正在使用https://www.jsonschemavalidator.net/ 并尝试验证我编写的架构。

架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "required": [
    "accounts"
  ],
  "accounts": {
    "required": "account",
    "properties": {
      "account": {
        "type": "array",
        "minItems": 1,
        "maxItems": 999,
        "required": [
          "scheme",
          "accountType",
          "accountSubType"
        ],
        "items": {
          "type": "object",
          "properties": {
            "scheme": {
              "description": "scheme",
              "type": "object",
              "required": [
                "schemeName",
                "identification"
              ],
              "properties": {
                "schemeName": {
                  "type": "string",
                  "maxLength": 40,
                },
                "identification": {
                  "type": "string",
                  "maxLength": 256
                },
                "name": {
                  "type": "string",
                  "maxLength": 70
                },
                "secondaryIdentification": {
                  "type": "string",
                  "maxLength": 35
                }
              }
            },
            "currency": {
              "type": "string",
              "format": "iso-4217",
              "pattern": "^[A-Z]{3,3}$",
              "maxLength": 3,
              "example": "EUR"
            },
            "accountType": {
              "type": "string"
            },
            "accountSubType": {
              "type": "string",
              "maxLength": 35
            }
          }
        }
      }
    }
  }
}

当我使用在线链接进行验证时

{}

我收到一个错误

Message:
Required properties are missing from object: accounts.
Schema path:
#/required

这是正确的,但当我这样做时

{
  "accounts": {

  }
}

我没有收到任何错误,尽管我应该收到一个错误,说“帐户”是必需的。似乎没有验证任何内部“必填”字段。

我该如何解决这个问题?

【问题讨论】:

    标签: json validation


    【解决方案1】:

    我发现了问题。

    1. 帐户需要位于“属性”中
    2. “必需”必须在“项目”内

    现在架构看起来像

        {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "properties": {
            "accounts": {
                "required": "account",
                "properties": {
                    "account": {
                        "type": "array",
                        "minItems": 1,
                        "maxItems": 999,
                        "items": {
                            "type": "object",
                            "required": [
                                "scheme",
                                "accountType",
                                "accountSubType"
                            ],
                            "properties": {
                                "scheme": {
                                    "description": "scheme",
                                    "type": "object",
                                    "required": [
                                        "schemeName",
                                        "identification"
                                    ],
                                    "properties": {
                                        "schemeName": {
                                            "type": "string",
                                            "maxLength": 40
                                        },
                                        "identification": {
                                            "type": "string",
                                            "maxLength": 256
                                        },
                                        "name": {
                                            "type": "string",
                                            "maxLength": 70
                                        },
                                        "secondaryIdentification": {
                                            "type": "string",
                                            "maxLength": 35
                                        }
                                    }
                                },
                                "currency": {
                                    "type": "string",
                                    "format": "iso-4217",
                                    "pattern": "^[A-Z]{3,3}$",
                                    "maxLength": 3,
                                    "example": "EUR"
                                },
                                "accountType": {
                                    "type": "string"
                                },
                                "accountSubType": {
                                    "type": "string",
                                    "maxLength": 35
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 2021-07-20
      • 2015-10-20
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      相关资源
      最近更新 更多