JSON Schema Draft-07 if-then-else必需的字段验证似乎不正确

时间:2018-07-02 20:13:28

标签: json validation schema json-schema-validator

使用草案07

我得到的是 有效的JSON

我预期从审核对象发生什么错误是 目录:字符串长度必须大于或等于2

尝试了两个具有相同结果的不同验证器

  1. https://www.jsonschemavalidator.net/
  2. GoLang https://github.com/xeipuuv/gojsonschema

这是我的模式

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "ISAM-Wrapper",
  "description": "Validate isam wrapper json",
  "type": "object",
  "properties": {
    "directory": {
      "description": "path to location of isam file",
      "type": "string",
      "minLength": 2
    },
    "isamFile": {
      "description": "isam database file",
      "type": "string",
      "minLength": 4
    },
    "isamIndex": {
      "description": "isam index file",
      "type": "string",
      "minLength": 4
    },
    "port": {
      "description": "port number for REST listener",
      "type": "integer",
      "minimum": 60410,
      "maximum": 69999
    },
    "actions": {
      "description": "Which operations are supported",
      "type": "object",
      "items": {
        "properties": {
          "create": {
            "type": "boolean"
          },
          "read": {
            "type": "boolean"
          },
          "update": {
            "type": "boolean"
          },
          "delete": {
            "type": "boolean"
          }
        }
      },
      "required": [
        "create",
        "read",
        "update",
        "delete"
      ]
    },
    "fields": {
      "description": "each object describes one field of the isam file",
      "type": "array",
      "minItems": 1,
      "items": {
        "title": "field",
        "description": "field schema",
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1
          },
          "ordinal": {
            "type": "integer",
            "minimum": 0
          },
          "offset": {
            "type": "integer",
            "minimum": 0
          },
          "length": {
            "type": "integer",
            "minimum": 1
          },
          "dataType": {
            "enum": [
              "uchar",
              "ulong",
              "long",
              "uint",
              "int",
              "ushort",
              "short"
            ]
          }
        },
        "required": [
          "name",
          "ordinal",
          "offset",
          "length",
          "dataType"
        ]
      }
    },
    "audit": {
      "description": "input needed to enable and configure isam auditing",
      "type": "object",
      "items": {
        "properties": {
          "enable": {
            "enum": [
              true,
              false
            ]
          },
          "directory": {
            "type": "string",
            "minLength": 2
          },
          "fileName": {
            "type": "string",
            "minLength": 4
          },
          "workDirectory": {
            "type": "string",
            "minLength": 2
          },
          "archiveDirectory": {
            "type": "string",
            "minLength": 2
          },
          "interval": {
            "type": "integer",
            "minimum": 1
          },
          "byteThreshold": {
            "type": "integer",
            "minimum": 1048576,
            "maximum": 1073741824
          }
        }
      },
      "required": [
        "enable"
      ],
      "if": {
        "not": {
          "properties": {
            "enable": {
              "enum": [
                false
              ]
            }
          }
        }
      },
      "then": {
        "required": [
          "directory",
          "fileName",
          "workDirectory",
          "archiveDirectory",
          "interval",
          "byteThreshold"
        ]
      }
    }
  },
  "required": [
    "directory",
    "isamFile",
    "isamIndex",
    "port",
    "actions",
    "fields",
    "audit"
  ]
}

这是我的JSON

{
  "directory": "./",
  "isamFile": "isam.dat",
  "isamIndex": "isam.idx",
  "port": 60410,
  "actions": {
    "create": true,
    "read": true,
    "update": true,
    "delete": true
  },
  "fields": [
    {
      "name": "F1",
      "ordinal": 0,
      "offset": 0,
      "length": 4,
      "dataType": "ulong"
    },
    {
      "name": "F2",
      "ordinal": 1,
      "offset": 4,
      "length": 4,
      "dataType": "ulong"
    }
  ],
  "audit": {
    "enable": true,
    "directory": "",
    "fileName": "file",
    "workDirectory": "./work",
    "archiveDirectory": "./archive",
    "interval": 5,
      "byteThreshold": 1500000
  }
}

1 个答案:

答案 0 :(得分:1)

您遇到的这个问题是您的架构无效。对于actionsaudit,都将它们指定为object,但是您不提供任何properties。但是, 要做的是指定一个包含属性的items键(这里什么也不做-这是array上的键)。

更正此错误后,架构将按预期运行,请参阅https://repl.it/repls/BlankWellmadeFrontpage