Skip to content

Commit

Permalink
Do not return error if coerce_numbers_to_str enabled and string has i…
Browse files Browse the repository at this point in the history
…nvalid unicode character
  • Loading branch information
aberenda-optifino committed Oct 31, 2024
1 parent 89bdee1 commit 9822171
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/validators/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,14 @@ impl StrConstrainedValidator {
}

// whether any of the constraints/customisations are actually enabled
// except strict which can be set on StrValidator
// except strict and coerce_numbers_to_str which can be set on StrValidator
fn has_constraints_set(&self) -> bool {
self.pattern.is_some()
|| self.max_length.is_some()
|| self.min_length.is_some()
|| self.strip_whitespace
|| self.to_lower
|| self.to_upper
|| self.coerce_numbers_to_str
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/validators/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ class StrSubclass(str):
assert not isinstance(v.validate_python(StrSubclass(''), strict=True), StrSubclass)


@pytest.mark.parametrize('string', [True, False])
def test_coerce_numbers_to_str_with_invalid_unicode_character(string) -> None:
config = core_schema.CoreConfig(coerce_numbers_to_str=True)

v = SchemaValidator(core_schema.str_schema(strict=string), config)
assert v.validate_python('\ud835') == '\ud835'


def test_coerce_numbers_to_str_disabled_in_strict_mode() -> None:
config = core_schema.CoreConfig(coerce_numbers_to_str=True)

Expand Down

0 comments on commit 9822171

Please sign in to comment.