Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Force jsonschema to use our validator #118

Closed

Conversation

jpmckinney
Copy link
Contributor

@jpmckinney jpmckinney commented Mar 16, 2023

Otherwise, jsonschema switches back to the default Draft4Validator: python-jsonschema/jsonschema#994 This code seems to be the recommended solution, per that issue.

There are some whitespace changes because black was complaining about the lint workflow.


I also tried the following patch, but extend, doesn't seem to behave as documented. (The validators argument doesn't cause the validators to be overridden for $referenced schema.)

Note that, even if the below diff were to work, some lib-cove-* libraries (like lib-cove-ocds) override validator.VALIDATORS directly (which is not part of jsonschema's public API).

Allowing such overrides while following jsonschema's public API would require more significant refactoring in lib-cove.

diff --git a/libcove/lib/common.py b/libcove/lib/common.py
index f5c508b..6568b2f 100644
--- a/libcove/lib/common.py
+++ b/libcove/lib/common.py
@@ -92,17 +92,6 @@ class TypeChecker:
         raise UndefinedTypeCheck(type)
 
 
-# Because we will be changing items on this validator, it's important we take a copy!
-# Otherwise we could cause conflicts with other software in the same process.
-validator = jsonschema.validators.extend(
-    jsonschema.validators.Draft4Validator,
-    validators={
-        "type": type_validator,
-    },
-    type_checker=TypeChecker(),
-)
-
-uniqueItemsValidator = validator.VALIDATORS.pop("uniqueItems")
 LANGUAGE_RE = re.compile(
     "^(.*_(((([A-Za-z]{2,3}(-([A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-([A-Za-z]{4}))?(-([A-Za-z]{2}|[0-9]{3}))?(-([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-([0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(x(-[A-Za-z0-9]{1,8})+))?)|(x(-[A-Za-z0-9]{1,8})+)))$"
 )
@@ -345,13 +334,23 @@ def dependencies_extra_data(validator, dependencies, instance, schema):
                 yield error
 
 
-validator.VALIDATORS.pop("patternProperties")
-validator.VALIDATORS["uniqueItems"] = unique_ids
-validator.VALIDATORS["required"] = required_draft4
-validator.VALIDATORS["oneOf"] = oneOf_draft4
-validator.VALIDATORS["dependencies"] = dependencies_extra_data
-validator.VALIDATORS["additionalItems"] = additionalItems_extra_data
-validator.VALIDATORS["additionalProperties"] = additionalProperties_extra_data
+
+# Because we will be changing items on this validator, it's important we take a copy!
+# Otherwise we could cause conflicts with other software in the same process.
+validator = jsonschema.validators.extend(
+    jsonschema.validators.Draft4Validator,
+    validators={
+        "type": type_validator,
+        "required": required_draft4,
+        "oneOf": oneOf_draft4,
+        "dependencies": dependencies_extra_data,
+        "additionalItems": additionalItems_extra_data,
+        "additionalProperties": additionalProperties_extra_data,
+        "patternProperties": lambda *args, **kwargs: None,
+        "uniqueItems": lambda *args, **kwargs: None,
+    },
+    type_checker=TypeChecker(),
+)
 
 
 # Properties this class might look for

Bjwebb added a commit that referenced this pull request Jul 6, 2023
#118

Ben has edited the changelog only.

Co-authored-by: James McKinney <26463+jpmckinney@users.noreply.github.com>
Co-authored-by: Ben Webb <ben.webb@opendataservices.coop>
@Bjwebb
Copy link
Member

Bjwebb commented Jul 7, 2023

Merged in #122

@Bjwebb Bjwebb closed this Jul 7, 2023
@jpmckinney jpmckinney deleted the jsonschema4-fix-alt branch July 11, 2023 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants