基于布尔值的JSON模式验证if-then-else

时间:2018-06-29 16:06:03

标签: json validation schema

我正在尝试使用draft-07验证JSON,并正在使用https://www.jsonschemavalidator.net/进行测试

这是我正在使用的架构

 {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "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": {
            "type": "boolean"
          },
          "directory": {
            "type": "string",
            "minLength": 2
          },
          "fileName": {
            "type": "string",
            "maxLength": 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": { "properties": { "enable": true }
        },
        "then": {
          "required": [
            "directory",
            "fileName",
            "workDirectory",
            "archiveDirectory",
            "interval",
            "byteThreshold"
          ]
        }
      }
    }
  },
  "required": [
    "directory",
    "isamFile",
    "isamIndex",
    "port",
    "actions",
    "fields",
    "audit"
  ]
}

这是我尝试验证的JSON,由于“ enable:true”并且没有其余字段,但我希望通过验证。

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

关于我的问题有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我发现我的架构有问题

{
  "$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": {
            "type": "boolean"
          },
          "directory": {
            "type": "string",
            "minLength": 2
          },
          "fileName": {
            "type": "string",
            "maxLength": 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"
  ]
}