Skip to content

Commit f3d4098

Browse files
authored
Merge pull request #771 from handrews/type
Example of assertion independence per awwright
2 parents d6f1e32 + 4b55933 commit f3d4098

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
@@ -760,11 +760,38 @@
760760
to the assertion.
761761
</t>
762762
<t>
763-
For example, the "maxLength" keyword from the companion validation
764-
vocabulary will only restrict certain strings
763+
For example, the "maxLength" keyword from the companion
764+
<xref target="json-schema-validation">validation vocabulary</xref>:
765+
will only restrict certain strings
765766
(that are too long) from being valid. If the instance is a number,
766767
boolean, null, array, or object, then it is valid against this assertion.
767768
</t>
769+
<t>
770+
This behavior allows keywords to be used more easily with instances
771+
that can be of multiple primitive types. The companion validation
772+
vocabulary also includes a "type" keyword which can independently
773+
restrict the instance to one or more primitive types. This allows
774+
for a concise expression of use cases such as a function that might
775+
return either a string of a certain length or a null value:
776+
</t>
777+
<figure>
778+
<artwork>
779+
<![CDATA[
780+
{
781+
"type": ["string", "null"],
782+
"maxLength": 255
783+
}
784+
]]>
785+
</artwork>
786+
<postamble>
787+
If "maxLength" also restricted the instance type to be a string,
788+
then this would be substantially more cumbersome to express because
789+
the example as written would not actually allow null values.
790+
Each keyword is evaluated separately unless explicitly specified
791+
otherwise, so if "maxLength" restricted the instance to strings,
792+
then including "null" in "type" would not have any useful effect.
793+
</postamble>
794+
</figure>
768795
</section>
769796
</section>
770797

0 commit comments

Comments
 (0)