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?
$ref. A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's Base URI. When evaluating a $ref , an implementation uses the resolved identifier to retrieve the referenced schema and applies that schema to the instance.
The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against. The "$id" keyword itself is resolved against the base URI that the object as a whole appears in.
The JSON specification clearly states that the order of members in a JSON Object is of no importance; however, in many cases, such an order is important.
JSON Reference allows a JSON value to reference another value in a JSON document. This module implements utilities for exploring these objects.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With