0

I am very new to JSON Schema. I have a requirement in a array few items are mandatory and few are optional. Each item is having different validation rules. All the items may not come is sequence order. I have to create this schema only in draft04 version.

My JSON message is as below.

{
    "Event": {
        "AttributeList": [
            {
                "Attribute": {
                    "name": "OrderID",
                    "value": "String"
                }
            },
            {
                "Attribute": {
                    "name": "txnid",
                    "value": "Strinnkjnjknlg"
                }
            },
            {
                "Attribute": {
                    "name": "Appid",
                    "value": "Stg"
                }
            },
            {
                "Attribute": {
                    "name": "txnswitch",
                    "value": "false"
                }
            }
        ]
    }
}

I have to build a JSON schema where Attributes with the below conditions:

  1. The items(Attribute) under the Attribute list can have both mandatory and optional which is not working.Here "name" Appid and OrderID are mandatory and rest of them are optional ones.
  2. The items(Attribute) can be any sequence/order.
  3. Items shouldn't be repeated. I have written the below schema but couldn't achieve all the conditions.

JSON Schema is as follows

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "properties": {
            "Event": {
                "type": "object",
                "properties": {
                    "AttributeList": {
                        "type": "array",
                        "uniqueItems": true,
                        "items": {
                            "allOf": [
                                {
                                    "type": "object",
                                    "properties": {
                                        "Attribute": {
                                            "type": "object",
                                            "properties": {
                                                "name": {
                                                    "type": "string",
                                                    "enum": [
                                                        "OrderID"
                                                    ]
                                                },
                                                "value": {
                                                    "type": "string",
                                                    "maxLength": 16
                                                }
                                            },
                                            "required": [
                                                "name",
                                                "value"
                                            ],
                                            "optional": false
                                        }
                                    },
                                    "required": [
                                        "Attribute"
                                    ],
                                    "optional": false
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "Attribute": {
                                            "type": "object",
                                            "properties": {
                                                "name": {
                                                    "type": "string",
                                                    "enum": [
                                                        "txnid"
                                                    ]
                                                },
                                                "value": {
                                                    "type": "string",
                                                    "maxLength": 35
                                                }
                                            },
                                            "required": [
                                                "name",
                                                "value"
                                            ],
                                            "optional": true
                                        }
                                    },
                                    "required": [
                                        "Attribute"
                                    ],
                                    "optional": true
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "Attribute": {
                                            "type": "object",
                                            "properties": {
                                                "name": {
                                                    "type": "string",
                                                    "enum": [
                                                        "Appid"
                                                    ]
                                                },
                                                "value": {
                                                    "type": "string",
                                                    "maxLength": 8
                                                }
                                            },
                                            "required": [
                                                "name",
                                                "value"
                                            ],
                                            "optional": false
                                        }
                                    },
                                    "required": [
                                        "Attribute"
                                    ],
                                    "optional": false
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "Attribute": {
                                            "type": "object",
                                            "properties": {
                                                "name": {
                                                    "type": "string",
                                                    "enum": [
                                                        "txnswitch"
                                                    ]
                                                },
                                                "value": {
                                                    "type": "string",
                                                    "maxLength": 28
                                                }
                                            },
                                            "required": [
                                                "name",
                                                "value"
                                            ],
                                            "optional": true
                                        }
                                    },
                                    "required": [
                                        "Attribute"
                                    ],
                                    "optional": true
                                }
                            ]
                        }
                    }
                },
                "required": [
                    "AttributeList"
                ]
            }
        },
        "required": [
            "Event"
        ]
    }

This schema doesn't work as expected.

2
  • Can you expand on "couldn't achieve all the conditions" - what specifically were you having a problem with? Commented Mar 1, 2022 at 19:47
  • The conditions are as follows: 1) The items(Attribute) under the Attribute list can have both mandatory and optional which is not working. 2) All the items should not be in particular order 3) Items shouldn't be repeated. Also I have updated them in the query Commented Mar 2, 2022 at 7:49

1 Answer 1

1

Your schema isn't working because prefixItems isn't supported with draft4 (which is what your $schema keyword indicates), plus the schema under prefixItems will only be applied to the first item, not all items.

You can achieve what you need with contains combined with minContains and maxContains. For the mandatory items, use minContains: 1 and for the optional ones, use minContains: 0. You can set maxContains: 1 for both of these to ensure each item type can't appear twice.

https://json-schema.org/understanding-json-schema/reference/array.html#contains

You'll need an evaluator that supports at least draft2019-09 for minContains and maxContains.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply I have added prefixItems to assign separate schema for each item. I've changed that now. It is not possible to add evaluator here.Can you please tell me if there is anyother way to specify mandatory/optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.