Make validation code example runnable
This commit is contained in:
parent
768d874473
commit
27311f3d0b
1 changed files with 71 additions and 12 deletions
|
@ -7,6 +7,14 @@ Redis OM uses [Pydantic][pydantic-url] behind the scenes to validate data at run
|
||||||
Validation works for basic type annotations like `str`. Thus, given the following model:
|
Validation works for basic type annotations like `str`. Thus, given the following model:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from pydantic import EmailStr
|
||||||
|
|
||||||
|
from redis_om import HashModel
|
||||||
|
|
||||||
|
|
||||||
class Customer(HashModel):
|
class Customer(HashModel):
|
||||||
first_name: str
|
first_name: str
|
||||||
last_name: str
|
last_name: str
|
||||||
|
@ -25,18 +33,43 @@ But every Redis OM model is also a Pydantic model, so you can use existing Pydan
|
||||||
Let's see what happens if we try to create a `Customer` object with an invalid email address.
|
Let's see what happens if we try to create a `Customer` object with an invalid email address.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
# We'll get a validation error if we try to use an invalid email address!
|
# We'll get a validation error if we try to use an invalid email address!
|
||||||
Customer(
|
try:
|
||||||
|
Customer(
|
||||||
first_name="Andrew",
|
first_name="Andrew",
|
||||||
last_name="Brookins",
|
last_name="Brookins",
|
||||||
email="Not an email address!",
|
email="Not an email address!",
|
||||||
join_date=datetime.date.today(),
|
join_date=datetime.date.today(),
|
||||||
age=38,
|
age=38,
|
||||||
bio="Python developer, works at Redis, Inc."
|
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)
|
||||||
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
This code generates the following error:
|
As you can see, creating the `Customer` object generated the following error:
|
||||||
|
|
||||||
```
|
```
|
||||||
Traceback:
|
Traceback:
|
||||||
|
@ -45,9 +78,26 @@ This code generates the following error:
|
||||||
value is not a valid email address (type=value_error.email)
|
value is not a valid email address (type=value_error.email)
|
||||||
```
|
```
|
||||||
|
|
||||||
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 it:
|
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:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
andrew = Customer(
|
andrew = Customer(
|
||||||
first_name="Andrew",
|
first_name="Andrew",
|
||||||
last_name="Brookins",
|
last_name="Brookins",
|
||||||
|
@ -58,10 +108,19 @@ andrew = Customer(
|
||||||
)
|
)
|
||||||
|
|
||||||
andrew.email = "Not valid"
|
andrew.email = "Not valid"
|
||||||
andrew.save()
|
|
||||||
|
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)
|
||||||
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
Once again, we get the valiation error:
|
Once again, we get the validation error:
|
||||||
|
|
||||||
```
|
```
|
||||||
Traceback:
|
Traceback:
|
||||||
|
|
Loading…
Reference in a new issue