Skip to content

Commit 4b55933

Browse files
committed
Example of assertion independence per awwright
Based on awwright's suggesetion in the slack channel.
1 parent 3c08495 commit 4b55933

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

jsonschema-core.xml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,38 @@
748748
to the assertion.
749749
</t>
750750
<t>
751-
For example, the "maxLength" keyword from the companion validation
752-
vocabulary will only restrict certain strings
751+
For example, the "maxLength" keyword from the companion
752+
<xref target="json-schema-validation">validation vocabulary</xref>:
753+
will only restrict certain strings
753754
(that are too long) from being valid. If the instance is a number,
754755
boolean, null, array, or object, then it is valid against this assertion.
755756
</t>
757+
<t>
758+
This behavior allows keywords to be used more easily with instances
759+
that can be of multiple primitive types. The companion validation
760+
vocabulary also includes a "type" keyword which can independently
761+
restrict the instance to one or more primitive types. This allows
762+
for a concise expression of use cases such as a function that might
763+
return either a string of a certain length or a null value:
764+
</t>
765+
<figure>
766+
<artwork>
767+
<![CDATA[
768+
{
769+
"type": ["string", "null"],
770+
"maxLength": 255
771+
}
772+
]]>
773+
</artwork>
774+
<postamble>
775+
If "maxLength" also restricted the instance type to be a string,
776+
then this would be substantially more cumbersome to express because
777+
the example as written would not actually allow null values.
778+
Each keyword is evaluated separately unless explicitly specified
779+
otherwise, so if "maxLength" restricted the instance to strings,
780+
then including "null" in "type" would not have any useful effect.
781+
</postamble>
782+
</figure>
756783
</section>
757784
</section>
758785

0 commit comments

Comments
 (0)