2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# Validation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 17:31:36 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Redis OM uses [Pydantic][pydantic-url] behind the scenes to validate data at runtime, based on the model's type annotations.
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Basic Type Validation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Validation works for basic type annotations like `str` . Thus, given the following model:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```python
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import datetime
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from typing import Optional
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from pydantic import EmailStr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from redis_om import HashModel
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								class Customer(HashModel):
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    first_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    last_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    email: EmailStr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    join_date: datetime.date
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    age: int
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    bio: Optional[str]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								... Redis OM will ensure that `first_name`  is always a string.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								But every Redis OM model is also a Pydantic model, so you can use existing Pydantic validators like `EmailStr` , `Pattern` , and many more for complex validation!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Complex Validation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Let's see what happens if we try to create a `Customer`  object with an invalid email address.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```python
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import datetime
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from typing import Optional
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from pydantic import EmailStr, ValidationError
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from redis_om import HashModel
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class Customer(HashModel):
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    first_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    last_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    email: EmailStr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    join_date: datetime.date
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    age: int
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    bio: Optional[str]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# We'll get a validation error if we try to use an invalid email address!
  
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								try:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Customer(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        first_name="Andrew",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        last_name="Brookins",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        email="Not an email address!",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        join_date=datetime.date.today(),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        age=38,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        bio="Python developer, works at Redis, Inc."
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								except ValidationError as e:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    print(e)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pydantic.error_wrappers.ValidationError: 1 validation error for Customer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     email
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       value is not a valid email address (type=value_error.email)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								As you can see, creating the `Customer`  object generated the following error:
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 Traceback:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 pydantic.error_wrappers.ValidationError: 1 validation error for Customer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 email
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   value is not a valid email address (type=value_error.email)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We'll also get a validation error if we change a field on a model instance to an invalid value and then try to save the model:
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```python
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import datetime
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from typing import Optional
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from pydantic import EmailStr, ValidationError
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from redis_om import HashModel
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class Customer(HashModel):
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    first_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    last_name: str
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    email: EmailStr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    join_date: datetime.date
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    age: int
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    bio: Optional[str]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								andrew = Customer(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    first_name="Andrew",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    last_name="Brookins",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    email="andrew.brookins@example .com",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    join_date=datetime.date.today(),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    age=38,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    bio="Python developer, works at Redis, Inc."
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								andrew.email = "Not valid"
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								try:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    andrew.save()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								except ValidationError as e:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    print(e)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pydantic.error_wrappers.ValidationError: 1 validation error for Customer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     email
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       value is not a valid email address (type=value_error.email)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-12 07:13:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Once again, we get the validation error:
							 
						 
					
						
							
								
									
										
										
										
											2021-10-25 14:21:39 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 Traceback:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 pydantic.error_wrappers.ValidationError: 1 validation error for Customer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 email
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   value is not a valid email address (type=value_error.email)
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 17:31:36 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-24 18:12:27 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Constrained Values
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you want to use any of the constr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Pydantic includes many type annotations to introduce constraints to your model field values.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The concept of "constraints" includes quite a few possibilities:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Strings that are always lowercase 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Strings that must match a regular expression 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Integers within a range 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Integers that are a specific multiple 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  And many more... 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All of these constraint types work with Redis OM models. Read the [Pydantic documentation on constrained types ](https://pydantic-docs.helpmanual.io/usage/types/#constrained-types ) to learn more.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 17:31:36 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[pydantic-url]: https://github.com/samuelcolvin/pydantic