Search code examples
jsongeojsonjsonschema

Restrict json schema to Polygon from GeoJson


I'm building a JSON Schema which has a property boundary. I'm referencing the GeoJson schema which works fine. Now I want to restrict my boundary to be of type Polygon, which is an enum from the GeoJson schema.

How to do this?

This is the relevant part of my schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "plot": {
      "type": "object",
      "properties": {
        "boundary": {
          "description": "The boundary of the plot",
          "title": "Plot boundary",
          "additionalProperties": false,
          "required": [
            "type",
            "coordinates",
            "crs"
          ],
          "TODO": "Restrict to (multi)polygons only.",
          "$ref": "http://json.schemastore.org/geojson"
        }      
      }
    }
  }
}

This is my validating json:

{
  "plot":
  {
    "boundary": {
      "crs": {
        "type": "name",
        "properties": {
          "name": "EPSG:3857"
        }
      },
      "coordinates": [],
      "type": "MultiPolygon"
    }    
  }
}

Solution

  • It seems I was using the wrong geojson. I changed my code and now it works as expected and the new schemas are draft-7 as well. Here's my updated code:

            "boundary": {
              "title": "The boundary of the plot",
              "anyOf": [
                {
                  "$ref": "http://geojson.org/schema/MultiPolygon.json"
                },
                {
                  "$ref": "http://geojson.org/schema/Polygon.json"
                }
              ],
              "additionalProperties": false
    

    and

                "geoLocation": {
                  "title": "Front door geolocation",
                  "$ref": "http://geojson.org/schema/Point.json",
                  "additionalProperties": false
                },
    

    The JSON could be:

      "boundary":
      {
        "type": "Polygon",
        "coordinates": [
          [
            [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0],
            [100.0, 0.0]
          ]
        ]
      }
    

    and

        "geoLocation": {
          "coordinates": [125.25, 135.255],
          "type": "Point"
        }