MCPcopy
hub / github.com/pydantic/pydantic / apply_validators

Function apply_validators

pydantic/_internal/_generate_schema.py:2561–2584  ·  view source on GitHub ↗

Apply validators to a schema. Args: schema: The schema to apply validators on. validators: An iterable of validators. field_name: The name of the field if validators are being applied to a model field. Returns: The updated schema.

(
    schema: core_schema.CoreSchema,
    validators: Iterable[Decorator[RootValidatorDecoratorInfo]]
    | Iterable[Decorator[ValidatorDecoratorInfo]]
    | Iterable[Decorator[FieldValidatorDecoratorInfo]],
)

Source from the content-addressed store, hash-verified

2559# TODO V3: this function is only used for deprecated decorators. It should
2560# be removed once we drop support for those.
2561def apply_validators(
2562 schema: core_schema.CoreSchema,
2563 validators: Iterable[Decorator[RootValidatorDecoratorInfo]]
2564 | Iterable[Decorator[ValidatorDecoratorInfo]]
2565 | Iterable[Decorator[FieldValidatorDecoratorInfo]],
2566) -> core_schema.CoreSchema:
2567 """Apply validators to a schema.
2568
2569 Args:
2570 schema: The schema to apply validators on.
2571 validators: An iterable of validators.
2572 field_name: The name of the field if validators are being applied to a model field.
2573
2574 Returns:
2575 The updated schema.
2576 """
2577 for validator in validators:
2578 # Actually, type could be 'field' or 'model', but this is only used for deprecated
2579 # decorators, so let's not worry about it.
2580 info_arg = inspect_validator(validator.func, mode=validator.info.mode, type='field')
2581 val_type = 'with-info' if info_arg else 'no-info'
2582
2583 schema = _VALIDATOR_F_MATCH[(validator.info.mode, val_type)](validator.func, schema)
2584 return schema
2585
2586
2587def _validators_require_validate_default(validators: Iterable[Decorator[ValidatorDecoratorInfo]]) -> bool:

Callers 4

_model_schemaMethod · 0.85
_common_field_schemaMethod · 0.85
_dataclass_schemaMethod · 0.85

Calls 1

inspect_validatorFunction · 0.85

Tested by

no test coverage detected