Skip to main content

Accessing Configuration in Custom Validators

To retrieve and respect the active CoreConfig within functional validators, use the with_info variants of validator schemas and access the config property on the ValidationInfo object.

Accessing Configuration in a Validator

You can access the CoreConfig by defining a validator function that accepts a ValidationInfo argument and using a schema like with_info_after_validator_function.

from typing import Any
from pydantic_core import SchemaValidator, core_schema

def validate_with_config(value: Any, info: core_schema.ValidationInfo) -> Any:
# Access the active configuration
config = info.config

if config and config.get('str_to_upper'):
return str(value).upper()

if info.mode == 'json':
# Logic specific to JSON validation
pass

return value

# Create a schema that uses the validator with info
schema = core_schema.with_info_after_validator_function(
validate_with_config,
core_schema.str_schema()
)

# Pass the config to the SchemaValidator
v = SchemaValidator(
schema,
config=core_schema.CoreConfig(str_to_upper=True)
)

assert v.validate_python('hello') == 'HELLO'

Key Components

  • ValidationInfo: A protocol passed as the second argument to "with-info" validators. It provides access to:
    • config: The CoreConfig dictionary active for the current validation.
    • mode: Either 'python' or 'json', indicating the source of the data.
    • field_name: The name of the field being validated (if applicable).
    • data: A dictionary of already validated fields (useful in typed_dict or model schemas).
    • context: Arbitrary user-provided context passed to validate_python.
  • CoreConfig: A TypedDict containing settings like strict, str_strip_whitespace, from_attributes, and more.

Using Configuration in Wrap Validators

Wrap validators allow you to inspect the configuration before deciding how to invoke the inner validation handler.

from typing import Any
from pydantic_core import SchemaValidator, core_schema

def wrap_validator(
value: Any,
handler: core_schema.ValidatorFunctionWrapHandler,
info: core_schema.ValidationInfo
) -> Any:
# Check if strict mode is enabled in the config
is_strict = info.config.get('strict') if info.config else False

if is_strict:
# Perform custom strict logic or just call handler
return handler(value)

# Non-strict logic: coerce to string before calling handler
return handler(str(value))

v = SchemaValidator(
core_schema.with_info_wrap_validator_function(
wrap_validator,
core_schema.str_schema()
),
config=core_schema.CoreConfig(strict=False)
)

assert v.validate_python(123) == '123'

Accessing Field Names and Peer Data

In complex schemas like typed_dict_schema, ValidationInfo allows you to see the name of the field currently being validated and the values of fields that have already passed validation.

from pydantic_core import SchemaValidator, core_schema

def check_dependency(value: int, info: core_schema.ValidationInfo) -> int:
# info.field_name tells us which field we are in
# info.data contains previously validated fields
if info.field_name == 'b' and 'a' in info.data:
return value + info.data['a']
return value

v = SchemaValidator(
core_schema.typed_dict_schema({
'a': core_schema.typed_dict_field(core_schema.int_schema()),
'b': core_schema.typed_dict_field(
core_schema.with_info_after_validator_function(
check_dependency,
core_schema.int_schema()
)
),
})
)

# 'a' is validated first, so it's available in info.data when 'b' is validated
assert v.validate_python({'a': 10, 'b': 5}) == {'a': 10, 'b': 15}

Troubleshooting

info.config is None

If no config argument is provided to the SchemaValidator constructor, info.config will be None inside your validator. Always check for existence before calling .get() or accessing keys:

def safe_validator(value, info):
strict = False
if info.config:
strict = info.config.get('strict', False)
# ...

Deprecated field_name in Schema

The field_name argument in helper functions like with_info_before_validator_function(..., field_name='foo') is deprecated. The field_name is now automatically populated in the ValidationInfo object by the SchemaValidator when the validator is used within a field context (like model_field or typed_dict_field).

info.data Availability

In model or typed_dict validation, info.data only contains fields that have already been successfully validated. It does not contain fields that failed validation or fields that are defined later in the schema.