Skip to content

Commit 89f70b8

Browse files
committed
Specify "if", "then", and "else"
This addresses #180. I have intentionally allowed for any combination of these keywords to be present or absent. While having a "then" or "else" without "if" is pointless and/or nonsensical, so are many other possible JSON Schema keywords.
1 parent 0b67e5c commit 89f70b8

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

hyper-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
"properties": {
9191
"additionalProperties": { "$ref": "#" }
9292
},
93+
"if": {"$ref": "#"},
94+
"then": {"$ref": "#"},
95+
"else": {"$ref": "#"},
9396
"allOf": { "$ref": "#/definitions/schemaArray" },
9497
"anyOf": { "$ref": "#/definitions/schemaArray" },
9598
"oneOf": { "$ref": "#/definitions/schemaArray" },

jsonschema-hyperschema.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@
214214
<t>
215215
Hyper-schemas MUST NOT be applied to an instance if the instance fails to
216216
validate against the validation keywords within or containing the hyper-schema.
217-
Hyper-schema keywords in branches of an "anyOf" or "oneOf" that do not validate,
218-
or in a "dependencies" subschema that is not relevant to the instance, MUST be
219-
ignored.
217+
Hyper-schema keywords in branches of an "anyOf", "oneOf", or "if"/"then"/"else"
218+
that do not validate, or in a "dependencies" subschema that is not relevant
219+
to the instance, MUST be ignored.
220220
</t>
221221
<t>
222222
Hyper-schema keywords in a subschema contained within a "not", at any depth,

jsonschema-validation.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,78 @@
635635
</section>
636636
</section>
637637

638+
<section title="Applying subschemas conditionally">
639+
<t>
640+
These keywords work together to implement conditional
641+
application of a subschema based on the outcome of
642+
another subschema.
643+
</t>
644+
<t>
645+
These keywords MUST NOT interact with each other across
646+
subschema boundaries. In other words, an "if" in one
647+
branch of an "allOf" MUST NOT have an impact on a "then"
648+
or "else" in another branch.
649+
</t>
650+
<section title="if">
651+
<t>
652+
This keyword's value MUST be a valid JSON Schema.
653+
</t>
654+
<t>
655+
Instances that successfully validate against this
656+
keyword's subschema MUST also be valid against
657+
the subschema value of the "then" keyword, if
658+
present.
659+
</t>
660+
<t>
661+
Instances that fail to validate against this
662+
keyword's subschema MUST also be valid against
663+
the subschema value of the "else" keyword.
664+
</t>
665+
<t>
666+
Validation of the instance against this keyword
667+
on its own always succeeds, regardless of the
668+
validation outcome of against its subschema.
669+
</t>
670+
</section>
671+
<section title="then">
672+
<t>
673+
This keyword's value MUST be a valid JSON Schema.
674+
</t>
675+
<t>
676+
When present alongside of "if", the instance
677+
successfully validates against this keyword if
678+
it validates against both the "if"'s subschema
679+
and this keyword's subschema.
680+
</t>
681+
<t>
682+
When "if" is absent, or the instance fails to
683+
validate against its subschema, validation against
684+
this keyword always succeeds. Implementations
685+
SHOULD avoid attempting to validate against
686+
the subschema in these cases.
687+
</t>
688+
</section>
689+
<section title="else">
690+
<t>
691+
This keyword's value MUST be a valid JSON Schema.
692+
</t>
693+
<t>
694+
When present alongside of "if", the instance
695+
successfully validates against this keyword if
696+
it fails to validate against the "if"'s
697+
subschema, and successfully validates against
698+
this keyword's subschema.
699+
</t>
700+
<t>
701+
When "if" is absent, or the instance successfully
702+
validates against its subschema, validation against
703+
this keyword always succeeds. Implementations
704+
SHOULD avoid attempting to validate against
705+
the subschema in these cases.
706+
</t>
707+
</section>
708+
</section>
709+
638710
<section title="Keywords for applying subschemas with boolean logic">
639711
<section title="allOf">
640712
<t>

schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
]
142142
},
143143
"format": { "type": "string" },
144+
"if": {"$ref": "#"},
145+
"then": {"$ref": "#"},
146+
"else": {"$ref": "#"},
144147
"allOf": { "$ref": "#/definitions/schemaArray" },
145148
"anyOf": { "$ref": "#/definitions/schemaArray" },
146149
"oneOf": { "$ref": "#/definitions/schemaArray" },

0 commit comments

Comments
 (0)