Ricardo Machado
Ricardo Machado

Reputation: 847

Json Schema - Validate an infinite recursive structure

I'm trying to validate a json object using the "justinrainbow/json-schema" package for php.

Here's the json that i'm trying to validate:

{
"questions": [
     {
        "type": "section",
        "name": "Section one",
        "questions": [
            {
                "type": "section",
                "name": "Subsection 1.1" 
                "questions":[
                    {
                        "type": "section",
                        "name": "Subsection 1.1" 
                        "questions":
                             [
                                 {
                                      ...
                                 }
                             ]
                     }
                 ] 
             }
         ]
     }
 ]

The questions property can always be present inside questions property .... How can i validate it?

Thank you for your answers

Upvotes: 2

Views: 1033

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

You can use $ref to define a recursive structure.

{
  "type": "object",
  "properties": {
    "type": { "type": "string" },
    "name": { "type": "string" },
    "questions": { "type": "array", "items": { "$ref": "#"} }
  }
}

Upvotes: 2

Related Questions