Skip to content

Commit 0a467e9

Browse files
authored
Fix enum strict JSON validation when validators are present (#1632)
1 parent 51bae7d commit 0a467e9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/validators/enum_.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pyo3::types::{PyDict, PyFloat, PyInt, PyList, PyString, PyType};
88

99
use crate::build_tools::{is_strict, py_schema_err};
1010
use crate::errors::{ErrorType, ValError, ValResult};
11-
use crate::input::Input;
11+
use crate::input::{Input, InputType};
1212
use crate::tools::{safe_repr, SchemaDict};
1313

1414
use super::is_instance::class_repr;
@@ -107,7 +107,7 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> {
107107
return Ok(exact_py_input.clone().unbind());
108108
}
109109
let strict = state.strict_or(self.strict);
110-
if strict && input.as_python().is_some() {
110+
if strict && state.extra().input_type == InputType::Python {
111111
// TODO what about instances of subclasses?
112112
return Err(ValError::new(
113113
ErrorType::IsInstanceOf {

tests/validators/test_enums.py

+17
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,20 @@ def __new__(cls, species: str, sound: str):
487487
assert v.validate_python('meow') is Animal.CAT
488488
assert v.validate_python('dog') is Animal.DOG
489489
assert v.validate_python('woof') is Animal.DOG
490+
491+
492+
def test_strict_enum_wrap_json() -> None:
493+
"""https://github.com/pydantic/pydantic/issues/11070"""
494+
495+
class Animal(str, Enum):
496+
CAT = 'cat'
497+
DOG = 'dog'
498+
499+
schema = core_schema.no_info_wrap_validator_function(
500+
lambda v, handler: handler(v),
501+
core_schema.enum_schema(Animal, list(Animal.__members__.values()), sub_type='str', strict=True),
502+
)
503+
v = SchemaValidator(schema)
504+
505+
assert v.validate_python(Animal.CAT) == Animal.CAT
506+
assert v.validate_json('"dog"') == Animal.DOG

0 commit comments

Comments
 (0)