首页 > 解决方案 > JSON Schema Draft-07 if-then-else 必填字段验证似乎不正确

问题描述

使用 Draft-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
  }
}

标签: jsonvalidationschemajson-schema-validator

解决方案


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

更正此错误后,架构将按照您的意愿运行,请参阅https://repl.it/repls/BlankWellmadeFrontpage


推荐阅读