-
Notifications
You must be signed in to change notification settings - Fork 254
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
Passing data to default_factory
in with_default_schema
#1582
Comments
The data that is passed to the |
Thanks for the quick response: I tried but it still says
The example now looks like this: from pydantic_core import SchemaValidator, core_schema
from typing import Any
from pydantic import BaseModel, Field
class Item(BaseModel):
value: float = Field(ge=0)
weight: float = Field(ge=0)
def error_handler(context: dict[str, Any]) -> Item:
import pdb
pdb.set_trace()
return Item(value=0, weight=0)
# to replace with default value: https://docs.pydantic.dev/latest/api/pydantic_core_schema/#pydantic_core.core_schema.with_default_schema
default_item_schema = core_schema.with_default_schema(
schema=Item.__pydantic_core_schema__,
default_factory=error_handler,
default_factory_takes_data=True,
on_error="default",
)
schema_validator = SchemaValidator(default_item_schema)
class DefaultItem(Item):
__pydantic_core_schema__ = default_item_schema
__pydantic_validator__ = schema_validator
default_item = DefaultItem(value=-4, weight=7) When I go up the stack to see where
This line is in |
@RolandSaur, can you please provide an example of what you're trying to achieve exactly, before explaining it with |
@Viicos Thanks for pointing this out. It helped me structure my thoughts. Immediate problem: Log information when there is a validation error. from pydantic import BaseModel, Field, ValidationError
import logging
logger = logging.getLogger(__name__)
class Item(BaseModel):
value: float = Field(ge=0)
weight: float = Field(ge=0)
def create_item(value: float, weight: float) -> Item:
try:
new_item = Item(value=value, weight=weight)
return new_item
except ValidationError as err:
for error in err.errors():
logger.warning(
f"Error at {error['loc']} with input {error['input']}: {error['msg']}"
)
return Item(value=1.0, weight=1.0)
new_item = create_item(1, -4) But I would like to do the logging directly in the |
Does this example cover your use case? |
I am trying to replace a model with a default model in case validation fails. But I would like to customize the default model based on the error message.
For this I am using with_default_schema and am trying to pass data to
default_factory
.My Question: How to pass the validation error to the
default_factory
ofwith_default_schema
?Minimal Example:
The Minimal Example validates an
Item
and callserror_handler
because of a validation error. This part works. However from the docs it is not clear how to pass anything to theerror_handler
.context
is simplyNone
.System:
2.10.3
3.12.3
24.04
The text was updated successfully, but these errors were encountered: