Skip to content

Commit 27311f3

Browse files
author
Andrew Brookins
committed
Make validation code example runnable
1 parent 768d874 commit 27311f3

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

docs/validation.md

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Redis OM uses [Pydantic][pydantic-url] behind the scenes to validate data at run
77
Validation works for basic type annotations like `str`. Thus, given the following model:
88

99
```python
10+
import datetime
11+
from typing import Optional
12+
13+
from pydantic import EmailStr
14+
15+
from redis_om import HashModel
16+
17+
1018
class Customer(HashModel):
1119
first_name: str
1220
last_name: str
@@ -25,18 +33,43 @@ But every Redis OM model is also a Pydantic model, so you can use existing Pydan
2533
Let's see what happens if we try to create a `Customer` object with an invalid email address.
2634

2735
```python
36+
import datetime
37+
from typing import Optional
38+
39+
from pydantic import EmailStr, ValidationError
40+
41+
from redis_om import HashModel
42+
43+
44+
class Customer(HashModel):
45+
first_name: str
46+
last_name: str
47+
email: EmailStr
48+
join_date: datetime.date
49+
age: int
50+
bio: Optional[str]
51+
52+
2853
# We'll get a validation error if we try to use an invalid email address!
29-
Customer(
30-
first_name="Andrew",
31-
last_name="Brookins",
32-
email="Not an email address!",
33-
join_date=datetime.date.today(),
34-
age=38,
35-
bio="Python developer, works at Redis, Inc."
36-
)
54+
try:
55+
Customer(
56+
first_name="Andrew",
57+
last_name="Brookins",
58+
email="Not an email address!",
59+
join_date=datetime.date.today(),
60+
age=38,
61+
bio="Python developer, works at Redis, Inc."
62+
)
63+
except ValidationError as e:
64+
print(e)
65+
"""
66+
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
67+
email
68+
value is not a valid email address (type=value_error.email)
69+
"""
3770
```
3871

39-
This code generates the following error:
72+
As you can see, creating the `Customer` object generated the following error:
4073

4174
```
4275
Traceback:
@@ -45,9 +78,26 @@ This code generates the following error:
4578
value is not a valid email address (type=value_error.email)
4679
```
4780

48-
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:
81+
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:
4982

5083
```python
84+
import datetime
85+
from typing import Optional
86+
87+
from pydantic import EmailStr, ValidationError
88+
89+
from redis_om import HashModel
90+
91+
92+
class Customer(HashModel):
93+
first_name: str
94+
last_name: str
95+
email: EmailStr
96+
join_date: datetime.date
97+
age: int
98+
bio: Optional[str]
99+
100+
51101
andrew = Customer(
52102
first_name="Andrew",
53103
last_name="Brookins",
@@ -58,10 +108,19 @@ andrew = Customer(
58108
)
59109

60110
andrew.email = "Not valid"
61-
andrew.save()
111+
112+
try:
113+
andrew.save()
114+
except ValidationError as e:
115+
print(e)
116+
"""
117+
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
118+
email
119+
value is not a valid email address (type=value_error.email)
120+
"""
62121
```
63122

64-
Once again, we get the valiation error:
123+
Once again, we get the validation error:
65124

66125
```
67126
Traceback:

0 commit comments

Comments
 (0)