-
Notifications
You must be signed in to change notification settings - Fork 12
Schema references
Mihael Konjevic edited this page Jun 1, 2014
·
1 revision
JSON schema allow referencing another schema (or a part of it). You can do it by using the $ref
attribute. APItizer requires that you add all the schema before you can reference it.
You can reference schemas in these ways:
- reference data from the same schema:
#path/to/attribute
- reference another schema:
schemaName
- reference path of another schema:
schemaName#path/to/attribute
In the following example, we're referencing data in the same schema:
{
type: "object",
definitions: {
address: {
type: "object",
properties: {
street_address: {
"type": "string"
},
city: {
"type": "string"
},
state: {
"type": "string"
},
phoneNumbers: {
"type": "array",
items: {
type: "integer",
minimum: 1000000,
maximum: 9999999
}
},
country: {
oneOf: [{
type: "object",
properties: {
postal: {
type: "integer",
minimum: 100,
maximum: 999
},
name: {
type: "string"
}
}
}, {
type: "string"
}]
}
},
required: ["street_address", "city", "state"]
}
},
properties: {
billing_address: {
"$ref": "#/definitions/address"
},
shipping_address: {
allOf: [{
$ref: "#/definitions/address"
}, {
properties: {
type: {
enum: ["residential", "business"]
}
},
required: ["type"]
}]
}
}
}
--
Here we have a main schema that holds the definitions and we reference them from the another schema:
var defSchema : {
type: "object",
definitions: {
address: {
type: "object",
properties: {
street_address: {
"type": "string"
},
city: {
"type": "string"
},
state: {
"type": "string"
}
}
}
}
},
addressSchema : {
properties: {
billing_address: {
"$ref": "definitionsSchema#/definitions/address"
}
}
}
apitizer.addSchema('definitionsSchema', defSchema); // we use the `definitionsSchema` name in the `addressSchema`
apitizer.addSchema('address', addressSchema);