Skip to content

Commit 040082c

Browse files
committed
Control link applicability with hrefRequired
This addresses issue json-schema-org#49. Add a keyword, "hrefRequired", that prevents a link from being used if a variable named in that keyword's array cannot be resolved to a value by any available mechanism. Remove outdated language about non-object instances causing problems, as this is addressed by "hrefPointers".
1 parent 4d74bc8 commit 040082c

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

hyper-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"description": "a schema for validating user input to the URI template, where the input is in the form of a JSON object with property names matching variable names in \"href\"",
2929
"allOf": [ {"$ref": "#"} ]
3030
},
31+
"hrefRequired": {
32+
"description": "an array of URI template variables names from \"href\" which must be filled out with some value, either from the instance or user input"
33+
},
3134
"rel": {
3235
"description": "relation to the target resource of the link",
3336
"type": "string"

jsonschema-hyperschema.xml

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,6 @@
183183
binary data treated as a PNG image.
184184
It also defines link relations for the instance, with URIs incorporating values
185185
from the instance.
186-
<cref>
187-
"id" probably should not normally be a required keyword, since new instances
188-
will have an unknown "id" property until is it assigned by the server.
189-
However, this property is used in a link, and without it, multiple different
190-
instances would be given the same rel=self URI!
191-
</cref>
192186
</postamble>
193187
</figure>
194188

@@ -644,18 +638,23 @@
644638

645639
<section title="Missing values" anchor="missingValues">
646640
<t>
647-
Sometimes, the appropriate values will not be available.
648-
For example, the template might specify the use of object properties,
649-
but no such data was provided (or "hrefSchema" is not present), and the
650-
instance is an array or a string.
641+
Sometimes, the appropriate values will not be available. In many
642+
cases, the URI Template behavior of simply removing varibles that
643+
do not have a value will be appropriate. An example of this is
644+
optional query parameters, the presence or absence of which does
645+
not change the nature of the link relation type.
651646
</t>
652647

653648
<t>
654-
If any of the values required for the template are neither present in
655-
the user agent data (if relevant) nor the JSON instance, then substitute
656-
values MAY be provided from another source (such as default values).
657-
Otherwise, the link definition SHOULD be considered not to apply to the
658-
instance.
649+
However, some variables, such as an identifier used in a path component,
650+
cannot meaningfully be omitted. The resulting URI would be meaningless,
651+
or would require a different link relation type. While "hrefSchema" can
652+
express a requirement for those variables that can be supplied via input,
653+
some variables must be resolved from instance data. When that instance
654+
data is not required by the context schema, the
655+
<xref target="hrefRequired">"hrefRequired</xref> keyword may be used to
656+
indicate that when the instance data is not available, the link does
657+
not apply.
659658
</t>
660659
</section>
661660
</section>
@@ -709,7 +708,8 @@
709708
"hrefPointers": {
710709
"rootId": "/id",
711710
"parentId": "2/id"
712-
}
711+
},
712+
"hrefRequired": ["parentId"]
713713
}
714714
]
715715
}]]>
@@ -761,14 +761,8 @@
761761
</t>
762762
<t>
763763
For the root node, the relative pointer for the parent doesn't point
764-
to anything. As noted under
765-
<xref target="missingValues">missing values</xref>, such a link should
766-
simply be ignored.
767-
<cref>
768-
GitHub issue #49 tracks the question of distinguishing this situation
769-
of a missing required variable (common in path components) from
770-
missing optional variables (common in query string parameters).
771-
</cref>
764+
to anything, so <xref target="hrefRequired">"hrefRequired"</xref>
765+
prevents the link from being used with that node.
772766
</t>
773767
</section>
774768

@@ -863,6 +857,37 @@
863857
</t>
864858
</section>
865859

860+
<section title="hrefRequired" anchor="hrefRequired">
861+
<t>
862+
The value of this keyword MUST be an array, and the elements MUST be unique.
863+
Each element SHOULD match a variable in the link's URI Template, without
864+
percent-encoding. After completing the entire URI Template resolution
865+
process, if any variable that is present in this array does not have
866+
a value, the link MUST NOT be used.
867+
</t>
868+
<figure>
869+
<preamble>
870+
Here is a simplified version of the "up" link from the
871+
<xref target="hrefPointers">"hrefPointers</xref> tree example,
872+
modified to only use the parent identifier for its "href" template.
873+
While each individual node is required to have an "id" field, the
874+
"up" link uses the parent node's field, and the root node, by definition,
875+
does not have a parent node. Putting "parentId" in "hrefRequired" ensures
876+
that the "up" link is correctly unusable with the root node.
877+
</preamble>
878+
<artwork>
879+
<![CDATA[{
880+
"rel": "up",
881+
"href": "/nodes/{parentId}",
882+
"hrefPointers": {
883+
"parentId": "2/id"
884+
},
885+
"hrefRequired": ["parentId"]
886+
}]]>
887+
</artwork>
888+
</figure>
889+
</section>
890+
866891
<section title="rel" anchor="rel">
867892
<t>
868893
The value of the "rel" property indicates the name of the relation to the target

links.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"description": "a schema for validating user input to the URI template, where the input is in the form of a JSON object with property names matching variable names in \"href\"",
1818
"allOf": [ {"$ref": "#"} ]
1919
},
20+
"hrefRequired": {
21+
"description": "an array of URI template variables names from \"href\" which must be filled out with some value, either from the instance or user input"
22+
},
2023
"rel": {
2124
"description": "relation to the target resource of the link",
2225
"type": "string"

0 commit comments

Comments
 (0)