JSON schema anyOf properties required with a specific error message

136 Views Asked by At

I'm trying to implement JSON validation using Python jsonschema library. I want to have at least one of the three properties to be required in my JSON. I'm using anyOf subschema to achieve that. This rule should apply to different sets of properties, therefore I'm enclosing anyOfs within allOf:

{
    "type": "object",
    "properties": {
        "a": {"type": "string"},
        "b": {"type": "string"},
        "c": {"type": "string"},
        "x": {"type": "string"},
        "y": {"type": "string"},
        "z": {"type": "string"}
    },
    "allOf": [
        "anyOf": [
            {"required": ["a"]},
            {"required": ["b"]},
            {"required": ["c"]}
        ],
        "anyOf": [
            {"required": ["x"]},
            {"required": ["y"]},
            {"required": ["z"]}
        ]
    ]
}

If I try to validate empty JSON {} against that schema I'm getting a generic error message {} is not valid under any of the given schemas. Is there a way to get a more specific (custom) message, e.g. One of 'x', 'y', 'z' is required?

1

There are 1 best solutions below

2
spacether On

How about using additionalProperties: false minProperties: 1?