Skip to content

Commit

Permalink
fix(python): Panic on initializing a validator
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Oct 25, 2024
1 parent d127315 commit 5490587
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
8 changes: 8 additions & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Changed

- Schema representation inside `__repr__`.

### Fixed

- Panic on initializing a validator. [#618](https://github.com/Stranger6667/jsonschema-rs/issues/618)

## [0.25.0] - 2024-10-24

**Important:** This release removes deprecated old APIs. See the [Migration Guide](MIGRATION.md) for details on transitioning to the new API.
Expand Down
35 changes: 9 additions & 26 deletions crates/jsonschema-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,6 @@ fn iter_errors(
}
}

const SCHEMA_LENGTH_LIMIT: usize = 32;

fn get_schema_repr(schema: &serde_json::Value) -> String {
// It could be more efficient, without converting the whole Value to a string
let mut repr = schema.to_string();
if repr.len() > SCHEMA_LENGTH_LIMIT {
repr.truncate(SCHEMA_LENGTH_LIMIT);
repr.push_str("...}");
}
repr
}

fn handle_format_checked_panic(err: Box<dyn Any + Send>) -> PyErr {
LAST_FORMAT_ERROR.with(|last| {
if let Some(err) = last.borrow_mut().take() {
Expand All @@ -383,7 +371,6 @@ fn handle_format_checked_panic(err: Box<dyn Any + Send>) -> PyErr {
#[pyclass(module = "jsonschema_rs", subclass)]
struct Validator {
validator: jsonschema::Validator,
repr: String,
}

/// validator_for(schema, formats=None, validate_formats=None, ignore_unknown_formats=True)
Expand Down Expand Up @@ -434,10 +421,7 @@ fn validator_for_impl(
};
let options = make_options(draft, formats, validate_formats, ignore_unknown_formats)?;
match options.build(&schema) {
Ok(validator) => Ok(Validator {
validator,
repr: get_schema_repr(&schema),
}),
Ok(validator) => Ok(Validator { validator }),
Err(error) => Err(into_py_err(py, error)?),
}
}
Expand Down Expand Up @@ -506,16 +490,15 @@ impl Validator {
) -> PyResult<ValidationErrorIter> {
iter_on_error(py, &self.validator, instance)
}
fn __repr__(&self) -> String {
let draft = match self.validator.draft() {
Draft::Draft4 => "Draft4",
Draft::Draft6 => "Draft6",
Draft::Draft7 => "Draft7",
Draft::Draft201909 => "Draft201909",
Draft::Draft202012 => "Draft202012",
fn __repr__(&self) -> &'static str {
match self.validator.draft() {
Draft::Draft4 => "<Draft4Validator>",
Draft::Draft6 => "<Draft6Validator>",
Draft::Draft7 => "<Draft7Validator>",
Draft::Draft201909 => "<Draft201909Validator>",
Draft::Draft202012 => "<Draft202012Validator>",
_ => "Unknown",
};
format!("<{draft}Validator: {}>", self.repr)
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/jsonschema-py/tests-py/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_invalid_type(func):


def test_repr():
assert repr(validator_for({"minimum": 5})) == '<Draft202012Validator: {"minimum":5}>'
assert repr(validator_for({"minimum": 5})) == "<Draft202012Validator>"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -364,3 +364,8 @@ def test_ignore_unknown_formats(cls, ignore_unknown_formats, should_raise):
else:
validator = cls(unknown_format_schema, ignore_unknown_formats=ignore_unknown_formats)
assert validator.is_valid("any string")


def test_unicode_pattern():
validator = Draft202012Validator({"pattern": "aaaaaaaèaaéaaaaéè"})
assert not validator.is_valid("a")

0 comments on commit 5490587

Please sign in to comment.