|
748 | 748 | to the assertion.
|
749 | 749 | </t>
|
750 | 750 | <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 |
753 | 754 | (that are too long) from being valid. If the instance is a number,
|
754 | 755 | boolean, null, array, or object, then it is valid against this assertion.
|
755 | 756 | </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> |
756 | 783 | </section>
|
757 | 784 | </section>
|
758 | 785 |
|
|
0 commit comments