Skip to content

Commit f8c0920

Browse files
Fix bug allowing validation of bool types with coerce_numbers_to_str=True (#1017)
1 parent 1b7b6e9 commit f8c0920

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/input/input_python.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ impl<'a> Input<'a> for PyAny {
246246
Err(_) => return Err(ValError::new(ErrorTypeDefaults::StringUnicode, self)),
247247
};
248248
Ok(s.into())
249-
} else if coerce_numbers_to_str && {
249+
} else if coerce_numbers_to_str && !PyBool::is_exact_type_of(self) && {
250250
let py = self.py();
251251
let decimal_type: Py<PyType> = get_decimal_type(py);
252252

253+
// only allow int, float, and decimal (not bool)
253254
self.is_instance_of::<PyInt>()
254255
|| self.is_instance_of::<PyFloat>()
255256
|| self.is_instance(decimal_type.as_ref(py)).unwrap_or_default()

tests/validators/test_string.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ def test_coerce_numbers_to_str_disabled_in_strict_mode() -> None:
277277
v.validate_json('42')
278278

279279

280+
def test_coerce_numbers_to_str_raises_for_bool() -> None:
281+
config = core_schema.CoreConfig(coerce_numbers_to_str=True)
282+
283+
v = SchemaValidator(core_schema.str_schema(), config)
284+
with pytest.raises(ValidationError):
285+
v.validate_python(True)
286+
with pytest.raises(ValidationError):
287+
v.validate_json(False)
288+
289+
280290
@pytest.mark.parametrize(
281291
('number', 'expected_str'),
282292
[

0 commit comments

Comments
 (0)