Skip to content

Commit 9822171

Browse files
Do not return error if coerce_numbers_to_str enabled and string has invalid unicode character
1 parent 89bdee1 commit 9822171

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/validators/string.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,14 @@ impl StrConstrainedValidator {
200200
}
201201

202202
// whether any of the constraints/customisations are actually enabled
203-
// except strict which can be set on StrValidator
203+
// except strict and coerce_numbers_to_str which can be set on StrValidator
204204
fn has_constraints_set(&self) -> bool {
205205
self.pattern.is_some()
206206
|| self.max_length.is_some()
207207
|| self.min_length.is_some()
208208
|| self.strip_whitespace
209209
|| self.to_lower
210210
|| self.to_upper
211-
|| self.coerce_numbers_to_str
212211
}
213212
}
214213

tests/validators/test_string.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ class StrSubclass(str):
286286
assert not isinstance(v.validate_python(StrSubclass(''), strict=True), StrSubclass)
287287

288288

289+
@pytest.mark.parametrize('string', [True, False])
290+
def test_coerce_numbers_to_str_with_invalid_unicode_character(string) -> None:
291+
config = core_schema.CoreConfig(coerce_numbers_to_str=True)
292+
293+
v = SchemaValidator(core_schema.str_schema(strict=string), config)
294+
assert v.validate_python('\ud835') == '\ud835'
295+
296+
289297
def test_coerce_numbers_to_str_disabled_in_strict_mode() -> None:
290298
config = core_schema.CoreConfig(coerce_numbers_to_str=True)
291299

0 commit comments

Comments
 (0)