6

The following JSON Schema describes a valid JSON for a lat/lon coordinate:

{
    "title": "coordinates",
    "type": "object",
    "properties": {
        "longitude": {
        "type": "number",
        "minimum": -180,
            "maximum":180,
            "exclusiveMinimum": false,
            "exclusiveMaximum": false
        },
        "latitude": {
        "type": "number",
        "minimum": -180,
            "maximum":180,
            "exclusiveMinimum": false,
            "exclusiveMaximum": false
        }
    },
    "required": ["longitude", "latitude"],
    "additionalProperties":false
}

The required setting sets the latitude property to be mandatory.

Is there a way to define an alias for the latitude key, so that the client can use either latitude or lat - but not neither and not both?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Adam Matan
  • 128,757
  • 147
  • 397
  • 562

2 Answers2

8

For "either one or the other, but not both", you need oneOf:

{
    "oneOf": [
        {"required": ["lat"]},
        {"required": ["latitude"]}
    ]
}

All you need then is to have a common definition for the two properties. :)

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
4

You can make a property name a pattern (found examples here)

{
    "title": "coordinates",
    "type": "object",
    "patternProperties": {
        "/^lat(itude)?$/": {
            "type": "number",
            "minimum": -180,
            "maximum":180,
            "exclusiveMinimum": false,
            "exclusiveMaximum": false
        }
    },
    "additionalProperties":false
}

...but this breaks the required property (it can't seem to handle patterns). Not a great answer but perhaps helpful. :)