geojsonjsonschemajson-schema-validator

How do I add additional constraints to a property in a referenced schema in JSON Schema?


I am trying to write a JSON Schema to impose additional required properties in a referenced schema. I have looked here and here, but check-jsonschema tells me that I need the properties in the top level, not in the nested properties object. How do I add a constraint on a nested object property?

Attempted schema:

{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/sample.schema.json",
  "type": "object",
  "properties": {
    "boundaries": {
      "description": "Boundaries",
      "allOf": [
        { "$ref": "http://geojson.org/schema/Feature.json" },
        {
          "geometry": {
            "oneOf": [
              { "$ref": "http://geojson.org/schema/MultiPolygon.json" },
              { "$ref": "http://geojson.org/schema/Polygon.json" }
            ],
            "additionalProperties": false
          }
        },
        { 
          "type": "object",
          "required" : ["name"]
        }
      ]
    }
  }
}

I'd like the following to succeed, but the above validator asks me to put name at the same level as type, properties, and geometry.

JSON instance:

{
  "boundaries": {
    "type": "Feature",
    "properties": {
      "name": "Bounded Place",
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [ -100.0, 40.0 ],
          [ -100.0, 45.0 ],
          [ -110.0, 45.0 ],
          [ -110.0, 40.0 ]
        ]
      ]
    }
  }
}

Solution

  • You have a couple of issues with the attempted schema

    From my understanding, you want to require name in your data instance. To do this, you need to define the nested structure in an allOf subschema and then require that property. Because the property properties is defined in the geoJSON schema and it appears that is where you want this requirement, this is how you would achieve that.

    JSON Schema definition

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "https://example.com/sample.schema.json",
      "type": "object",
      "properties": {
        "boundaries": {
          "description": "Boundaries",
          "allOf": [
            {
              "type": "object",
              "properties": {
                "properties": {
                  "required": [
                    "name"
                  ]
                }
              }
            },
            {
              "$ref": "https://geojson.org/schema/Feature.json"
            }
          ]
        }
      }
    }
    

    data instance

    {
      "boundaries": {
        "type": "Feature",
        "properties": {
          "name": "Bounded Place"
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [ -100.0, 40.0 ],
              [ -100.0, 45.0 ],
              [ -110.0, 45.0 ],
              [ -110.0, 40.0 ]
            ]
          ]
        }
      }
    }