URI Templates
Using URI templates with $vars keyword
URI templates help us dynamically reuse schemas based on the values of current data being validated.
For simplicity, we can say that an URI template contains placeholders that will be replaced by values to get an URI reference that will be used further. Here is an example:
{
"$ref": "http://example.com/{folder}/{file}.json",
"$vars": {
"folder": "user",
"file": "schema"
}
}
Above, we have two placeholders {folder}
and {file}
, which will be replaced with
the corresponding values from the $vars
keyword ({folder}
=> "user"
, {file}
=> "schema"
),
resulting the final URI http://example.com/user/schema.json
.
More details about the structure of uri templates can be found here.
Expansion examples
{
"$ref": "{/var1,var2}",
"$vars": {
"var1": "a",
"var2": "b"
}
}
{
"$ref": "{?var1,var2}",
"$vars": {
"var1": "a",
"var2": "b"
}
}
{
"$ref": "?x=1&y=2{&var1,var2}",
"$vars": {
"var1": "a",
"var2": "b"
}
}
{
"$ref": "{#var1}",
"$vars": {
"var1": "test"
}
}
Modifier examples
{
"$ref": "{var:3}",
"$vars": {
"var": "test"
}
}
{
"$ref": "{?address*}",
"$vars": {
"address": {
"city": "Some city",
"street": "Some street",
"number": 5
}
}
}