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 when coerce_numbers_to_str enabled and string has invalid unicode character #1515

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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