Reusing JSON subschema

Viewed 309

I am needing to use a sub schema multiple times in my JSON file, but haven't been able to figure out the correct way to structure the schema file such that I am able to get the schema validation on all the sub properties instead of just the property that I list in the schema file.

This question here was getting at a similar question, but the answer didn't make much sense/I wasn't sure if or how I could use the same method here. Am I thinking too much in the OOP mindset with multiple instances of a single class?

Here is more or less what I am trying to do

{
    "Object1": {
        "Title": "Some Title",
        "Description": "Some Description"
    },
    "Object2": {
        "Title": "Another title",
        "Description": "Another Description"
    }
// unknown number of objects but each object should have the same sub schema
}

Here is what I have thus far

{
    "$id": "http://example.com/example.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "required": [
        "Object1"
    ],
    "title": "The root schema",
    "type": "object",
    "properties": {
        "Object1": {
            "required": [
                "Title",
                "Description"
            ],
            "title": "The Reusable Object schema",
            "type": "object",
            "properties": {
                "Title": {
                    "title": "The Title schema",
                    "type": "string"
                },
                "Description": {
                    "title": "The Description schema",
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    },
    "additionalProperties": true
}
1 Answers

If all values of the object should follow the schema, the solution is quite simple.

First, you have to remember how additionalProperties works...

The value of "additionalProperties" MUST be a valid JSON Schema.

This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.

Validation with "additionalProperties" applies only to the child
values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".

For all such properties, validation succeeds if the child instance validates against the "additionalProperties" schema.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.6

So, now we know that additionalProperties takes a JSON Schema, and not just booleans (booleans are valid JSON Schema), the solution might be a little obvious.

Remove the outermost additionalPropertie, rename properties to additionalProperties, and remove the key Object1 and object braces.

The result is the following...

...
"title": "The root schema",
  "type": "object",
  "additionalProperties": {
    "required": [
      "Title",
      "Description"
    ],
...

Live demo: https://jsonschema.dev/s/pqwCc

I don't know what you would want to do with the outer most required though. I guess remove it, as you don't know in advance what the keys will be.

Maybe you want to use minProperties to make sure there is at least one?

Related