MCPcopy
hub / github.com/pydantic/pydantic / PlainValidator

Class PlainValidator

pydantic/functional_validators.py:157–253  ·  view source on GitHub ↗

!!! abstract "Usage Documentation" [field *plain* validators](../concepts/validators.md#field-plain-validator) A metadata class that indicates that a validation should be applied **instead** of the inner validation logic. !!! note Before v2.9, `PlainValidator` wasn't always

Source from the content-addressed store, hash-verified

155
156@dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
157class PlainValidator:
158 """!!! abstract "Usage Documentation"
159 [field *plain* validators](../concepts/validators.md#field-plain-validator)
160
161 A metadata class that indicates that a validation should be applied **instead** of the inner validation logic.
162
163 !!! note
164 Before v2.9, `PlainValidator` wasn't always compatible with JSON Schema generation for `mode='validation'`.
165 You can now use the `json_schema_input_type` argument to specify the input type of the function
166 to be used in the JSON schema when `mode='validation'` (the default). See the example below for more details.
167
168 Attributes:
169 func: The validator function.
170 json_schema_input_type: The input type used to generate the appropriate
171 JSON Schema (in validation mode). The actual input type is `Any`.
172
173 Example:
174 ```python
175 from typing import Annotated, Union
176
177 from pydantic import BaseModel, PlainValidator
178
179 def validate(v: object) -> int:
180 if not isinstance(v, (int, str)):
181 raise ValueError(f'Expected int or str, got {type(v)}')
182
183 return int(v) + 1
184
185 MyInt = Annotated[
186 int,
187 PlainValidator(validate, json_schema_input_type=Union[str, int]), # (1)!
188 ]
189
190 class Model(BaseModel):
191 a: MyInt
192
193 print(Model(a='1').a)
194 #> 2
195
196 print(Model(a=1).a)
197 #> 2
198 ```
199
200 1. In this example, we've specified the `json_schema_input_type` as `Union[str, int]` which indicates to the JSON schema
201 generator that in validation mode, the input type for the `a` field can be either a [`str`][] or an [`int`][].
202 """
203
204 func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
205 json_schema_input_type: Any = Any
206
207 def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
208 # Note that for some valid uses of PlainValidator, it is not possible to generate a core schema for the
209 # source_type, so calling `handler(source_type)` will error, which prevents us from generating a proper
210 # serialization schema. To work around this for use cases that will not involve serialization, we simply
211 # catch any PydanticSchemaGenerationError that may be raised while attempting to build the serialization schema
212 # and abort any attempts to handle special serialization.
213 from pydantic import PydanticSchemaGenerationError
214

Callers 9

FooClass · 0.90
ModelClass · 0.90
MyModelClass · 0.90
ModelClass · 0.90
ModelClass · 0.90

Calls

no outgoing calls