Skip to content

Commit fdfeb27

Browse files
committed
add missing IPv[46]Addwork doc
1 parent 676864b commit fdfeb27

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,87 @@ The validation relies mostly on `netaddr` objects constructors. There's currentl
131131

132132
### Additionnal features
133133

134+
## IPAny
135+
134136
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
135137

136138
* an `IPSet` if provided type is `list`, `tuple` or `set`
137139
* an `IPNetwork` if value is a CIDR `str`
138140
* an `IPRange` if value is an `str` containing a `-` character
139141
* an `IPGlob` if value is an `str` containing a `*` char.
140142
* 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+
class Model(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__
172+
validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
173+
pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
174+
address
175+
Value error, base address 'dead:b00b:cafe::1' is not IPv4 [type=value_error, input_value='dead:b00b:cafe::1', input_type=str]
176+
For further information visit https://errors.pydantic.dev/2.11/v/value_error
177+
```
178+
179+
## `IPv4Network` / `IPv6Network`
180+
181+
Well, duh. :unamused:
182+
183+
```python
184+
import pydantic
185+
import netaddr_pydantic
186+
187+
class Model(pydantic.BaseModel):
188+
address: netaddr_pydantic.IPv6Network
189+
190+
m6 = Model(address="dead:b00b:cafe::/64")
191+
print(m6.address)
192+
193+
m4 = Model(address="1.2.3.0/24")
194+
```
195+
196+
:interrobang:
197+
198+
:expressionless:
199+
200+
```
201+
IPv6 CIDR has been validated: dead:b00b:cafe::/64
202+
Traceback (most recent call last):
203+
File "/home/anvil/t.py", line 10, in <module>
204+
m4 = Model(address="1.2.3.0/24")
205+
File "/home/anvil/.local/lib/python3.13/site-packages/pydantic/main.py", line 253, in __init__
206+
validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
207+
pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
208+
address
209+
Value error, base address '1.2.3.0' is not IPv6 [type=value_error, input_value='1.2.3.0/24', input_type=str]
210+
For further information visit https://errors.pydantic.dev/2.11/v/value_error
211+
```
212+
213+
:boom: :bangbang:
214+
215+
:tada:
216+
217+
:smirk:

0 commit comments

Comments
 (0)