comparison data_schema/__init__.py @ 19:c3a0fe8d4587

Consistent casing of schema items: all lowercase with dash as separator
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Jul 2023 10:05:48 +0200
parents 65f937fb8de7
children 413b344be2d1
comparison
equal deleted inserted replaced
18:94daf3381338 19:c3a0fe8d4587
75 E10016 = NC_("schema-msg", "max tuple length exceeded") 75 E10016 = NC_("schema-msg", "max tuple length exceeded")
76 E10017 = NC_("schema-msg", "additional items in tuple not allowed") 76 E10017 = NC_("schema-msg", "additional items in tuple not allowed")
77 E10018 = NC_("schema-msg", "object is not empty") 77 E10018 = NC_("schema-msg", "object is not empty")
78 E10019 = NC_("schema-msg", "more than one match in `one-of' detected") 78 E10019 = NC_("schema-msg", "more than one match in `one-of' detected")
79 E10020 = NC_("schema-msg", "int expected") 79 E10020 = NC_("schema-msg", "int expected")
80 E10021 = NC_("schema-msg", "int value lower than minValue") 80 E10021 = NC_("schema-msg", "int value lower than min-value")
81 E10022 = NC_("schema-msg", "int value greater than maxValue") 81 E10022 = NC_("schema-msg", "int value greater than max-value")
82 E10023 = NC_("schema-msg", "float expected") 82 E10023 = NC_("schema-msg", "float expected")
83 E10024 = NC_("schema-msg", "float value lower than minValue") 83 E10024 = NC_("schema-msg", "float value lower than min-value")
84 E10025 = NC_("schema-msg", "float value greater than maxValue") 84 E10025 = NC_("schema-msg", "float value greater than max-value")
85 E10026 = NC_("schema-msg", "boolean value expected") 85 E10026 = NC_("schema-msg", "boolean value expected")
86 E10027 = NC_("schema-msg", "boolean true expected") 86 E10027 = NC_("schema-msg", "boolean true expected")
87 E10028 = NC_("schema-msg", "boolean false expected") 87 E10028 = NC_("schema-msg", "boolean false expected")
88 E10029 = NC_("schema-msg", "`not' expected problems but got none") 88 E10029 = NC_("schema-msg", "`not' expected problems but got none")
89 E10030 = NC_("schema-msg", "numeric type (int or float) expected") 89 E10030 = NC_("schema-msg", "numeric type (int or float) expected")
90 E10031 = NC_("schema-msg", "numeric value lower than minValue") 90 E10031 = NC_("schema-msg", "numeric value lower than min-value")
91 E10032 = NC_("schema-msg", "numeric value greater than maxValue") 91 E10032 = NC_("schema-msg", "numeric value greater than max-value")
92 E10033 = NC_("schema-msg", "a plain scalar value expected") 92 E10033 = NC_("schema-msg", "a plain scalar value expected")
93 E10034 = NC_("schema-msg", "dict key does not match required schema") 93 E10034 = NC_("schema-msg", "dict key does not match required schema")
94 E10035 = NC_("schema-msg", "binary data expected") 94 E10035 = NC_("schema-msg", "binary data expected")
95 E10036 = NC_("schema-msg", "length of binary data lower than minValue") 95 E10036 = NC_("schema-msg", "length of binary data lower than min-value")
96 E10037 = NC_("schema-msg", "length of binary data exceeds maxValue") 96 E10037 = NC_("schema-msg", "length of binary data exceeds max-value")
97 E10038 = NC_("schema-msg", "a set is expected") 97 E10038 = NC_("schema-msg", "a set is expected")
98 E10039 = NC_("schema-msg", "length of set lower than minLength") 98 E10039 = NC_("schema-msg", "length of set lower than min-length")
99 E10040 = NC_("schema-msg", "length of set greater than maxLength") 99 E10040 = NC_("schema-msg", "length of set greater than max-length")
100 E10041 = NC_("schema-msg", "timestamp expected") 100 E10041 = NC_("schema-msg", "timestamp expected")
101 E10042 = NC_("schema-msg", "value of timestamp does not validate") 101 E10042 = NC_("schema-msg", "value of timestamp does not validate")
102 E10043 = NC_("schema-msg", "enumerated string value expected but not found") 102 E10043 = NC_("schema-msg", "enumerated string value expected but not found")
103 E10044 = NC_("schema-msg", "referenced object doest not exist") 103 E10044 = NC_("schema-msg", "referenced object doest not exist")
104 E10045 = NC_("schema-msg", "key is not contained in referenced object") 104 E10045 = NC_("schema-msg", "key is not contained in referenced object")
666 return 666 return
667 if not isinstance(obj, dict): 667 if not isinstance(obj, dict):
668 yield ValidationProblem(code=ERRORS.E10000, hint="got: {}".format(type(obj).__name__), context=context) 668 yield ValidationProblem(code=ERRORS.E10000, hint="got: {}".format(type(obj).__name__), context=context)
669 return 669 return
670 yield from _validate_index_constraint(obj, schema, context) 670 yield from _validate_index_constraint(obj, schema, context)
671 minlen = schema.get("minLength", None) 671 minlen = schema.get("min-length", None)
672 if minlen: 672 if minlen:
673 if len(obj) < minlen: 673 if len(obj) < minlen:
674 yield ValidationProblem(code=ERRORS.E10050, hint=obj, context=context) 674 yield ValidationProblem(code=ERRORS.E10050, hint=obj, context=context)
675 maxlen = schema.get("maxLength", None) 675 maxlen = schema.get("max-length", None)
676 if maxlen is not None: 676 if maxlen is not None:
677 if len(obj) > maxlen: 677 if len(obj) > maxlen:
678 yield ValidationProblem(code=ERRORS.E10051, hint=obj, context=context) 678 yield ValidationProblem(code=ERRORS.E10051, hint=obj, context=context)
679 schema_keys = schema.get("keys", {}) if schema else {} 679 schema_keys = schema.get("keys", {}) if schema else {}
680 seen_keys = set() 680 seen_keys = set()
681 schema_keynames = schema.get_child("keyNames", None) 681 schema_keynames = schema.get_child("key-names", None)
682 idx = -1 682 idx = -1
683 for key, item in obj.items(): 683 for key, item in obj.items():
684 idx += 1 684 idx += 1
685 if schema_keynames is None: 685 if schema_keynames is None:
686 if not isinstance(key, str): 686 if not isinstance(key, str):
702 if key in schema_keys: 702 if key in schema_keys:
703 new_context = Context(context, key=key, key_index=idx, current_object=item) 703 new_context = Context(context, key=key, key_index=idx, current_object=item)
704 yield from _validate(item, schema.ensure_child_schema(schema_keys[key]), new_context) 704 yield from _validate(item, schema.ensure_child_schema(schema_keys[key]), new_context)
705 else: 705 else:
706 # check whether additional keys are allowed 706 # check whether additional keys are allowed
707 additional_keys = schema.get_child("additionalKeys", False) 707 additional_keys = schema.get_child("additional-keys", False)
708 if isinstance(additional_keys, bool): 708 if isinstance(additional_keys, bool):
709 if not additional_keys: 709 if not additional_keys:
710 if not _is_in_skip_keys(key, context.settings.skip_keys): 710 if not _is_in_skip_keys(key, context.settings.skip_keys):
711 yield ValidationProblem(code=ERRORS.E10004, hint=str(key), context=context) 711 yield ValidationProblem(code=ERRORS.E10004, hint=str(key), context=context)
712 else: 712 else:
729 return 729 return
730 if not isinstance(obj, (list, tuple)): 730 if not isinstance(obj, (list, tuple)):
731 yield ValidationProblem(code=ERRORS.E10001, hint="got: {}".format(type(obj).__name__), context=context) 731 yield ValidationProblem(code=ERRORS.E10001, hint="got: {}".format(type(obj).__name__), context=context)
732 return 732 return
733 yield from _validate_index_constraint(obj, schema, context) 733 yield from _validate_index_constraint(obj, schema, context)
734 minlen = schema.get("minLength", None) 734 minlen = schema.get("min-length", None)
735 if minlen: 735 if minlen:
736 if len(obj) < minlen: 736 if len(obj) < minlen:
737 yield ValidationProblem(code=ERRORS.E10012, hint=obj, context=context) 737 yield ValidationProblem(code=ERRORS.E10012, hint=obj, context=context)
738 maxlen = schema.get("maxLength", None) 738 maxlen = schema.get("max-length", None)
739 if maxlen is not None: 739 if maxlen is not None:
740 if len(obj) > maxlen: 740 if len(obj) > maxlen:
741 yield ValidationProblem(code=ERRORS.E10013, hint=obj, context=context) 741 yield ValidationProblem(code=ERRORS.E10013, hint=obj, context=context)
742 try: 742 try:
743 schema_items = schema.ensure_child_schema(schema["items"]) 743 schema_items = schema.ensure_child_schema(schema["items"])
753 return 753 return
754 if not isinstance(obj, (set, frozenset)): 754 if not isinstance(obj, (set, frozenset)):
755 yield ValidationProblem(code=ERRORS.E10038, hint="got: {}".format(type(obj).__name__), context=context) 755 yield ValidationProblem(code=ERRORS.E10038, hint="got: {}".format(type(obj).__name__), context=context)
756 return 756 return
757 yield from _validate_index_constraint(obj, schema, context) 757 yield from _validate_index_constraint(obj, schema, context)
758 minlen = schema.get("minLength", None) 758 minlen = schema.get("min-length", None)
759 if minlen: 759 if minlen:
760 if len(obj) < minlen: 760 if len(obj) < minlen:
761 yield ValidationProblem(code=ERRORS.E10039, hint=obj, context=context) 761 yield ValidationProblem(code=ERRORS.E10039, hint=obj, context=context)
762 maxlen = schema.get("maxLength", None) 762 maxlen = schema.get("max-length", None)
763 if maxlen is not None: 763 if maxlen is not None:
764 if len(obj) > maxlen: 764 if len(obj) > maxlen:
765 yield ValidationProblem(code=ERRORS.E10040, hint=obj, context=context) 765 yield ValidationProblem(code=ERRORS.E10040, hint=obj, context=context)
766 try: 766 try:
767 schema_items = schema.ensure_child_schema(schema["items"]) 767 schema_items = schema.ensure_child_schema(schema["items"])
777 return 777 return
778 if not isinstance(obj, (list, tuple)): 778 if not isinstance(obj, (list, tuple)):
779 yield ValidationProblem(code=ERRORS.E10014, hint="got: {}".format(type(obj).__name__), context=context) 779 yield ValidationProblem(code=ERRORS.E10014, hint="got: {}".format(type(obj).__name__), context=context)
780 return 780 return
781 yield from _validate_index_constraint(obj, schema, context) 781 yield from _validate_index_constraint(obj, schema, context)
782 minlen = schema.get("minLength", None) 782 minlen = schema.get("min-length", None)
783 if minlen: 783 if minlen:
784 if len(obj) < minlen: 784 if len(obj) < minlen:
785 yield ValidationProblem(code=ERRORS.E10015, hint=obj, context=context) 785 yield ValidationProblem(code=ERRORS.E10015, hint=obj, context=context)
786 maxlen = schema.get("maxLength", None) 786 maxlen = schema.get("max-length", None)
787 if maxlen is not None: 787 if maxlen is not None:
788 if len(obj) > maxlen: 788 if len(obj) > maxlen:
789 yield ValidationProblem(code=ERRORS.E10016, hint=obj, context=context) 789 yield ValidationProblem(code=ERRORS.E10016, hint=obj, context=context)
790 schema_items = schema.get("items", []) 790 schema_items = schema.get("items", [])
791 if not isinstance(schema_items, (list, tuple)): 791 if not isinstance(schema_items, (list, tuple)):
796 break 796 break
797 new_context = Context(parent=context, index=idx, current_object=o) 797 new_context = Context(parent=context, index=idx, current_object=o)
798 try: 798 try:
799 schema_index = schema.ensure_child_schema(schema_items[idx]) 799 schema_index = schema.ensure_child_schema(schema_items[idx])
800 except IndexError: 800 except IndexError:
801 additional_items = schema.get_child("additionalItems", False) 801 additional_items = schema.get_child("additional-items", False)
802 if isinstance(additional_items, bool): 802 if isinstance(additional_items, bool):
803 if not additional_items: 803 if not additional_items:
804 yield ValidationProblem(code=ERRORS.E10017, context=new_context) 804 yield ValidationProblem(code=ERRORS.E10017, context=new_context)
805 else: 805 else:
806 yield from _validate(o, additional_items, new_context) 806 yield from _validate(o, additional_items, new_context)
820 for ev in enumvalues: 820 for ev in enumvalues:
821 if ev == obj: 821 if ev == obj:
822 break 822 break
823 else: 823 else:
824 yield ValidationProblem(code=ERRORS.E10043, hint=obj, context=context) 824 yield ValidationProblem(code=ERRORS.E10043, hint=obj, context=context)
825 minlen = schema.get("minLength", None) 825 minlen = schema.get("min-length", None)
826 if minlen: 826 if minlen:
827 if len(obj) < minlen: 827 if len(obj) < minlen:
828 yield ValidationProblem(code=ERRORS.E10006, hint=obj, context=context) 828 yield ValidationProblem(code=ERRORS.E10006, hint=obj, context=context)
829 maxlen = schema.get("maxLength", None) 829 maxlen = schema.get("max-length", None)
830 if maxlen is not None: 830 if maxlen is not None:
831 if len(obj) > maxlen: 831 if len(obj) > maxlen:
832 yield ValidationProblem(code=ERRORS.E10007, hint=obj, context=context) 832 yield ValidationProblem(code=ERRORS.E10007, hint=obj, context=context)
833 pattern = schema.get("pattern", None) 833 pattern = schema.get("pattern", None)
834 if pattern is not None: 834 if pattern is not None:
863 def validate_binary(obj, schema, context): 863 def validate_binary(obj, schema, context):
864 if not isinstance(obj, (bytes, bytearray)): 864 if not isinstance(obj, (bytes, bytearray)):
865 yield ValidationProblem(code=ERRORS.E10035, hint=obj, context=context) 865 yield ValidationProblem(code=ERRORS.E10035, hint=obj, context=context)
866 else: 866 else:
867 yield from _validate_index_constraint(obj, schema, context) 867 yield from _validate_index_constraint(obj, schema, context)
868 minlen = schema.get("minLength", None) 868 minlen = schema.get("min-length", None)
869 if minlen: 869 if minlen:
870 if len(obj) < minlen: 870 if len(obj) < minlen:
871 yield ValidationProblem(code=ERRORS.E10036, hint=obj, context=context) 871 yield ValidationProblem(code=ERRORS.E10036, hint=obj, context=context)
872 maxlen = schema.get("maxLength", None) 872 maxlen = schema.get("max-length", None)
873 if maxlen is not None: 873 if maxlen is not None:
874 if len(obj) > maxlen: 874 if len(obj) > maxlen:
875 yield ValidationProblem(code=ERRORS.E10037, hint=obj, context=context) 875 yield ValidationProblem(code=ERRORS.E10037, hint=obj, context=context)
876 pattern = schema.get("pattern", None) 876 pattern = schema.get("pattern", None)
877 if pattern is not None: 877 if pattern is not None:
918 return 918 return
919 if not isinstance(obj, int): 919 if not isinstance(obj, int):
920 yield ValidationProblem(code=ERRORS.E10020, hint=obj, context=context) 920 yield ValidationProblem(code=ERRORS.E10020, hint=obj, context=context)
921 else: 921 else:
922 yield from _validate_index_constraint(obj, schema, context) 922 yield from _validate_index_constraint(obj, schema, context)
923 minValue = schema.get("minValue", None) 923 min_value = schema.get("min-value", None)
924 if minValue is not None and obj < minValue: 924 if min_value is not None and obj < min_value:
925 yield ValidationProblem(code=ERRORS.E10021, hint=obj, context=context) 925 yield ValidationProblem(code=ERRORS.E10021, hint=obj, context=context)
926 maxValue = schema.get("maxValue", None) 926 max_value = schema.get("max-value", None)
927 if maxValue is not None and obj > maxValue: 927 if max_value is not None and obj > max_value:
928 yield ValidationProblem(code=ERRORS.E10022, hint=obj, context=context) 928 yield ValidationProblem(code=ERRORS.E10022, hint=obj, context=context)
929 enumvalues = schema.get("enum", None) 929 enumvalues = schema.get("enum", None)
930 if enumvalues is not None: 930 if enumvalues is not None:
931 for ev in enumvalues: 931 for ev in enumvalues:
932 if ev == obj: 932 if ev == obj:
946 return 946 return
947 if not isinstance(obj, float): 947 if not isinstance(obj, float):
948 yield ValidationProblem(code=ERRORS.E10023, hint=obj, context=context) 948 yield ValidationProblem(code=ERRORS.E10023, hint=obj, context=context)
949 else: 949 else:
950 yield from _validate_index_constraint(obj, schema, context) 950 yield from _validate_index_constraint(obj, schema, context)
951 minValue = schema.get("minValue", None) 951 min_value = schema.get("min-value", None)
952 if minValue is not None and obj < minValue: 952 if min_value is not None and obj < min_value:
953 yield ValidationProblem(code=ERRORS.E10024, hint=obj, context=context) 953 yield ValidationProblem(code=ERRORS.E10024, hint=obj, context=context)
954 maxValue = schema.get("maxValue", None) 954 max_value = schema.get("max-value", None)
955 if maxValue is not None and obj > maxValue: 955 if max_value is not None and obj > max_value:
956 yield ValidationProblem(code=ERRORS.E10025, hint=obj, context=context) 956 yield ValidationProblem(code=ERRORS.E10025, hint=obj, context=context)
957 value = schema.get("value", None) 957 value = schema.get("value", None)
958 if value is not None: 958 if value is not None:
959 if callable(value): 959 if callable(value):
960 yield from value(obj, schema, context) 960 yield from value(obj, schema, context)
967 return 967 return
968 if not isinstance(obj, (int, float)): 968 if not isinstance(obj, (int, float)):
969 yield ValidationProblem(code=ERRORS.E10030, hint=obj, context=context) 969 yield ValidationProblem(code=ERRORS.E10030, hint=obj, context=context)
970 else: 970 else:
971 yield from _validate_index_constraint(obj, schema, context) 971 yield from _validate_index_constraint(obj, schema, context)
972 minValue = schema.get("minValue", None) 972 min_value = schema.get("min-value", None)
973 if minValue is not None and isinstance(obj, float): 973 if min_value is not None and isinstance(obj, float):
974 minValue *= 1.0 974 min_value *= 1.0
975 if minValue is not None and obj < minValue: 975 if min_value is not None and obj < min_value:
976 yield ValidationProblem(code=ERRORS.E10031, hint=obj, context=context) 976 yield ValidationProblem(code=ERRORS.E10031, hint=obj, context=context)
977 maxValue = schema.get("maxValue", None) 977 max_value = schema.get("max-value", None)
978 if maxValue is not None and isinstance(obj, float): 978 if max_value is not None and isinstance(obj, float):
979 maxValue *= 1.0 979 max_value *= 1.0
980 if maxValue is not None and obj > maxValue: 980 if max_value is not None and obj > max_value:
981 yield ValidationProblem(code=ERRORS.E10032, hint=obj, context=context) 981 yield ValidationProblem(code=ERRORS.E10032, hint=obj, context=context)
982 enumvalues = schema.get("enum", None) 982 enumvalues = schema.get("enum", None)
983 if enumvalues is not None: 983 if enumvalues is not None:
984 for ev in enumvalues: 984 for ev in enumvalues:
985 if ev == obj: 985 if ev == obj: