Mercurial > hgrepos > Python > libs > data-schema
comparison data_schema/__init__.py @ 38:5a2fba996773
Implement a "forbidden" schema field for dicts
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Wed, 19 Jul 2023 13:30:55 +0200 |
| parents | ef586378f79a |
| children | 78f5ef0ee087 |
comparison
equal
deleted
inserted
replaced
| 37:ef586378f79a | 38:5a2fba996773 |
|---|---|
| 116 E10054 = NC_("schema-msg", "failing `one-of' item") | 116 E10054 = NC_("schema-msg", "failing `one-of' item") |
| 117 E10055 = NC_("schema-msg", "`any-of' failed") | 117 E10055 = NC_("schema-msg", "`any-of' failed") |
| 118 E10056 = NC_("schema-msg", "failing `any-of' item") | 118 E10056 = NC_("schema-msg", "failing `any-of' item") |
| 119 E10057 = NC_("schema-msg", "`all-of' failed") | 119 E10057 = NC_("schema-msg", "`all-of' failed") |
| 120 E10058 = NC_("schema-msg", "failing `all-of' item") | 120 E10058 = NC_("schema-msg", "failing `all-of' item") |
| 121 E10059 = NC_("schema-msg", "forbidden key detected") | |
| 121 | 122 |
| 122 | 123 |
| 123 @enum.unique | 124 @enum.unique |
| 124 class WARNINGS(enum.Enum): | 125 class WARNINGS(enum.Enum): |
| 125 W80000 = NC_("schema-msg", "duplicate dict key") | 126 W80000 = NC_("schema-msg", "duplicate dict key") |
| 736 except (TypeError, ValueError): | 737 except (TypeError, ValueError): |
| 737 raise SchemaError("`required` must be an iterable") | 738 raise SchemaError("`required` must be an iterable") |
| 738 if not required_keys <= seen_keys: | 739 if not required_keys <= seen_keys: |
| 739 hs = [str(i) for i in required_keys - seen_keys] | 740 hs = [str(i) for i in required_keys - seen_keys] |
| 740 yield ValidationProblem(code=ERRORS.E10005, hint=sorted(hs), context=context) | 741 yield ValidationProblem(code=ERRORS.E10005, hint=sorted(hs), context=context) |
| 742 # check whether no forbidden keys are seen | |
| 743 try: | |
| 744 forbidden_keys = set(schema.get("forbidden", set())) | |
| 745 except (TypeError, ValueError): | |
| 746 raise SchemaError("`forbidden` must be an iterable") | |
| 747 if forbidden_keys & seen_keys: | |
| 748 hs = [str(i) for i in forbidden_keys & seen_keys] | |
| 749 yield ValidationProblem(code=ERRORS.E10059, hint=sorted(hs), context=context) | |
| 741 | 750 |
| 742 | 751 |
| 743 def validate_list(obj, schema, context): | 752 def validate_list(obj, schema, context): |
| 744 if _is_null_allowed_for_object(obj, schema, context): | 753 if _is_null_allowed_for_object(obj, schema, context): |
| 745 return | 754 return |
