Migrating from Python jsonschema. Guidance sought #932
Unanswered
jpgoldberg
asked this question in
Q&A
Replies: 1 comment 1 reply
-
|
Hi @jpgoldberg I'll write a proper migration guide, but here are the main differences: Order of arguments: try:
jsonschema_rs.validate({"type": "object"}, {"name": "John"}) # Note: schema first, instance second
except ValidationError as e:
print(e.message)Custom format checks: # jsonschema
from jsonschema import Draft7Validator, FormatChecker
format_checker = FormatChecker()
@format_checker.checks("custom-format")
def check_custom(value):
return value.startswith("custom-")
validator = Draft7Validator(schema, format_checker=format_checker)
# jsonschema-rs
from jsonschema_rs import Draft7Validator
def check_custom(value: str) -> bool:
return value.startswith("custom-")
validator = Draft7Validator(
schema,
formats={"custom-format": check_custom},
validate_formats=True,
)
# jsonschema
error.message # Error message
error.path # deque(['field', 0])
error.schema_path # deque(['properties', 'field', 'type'])
error.validator # 'type'
error.validator_value # 'string'
# jsonschema-rs
error.message # Error message
error.instance_path # ['field', 0]
error.schema_path # ['properties', 'field', 'type']
error.kind # ValidationErrorKind.Type(types=['string'])
error.instance # The actual invalid valueSchema validation: # jsonschema
from jsonschema import Draft7Validator
Draft7Validator.check_schema(schema) # Raises SchemaError if invalid
# jsonschema-rs
import jsonschema_rs
jsonschema_rs.meta.validate(schema) # Raises ValidationError if invalidSchema validity is also checked during validator creation process.
let me know what aspects are specifically interesting for you, I'll answer here and later will include it into the migration guide. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am a little lost looking for the documentation for the Python bindings, and in particular, I would appreciate some guidance on moving to this from the Python jsonschema library.
Beta Was this translation helpful? Give feedback.
All reactions