How to define choice element in json schema when elements are optional?

后端 未结 4 1891
深忆病人
深忆病人 2020-12-20 15:00

------------Josn schema-----------

{
    \"type\": \"object\",
    \"properties\": {
        \"street_address\": {
            \"type\": \"string\"
        }         


        
相关标签:
4条回答
  • 2020-12-20 15:31

    I found the enum property useful for this use case.

    Example:

    schema = {
        "type": "array", 
        "items": {
            "enum": ["choice1", "choice2"]
        }
    }
    
    validate(
        instance=["choice1"], 
        schema=schema
    )
    # all good
    
    validate(
        instance=["something-else"], 
        schema=schema
    )
    # ValidationError
    

    Reference: https://json-schema.org/understanding-json-schema/reference/combining.html#combining-schemas

    Hope it helps.

    0 讨论(0)
  • 2020-12-20 15:32

    You would need to use "oneOf". Like so:

    {
        "type": "object",
        "oneOf": [
            {
                "properties": {
                    "street_address": {
                        "type": "string"
                    },
                    "city": {
                        "type": "string"
                    }
                },
                "required": [
                    "street_address"
                ]
            },
            {
                "properties": {
                    "street_address": {
                        "type": "string"
                    },
                    "state": {
                        "type": "string"
                    }
                },
                "required": [
                    "street_address"
                ]
            }
        ]
    }
    

    You'll notice, it's a bit repetitive. Since, in your example, you only provide a "type" for each property, the repetition is not so bad. But if you have more complex properties, you could consider using deifinitions to define each property only once, at the top and then using $ref to reference the definition. Here's a good article on that.

    0 讨论(0)
  • 2020-12-20 15:35

    Here is a schema that satisfies all four conditions:

        {
            "type": "object",
            "properties": {
                "street_address": {
                    "type": "string"
                },
                "city": {
                    "type": "string"
                },
                "state": {
                    "type": "string"
                }
            },
            "required": [
                "street_address"
            ],
            "anyOf": [{}, {
                "required": ["city"]
            }, {
                "required": ["state"]
            }],
            "not": {
                "required": ["city", "state"]
            },
            "additionalProperties": false
        }
    
    0 讨论(0)
  • Use "oneOf" when only one of the alternatives should hold, and "anyOf" when at least one of the alternatives should hold.

    You don't need to repeat common properties within oneOf. The shortest way to accomplish your goal would be:

    {
        "type" : "object",
        "properties" : {
            "street_address" : {
                "type" : "string"
            },
            "city" : {
                "type" : "string"
            },
            "state" : {
                "type" : "string"
            }
        },
        "oneOf" : [{
                "required" : ["city"]
            }, {
                "required" : ["state"]
            }
        ],
        "required" : [
            "street_address"
        ],
        "additionalProperties" : false
    }
    
    0 讨论(0)
提交回复
热议问题