Andreas
Andreas

Reputation: 11

JSON Schema - Required object by value

it is possible to validate required values in a JSON Schema?

I have the following JSON:

{
    "genericData": [
        {
            "name": "field_one",
            "value": "data_one"
        },
        {
            "name": "field_two",
            "value": [
                "array_data_one",
                "array_data_two"
            ]
        },
        {
            "name": "field_three",
            "value": {
                "attr_one": "some_data",
                "attr_two": "more_data"
            }
        }
    ]
}

For validating the objects in the array i have the following JSON Schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "properties": {
        "genericData": {
            "type":"array",
            "minItems": 2,
            "items": [
                {
                    "type": "object",
                    "if": {
                        "properties": {
                            "name": {
                                "enum": [
                                    "field_one"
                                ]
                            }
                        }
                    },
                    "then": {
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "value": {
                                "type": "string"
                            }
                        }
                    }
                },
                {
                    "type": "object",
                    "if": {
                        "properties": {
                            "name": {
                                "enum": [
                                    "field_two"
                                ]
                            }
                        }
                    },
                    "then": {
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "value": {
                                "type": "array",
                                "minItems": 1,
                                "items": [
                                    {
                                        "type": "string"
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "type": "object",
                    "if": {
                        "properties": {
                            "name": {
                                "enum": [
                                    "field_three"
                                ]
                            }
                        }
                    },
                    "then": {
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "value": {
                                "type": "object",
                                "properties": {
                                    "attr_one": {
                                        "type": "string"
                                    },
                                    "attr_two": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "attr_one",
                                    "attr_two"
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

Now my question is: Is it possible to set the objects e.g. with the names "field_one" and "field_two" as required? I tried to set the propertie "name", in the "then" clause as required, but this has no impact!

Thanks, Andreas

Upvotes: 0

Views: 185

Answers (1)

Andreas
Andreas

Reputation: 11

I found the solution. The keywords "allOf" and "contains" in combination with "pattern" did what i want:

...
"genericData": {
            "type":"array",
            "minItems": 2,
            "allOf": [
                {
                    "contains": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string",
                                "pattern": "^field_one$"
                            }
                        }
                    }
                },
                {
                    "contains": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string",
                                "pattern": "^field_two$"
                            }
                        }
                    }
                }
            ],
            "items": [
                {
                    "type": "object",
                    "if": {
                        "properties": {
                            "name": {
                                "enum": [
                                    "field_one"
                                ]
                            }
                        }
                    },
...

Upvotes: 1

Related Questions