comparison data_schema/__init__.py @ 31:271ec3abdfa3

Use "$type" as the schema's type specifier instead of "type". THIS IS BACKWARDS INCOMPATIBLE.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Tue, 18 Jul 2023 14:28:52 +0200
parents 2e7c08c356ee
children 9a3da5a008fc
comparison
equal deleted inserted replaced
30:2e7c08c356ee 31:271ec3abdfa3
538 schema = process_schema_references( 538 schema = process_schema_references(
539 schema, context, check_single_ref_key=not is_root) 539 schema, context, check_single_ref_key=not is_root)
540 540
541 # 3. Real validation 541 # 3. Real validation
542 542
543 # check combinator shortcuts without "type" indirection 543 # check combinator shortcuts without "$type" indirection
544 combinator, combinator_schema = _get_one_of( 544 combinator, combinator_schema = _get_one_of(
545 schema, "not", "all-of", "any-of", "one-of") 545 schema, "not", "all-of", "any-of", "one-of")
546 if combinator is None: 546 if combinator is None:
547 try: 547 try:
548 t = schema["type"] 548 t = schema["$type"]
549 except KeyError: 549 except KeyError:
550 raise SchemaError("Schema has no `type' key: {!r}." 550 raise SchemaError("Schema has no `$type' key: {!r}."
551 " Context: {!s}".format(schema, context)) 551 " Context: {!s}".format(schema, context))
552 else: 552 else:
553 # 553 #
554 # Construct a temporary schema with the proper indirection for 554 # Construct a temporary schema with the proper indirection for
555 # the check below 555 # the check below
556 # 556 #
557 t = {"type": {combinator: combinator_schema}} 557 t = {"$type": {combinator: combinator_schema}}
558 if combinator_schema is None: 558 if combinator_schema is None:
559 raise SchemaError("a combinator requires a child") 559 raise SchemaError("a combinator requires a child")
560 if callable(t): 560 if callable(t):
561 yield from t(obj, schema, context) 561 yield from t(obj, schema, context)
562 elif t is None: 562 elif t is None:
756 if len(obj) > maxlen: 756 if len(obj) > maxlen:
757 yield ValidationProblem(code=ERRORS.E10013, hint=obj, context=context) 757 yield ValidationProblem(code=ERRORS.E10013, hint=obj, context=context)
758 try: 758 try:
759 schema_items = schema.ensure_child_schema(schema["items"]) 759 schema_items = schema.ensure_child_schema(schema["items"])
760 except KeyError: 760 except KeyError:
761 schema_items = _Schema(schema, False, {"type": validate_deny}) 761 schema_items = _Schema(schema, False, {"$type": validate_deny})
762 for idx, o in enumerate(obj): 762 for idx, o in enumerate(obj):
763 new_context = Context(parent=context, index=idx, current_object=o) 763 new_context = Context(parent=context, index=idx, current_object=o)
764 yield from _validate(o, schema_items, new_context) 764 yield from _validate(o, schema_items, new_context)
765 765
766 766
780 if len(obj) > maxlen: 780 if len(obj) > maxlen:
781 yield ValidationProblem(code=ERRORS.E10040, hint=obj, context=context) 781 yield ValidationProblem(code=ERRORS.E10040, hint=obj, context=context)
782 try: 782 try:
783 schema_items = schema.ensure_child_schema(schema["items"]) 783 schema_items = schema.ensure_child_schema(schema["items"])
784 except KeyError: 784 except KeyError:
785 schema_items = _Schema(schema, False, {"type": validate_deny}) 785 schema_items = _Schema(schema, False, {"$type": validate_deny})
786 for o in obj: 786 for o in obj:
787 new_context = Context(parent=context, key=o, current_object=o) 787 new_context = Context(parent=context, key=o, current_object=o)
788 yield from _validate(o, schema_items, new_context) 788 yield from _validate(o, schema_items, new_context)
789 789
790 790