1

I want to check particular value for multiple entry in a json file. I wrote a JsonSchema but it is not show me correct result.

For example:

"student": [
                        {
                            "main": false, 
                            "name": "Robert"  
                        }, 
                        {
                            "main": true, 
                            "name": "Jannet"
                        },
                        {
                            "main": false, 
                            "name": "Julie"
                        }
                    ]

the json file will be validate if it has only one true for "main" and rest will be false. If it has multiple true it will not validate. I tried the below jsonSchema:

"student":{
                                "type":"array",
                                "items":{
                                    "type":"object",
                                    "required":["name","main"],
                                    "properties":{
                                        "name":{
                                            "type":"string"
                                        },
                                        "main":{
                                            "type":"boolean"
                                        }
                                    },
                                            "anyOf":[
                                        {
                                            "properties":{
                                                "main":
                                                {
                                                "enum":[true]

                                                }   
                                            }
                                        }
                                    ]
                                }
                            }
naqwe
  • 45
  • 5

1 Answers1

0

Unfortunately this isn't possible with JSON Schema.

The only way this COULD be possible with JSON Schema is if you also required that the item in the array that is main: true is the first item.

If you CAN mandate this, let me know in a comment and I'll update this answer to explain how this would work.

Relequestual
  • 11,631
  • 6
  • 47
  • 83