Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONSchema and validating sub-object properties

Given this JSON object:

{
    "objects": {
        "foo": {
            "id": 1,
            "name": "Foo"
        },
        "bar": {
            "id": 2,
            "name": "Bar"
        }
    }
}

This is an object containing sub objects where each sub object has the same structure - they're all the same type. Each sub-object is keyed uniquely, so it acts like a named array.

I want to validate that each object within the objects property validates against a JSON Schema reference.

If the objects property was an array, such as:

{
  "objects": [
    {
      "id": 1,
      "name": "Foo"
    },
    {
      "id": 2,
      "name": "Bar"
    }  
  ]
}   

I could validate this with a schema definition such as:

{
  "id": "my-schema",
  "required": [
    "objects"
  ],
  "properties": {
    "objects": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

This is achieved because the type is array, and this permits the validation of items.

Is it possible to do something similar, but with nested objects?

Thanks!

like image 739
mgldev Avatar asked Oct 27 '17 13:10

mgldev


People also ask

What does Exclusiveminimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).

How do you validate a JSON file against a schema?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

What does $Ref mean in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.

What is $id in JSON Schema?

Declaring a unique identifier It is also best practice to include an $id property as a unique identifier for each schema. For now, just set it to a URL at a domain you control, for example: { "$id": "http://yourdomain.com/schemas/myschema.json" }


1 Answers

You can try something like this:

{
  "id": "my-schema",
  "type": "object",
  "properties": {
    "objects": {
      "type": "object",
      "patternProperties": {
        "[a-z]+": {
          "type": "object",
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": [
            "id",
            "name"
          ]
        }
      }
    }
  }
}
like image 167
Pedro Avatar answered Oct 31 '22 19:10

Pedro