【问题标题】:JSON schema does not validate missing propertiesJSON 模式不验证缺少的属性
【发布时间】:2020-12-30 15:10:31
【问题描述】:

提前感谢您的帮助。这是我糟糕的一天。

我有以下 json,我正在尝试找出它的架构。不幸的是,我被困在一个没有任何错误迹象的地方。

请告知解决方法

{
    "tables_not_to_mask": ["Table_1"],
    "tables_to_mask":{
        "Table_2": [
            {
                "column": "BinLogFilename",
                "masking_type": "replace_logfilename"
            },
            {
                "column": "ServerId",
                "masking_type": "replace_server_id"
            }
        ],
        "Table_3": [
            {
                "column": "BinLogFilename",
                "masking_type": "replace_logfilename"
            },
            {
                "column": "ServerId",
                "masking_type": "replace_server_id"
            }
        ]
     }
}

Table_1、Table_2、.. 是动态添加的。我已经创建了应该在下面验证 JSON 输入的架构,

  1. tables_not_to_mask 和 tables_to_mask 是必需的。
  2. tables_to_mask 可以有零个或多个表
  3. 如果tables_to_mask 中有表,它可以定义零到多列和masking_type。
  4. column 和 masking_type 是强制性的,没有一个是单一的。

我为它创建了架构,不幸的是,如果我删除列或 masking_type,架构不会引发任何错误。

    {
    "title": "Schema title",
    "description": "Description of the schema",
    "type": "object",
    "properties": {
        "tables_not_to_mask": {
            "type": "array",
            "minItems": 0,
            "items": {"type": "string"}
        },
        "tables_to_mask": {
            "type": "object",
            "patternProperties": {
                ".*": {
                    "type": "array",
                    "minItems": 0,
                    "properties": {
                        "column": {"type": "string"},
                        "masking_type": {"type": "string"}
                    },
                    "required": [
                        "masking_type",
                        "column"
                    ]
                }
            }
        }
    },
    "required": [
        "tables_not_to_mask",
        "tables_to_mask"
    ]
}

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    终于,我得到了答案。感谢您的调查。

     {
        "$schema": "https://json-schema.org/draft-04/schema#",
        "type": "object",
        "properties": {
            "tables_not_to_mask":
            {"$ref": "#/definitions/tables_not_to_mask_type"},
            "tables_to_mask":
            {"$ref": "#/definitions/tables_to_mask_type"}
        },
        "required": [
            "tables_not_to_mask",
            "tables_to_mask"
        ],
        "definitions": {
            "tables_not_to_mask_type": {
                "type": "array",
                "minItems": 0,
                "items": {"type": "string"}
            },
            "tables_to_mask_type": {
                "type": "object",
                "patternProperties": {
                    ".*": {"$ref": "#/definitions/tables_type"}
                }
            },
            "tables_type": {
                "type": "array",
                "minItems": 0,
                "items": {
                    "type": "object",
                    "properties": {
                        "column": {"type": "string"},
                        "masking_type": {"type": "string"}
                    },
                    "required": [
                        "masking_type",
                        "column"
                    ]
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2011-10-30
      • 2019-09-14
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多