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

问题描述 投票:0回答:2

我正在尝试使用 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
  }
}

对我的问题有什么建议吗?

json validation schema
2个回答
1
投票

我发现我的架构有问题

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

0
投票

我知道这是一个旧线程,但对于那些好奇的人来说,这也可以通过使用

"enable": { "const": true }
来提高模式可读性来完成。

(请参阅架构底部附近的 if 语句)

{
  "$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": {
        "properties": {
          "enable": {
            "const": true
          }
        }
      },
      "then": {
        "required": [
          "directory",
          "fileName",
          "workDirectory",
          "archiveDirectory",
          "interval",
          "byteThreshold"
        ]
      }
    }
  },
  "required": [
    "directory",
    "isamFile",
    "isamIndex",
    "port",
    "actions",
    "fields",
    "audit"
  ]
}

使用

enable: true

进行有效验证 使用
enable: false

进行有效验证
© www.soinside.com 2019 - 2024. All rights reserved.