Skip to content

Commit 001cbe9

Browse files
committed
oneOf
1 parent 29779bf commit 001cbe9

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

jsonschema/_validators.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,34 @@ def anyOf_draft6(validator, anyOf, instance, schema):
487487
)
488488

489489

490+
def oneOf_draft6(validator, oneOf, instance, schema):
491+
subschemas = enumerate(oneOf)
492+
all_errors = []
493+
for index, subschema in subschemas:
494+
if subschema is True: # FIXME: Messages
495+
subschema = {}
496+
elif subschema is False:
497+
subschema = {"not": {}}
498+
errs = list(validator.descend(instance, subschema, schema_path=index))
499+
if not errs:
500+
first_valid = subschema
501+
break
502+
all_errors.extend(errs)
503+
else:
504+
yield ValidationError(
505+
"%r is not valid under any of the given schemas" % (instance,),
506+
context=all_errors,
507+
)
508+
509+
more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)]
510+
if more_valid:
511+
more_valid.append(first_valid)
512+
reprs = ", ".join(repr(schema) for schema in more_valid)
513+
yield ValidationError(
514+
"%r is valid under each of %s" % (instance, reprs)
515+
)
516+
517+
490518
def not_(validator, not_schema, instance, schema):
491519
if validator.is_valid(instance, not_schema):
492520
yield ValidationError(

jsonschema/validators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def extend(validator, validators, version=None):
270270
u"minimum": _validators.minimum_draft6,
271271
u"multipleOf": _validators.multipleOf,
272272
u"not": _validators.not_,
273+
u"oneOf": _validators.oneOf_draft6,
273274
u"properties": _validators.properties,
274275
u"required": _validators.required,
275276
u"type": _validators.type,

0 commit comments

Comments
 (0)