Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

jsonschema - JSON schema does not validate missing properties

Thanks for your help in advance. Its my bad day.

I have the following json and I am trying to figure out the schema for it. Unfortunately, I was stuck at a point with no sign of error.

Please advise the solution

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

The Table_1,Table_2,.. are dynamically added. I have created schema that should validate JSON input in the following,

  1. tables_not_to_mask and tables_to_mask are required.
  2. tables_to_mask can have zero or more tables
  3. If there is table in tables_to_mask, it can have zero to many column and masking_type defined.
  4. column and masking_type are mandatory and no one is single.

I created the schema for it and unfortunately, if i remove column or masking_type, the schema does not throw any error.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally, I got the answer. Thanks for looking into it.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...