You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77Lines changed: 77 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,10 +131,87 @@ The validation relies mostly on `netaddr` objects constructors. There's currentl
131
131
132
132
### Additionnal features
133
133
134
+
## IPAny
135
+
134
136
That may not be much, but an `IPAny` type is available. `pydantic` should produce the most acccurate object depending of the source object. An `IPAny` field will produce
135
137
136
138
* an `IPSet` if provided type is `list`, `tuple` or `set`
137
139
* an `IPNetwork` if value is a CIDR `str`
138
140
* an `IPRange` if value is an `str` containing a `-` character
139
141
* an `IPGlob` if value is an `str` containing a `*` char.
140
142
* an `IPAddress` in other cases.
143
+
144
+
## `IPv4Address` / `IPv6Address`
145
+
146
+
In case you want to match only IPv4 or IPv6 addresses (although, for portability, I suggest you don't), you can use these Internet Protocol version specific types annotations.
147
+
148
+
For instance, the following code:
149
+
150
+
```python
151
+
import pydantic
152
+
import netaddr_pydantic
153
+
154
+
classModel(pydantic.BaseModel):
155
+
address: netaddr_pydantic.IPv4Address
156
+
157
+
m4 = Model(address="1.2.3.4")
158
+
print(f"IPv4 has been validated: {m4.address}")
159
+
160
+
m6 = Model(address="dead:b00b:cafe::1")
161
+
162
+
```
163
+
164
+
Produces the following:
165
+
166
+
```
167
+
IPv4 has been validated: 1.2.3.4
168
+
Traceback (most recent call last):
169
+
File "/home/anvil/t.py", line 10, in <module>
170
+
m6 = Model(address="dead:b00b:cafe::1")
171
+
File "/home/anvil/.local/lib/python3.13/site-packages/pydantic/main.py", line 253, in __init__
0 commit comments