Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json schema property aliasing

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?

like image 545
Adam Matan Avatar asked Feb 23 '14 15:02

Adam Matan


People also ask

What does $Ref mean in JSON Schema?

$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.

What is $id in JSON Schema?

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.

Does order matter in JSON Schema?

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.

Can a JSON file reference another JSON file?

JSON Reference allows a JSON value to reference another value in a JSON document. This module implements utilities for exploring these objects.


1 Answers

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. :)

like image 89
cloudfeet Avatar answered Sep 18 '22 17:09

cloudfeet