Using pre=True for Pre-Validation
Pre-validation is a powerful feature of Pydantic that allows developers to preprocess and modify input data before it undergoes the standard validation procedure. This is especially beneficial when working with user input or data from external sources, which may require cleaning, transformation, or reformatting before being validated against a field's specified data type.
To employ the pre=True
flag in your Pydantic model, you'll need to include it as a keyword argument in the @validator
decorator. The following example demonstrates how to achieve this:
from pydantic import BaseModel, validator
class MyModel(BaseModel):
field: str
@validator('field', pre=True)
def clean_field(cls, value):
# Implement pre-validation logic here
return value
In this example, a Pydantic model called MyModel
is defined, containing a single field called field
with a data type of str
. The @validator
decorator is applied to a class method named clean_field
, which is responsible for preprocessing the input data before it is validated.
The pre=True
flag, passed as a keyword argument within the @validator
decorator, indicates that the clean_field
method should be executed prior to standard validation. Within this method, you can implement any pre-validation logic necessary to clean or transform the input data. For instance, you might remove unwanted characters, convert the data to a specific format, or perform other data manipulation tasks. Once your preprocessing is complete, return the modified value, which will then be passed on to the standard validation process.
Implementing Custom Pre-Validation Functions in Pydantic
In addition to using the pre=True
flag within the @validator
decorator, Pydantic enables developers to implement custom pre-validation functions that further enhance data processing capabilities. Custom pre-validation functions can be particularly useful for handling complex data manipulation tasks or for reusing pre-validation logic across multiple fields or models.
To demonstrate how to create and use a custom pre-validation function, consider the following example:
from pydantic import BaseModel, validator
def strip_whitespace(value: str) -> str:
return value.strip()
class MyModel(BaseModel):
field1: str
field2: str
@validator('field1', 'field2', pre=True)
def apply_strip_whitespace(cls, value):
# Utilize the custom pre-validation function
return strip_whitespace(value)
In this example, a custom pre-validation function called strip_whitespace
is defined outside of the MyModel
class. This function takes a string value as input and returns the same value with leading and trailing whitespace removed.
The apply_strip_whitespace
class method within the MyModel
class is decorated with the @validator
decorator and the pre=True
flag. This method is responsible for applying the strip_whitespace
function to both field1
and field2
before they undergo standard validation.
By implementing custom pre-validation functions like strip_whitespace
, developers can create reusable data manipulation logic that can be applied to multiple fields or models. This approach not only helps maintain cleaner and more organized code, but also promotes consistency and reliability in data handling throughout the application.
References