@@ -7,6 +7,14 @@ Redis OM uses [Pydantic][pydantic-url] behind the scenes to validate data at run
7
7
Validation works for basic type annotations like ` str ` . Thus, given the following model:
8
8
9
9
``` python
10
+ import datetime
11
+ from typing import Optional
12
+
13
+ from pydantic import EmailStr
14
+
15
+ from redis_om import HashModel
16
+
17
+
10
18
class Customer (HashModel ):
11
19
first_name: str
12
20
last_name: str
@@ -25,18 +33,43 @@ But every Redis OM model is also a Pydantic model, so you can use existing Pydan
25
33
Let's see what happens if we try to create a ` Customer ` object with an invalid email address.
26
34
27
35
``` 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
+
28
53
# 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
+ """
37
70
```
38
71
39
- This code generates the following error:
72
+ As you can see, creating the ` Customer ` object generated the following error:
40
73
41
74
```
42
75
Traceback:
@@ -45,9 +78,26 @@ This code generates the following error:
45
78
value is not a valid email address (type=value_error.email)
46
79
```
47
80
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 :
49
82
50
83
``` 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
+
51
101
andrew = Customer(
52
102
first_name = " Andrew" ,
53
103
last_name = " Brookins" ,
@@ -58,10 +108,19 @@ andrew = Customer(
58
108
)
59
109
60
110
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
+ """
62
121
```
63
122
64
- Once again, we get the valiation error:
123
+ Once again, we get the validation error:
65
124
66
125
```
67
126
Traceback:
0 commit comments