具有'required'属性的JSON模式验证

时间:2019-01-09 15:02:23

标签: json validation

我正在使用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": {

  }
}

我没有收到任何错误,尽管我会因为要求“帐户”而出错。似乎所有内部​​“必填”字段均未得到验证。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现了问题。

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