8000 Fix enum strict JSON validation when validators are present by Viicos · Pull Request #1632 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]

Skip to content

Fix enum strict JSON validation when validators are present #1632

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

Merged
merged 1 commit into from
Feb 12, 2025
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
4 changes: 2 additions & 2 deletions src/validators/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pyo3::types::{PyDict, PyFloat, PyInt, PyList, PyString, PyType};

use crate::build_tools::{is_strict, py_schema_err};
use crate::errors::{ErrorType, ValError, ValResult};
use crate::input::Input;
use crate::input::{Input, InputType};
use crate::tools::{safe_repr, SchemaDict};

use super::is_instance::class_repr;
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> {
return Ok(exact_py_input.clone().unbind());
}
let strict = state.strict_or(self.strict);
if strict && input.as_python().is_some() {
if strict && state.extra().input_type == InputType::Python {
// TODO what about instances of subclasses?
return Err(ValError::new(
ErrorType::IsInstanceOf {
Expand Down
17 changes: 17 additions & 0 deletions tests/validators/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,20 @@ def __new__(cls, species: str, sound: str):
assert v.validate_python('meow') is Animal.CAT
assert v.validate_python('dog') is Animal.DOG
assert v.validate_python('woof') is Animal.DOG


def test_strict_enum_wrap_json() -> None:
"""https://github.com/pydantic/pydantic/issues/11070"""

class Animal(str, Enum):
CAT = 'cat'
DOG = 'dog'

schema = core_schema.no_info_wrap_validator_function(
lambda v, handler: handler(v),
core_schema.enum_schema(Animal, list(Animal.__members__.values()), sub_type='str', strict=True),
)
v = SchemaValidator(schema)

assert v.validate_python(Animal.CAT) == Animal.CAT
assert v.validate_json('"dog"') == Animal.DOG
Loading
0