0
# Network Field Types
1
2
Specialized field types for network-related data validation in Schematics. These types handle URLs, email addresses, IP addresses, and MAC addresses with protocol-specific validation and format normalization.
3
4
## Capabilities
5
6
### URL Fields
7
8
Handle web addresses with validation and optional existence checking.
9
10
```python { .api }
11
class URLType(BaseType):
12
"""
13
URL field with validation and optional existence checking.
14
15
Validates URL format according to RFC standards and optionally
16
verifies that the URL is accessible via HTTP requests.
17
"""
18
19
def __init__(self, verify_exists=False, **kwargs):
20
"""
21
Initialize URL field.
22
23
Args:
24
verify_exists (bool): Whether to verify URL accessibility
25
**kwargs: Base field options
26
"""
27
```
28
29
### Email Fields
30
31
Handle email addresses with comprehensive format validation.
32
33
```python { .api }
34
class EmailType(BaseType):
35
"""
36
Email address field with regex validation.
37
38
Validates email format according to RFC standards including
39
local and domain parts with comprehensive pattern matching.
40
"""
41
42
def __init__(self, **kwargs):
43
"""
44
Initialize email field.
45
46
Args:
47
**kwargs: Base field options
48
"""
49
```
50
51
### IP Address Fields
52
53
Handle various IP address formats with version-specific validation.
54
55
```python { .api }
56
class IPAddressType(BaseType):
57
"""
58
IPv4 or IPv6 address field with automatic version detection.
59
60
Accepts both IPv4 and IPv6 addresses and validates format
61
according to the appropriate IP version standards.
62
"""
63
64
def __init__(self, **kwargs):
65
"""
66
Initialize IP address field.
67
68
Args:
69
**kwargs: Base field options
70
"""
71
72
class IPv4Type(BaseType):
73
"""
74
IPv4 address field with dot notation validation.
75
76
Validates IPv4 addresses in dotted decimal notation (e.g., 192.168.1.1)
77
with range checking for each octet.
78
"""
79
80
def __init__(self, **kwargs):
81
"""
82
Initialize IPv4 field.
83
84
Args:
85
**kwargs: Base field options
86
"""
87
88
class IPv6Type(BaseType):
89
"""
90
IPv6 address field with colon notation validation.
91
92
Validates IPv6 addresses in colon notation with support for
93
compressed notation and embedded IPv4 addresses.
94
"""
95
96
def __init__(self, **kwargs):
97
"""
98
Initialize IPv6 field.
99
100
Args:
101
**kwargs: Base field options
102
"""
103
```
104
105
### MAC Address Fields
106
107
Handle network hardware addresses with flexible separator support.
108
109
```python { .api }
110
class MACAddressType(BaseType):
111
"""
112
MAC address field with colon/dash separator support.
113
114
Validates MAC addresses in various formats including colon-separated
115
(00:11:22:33:44:55) and dash-separated (00-11-22-33-44-55) notation.
116
"""
117
118
def __init__(self, **kwargs):
119
"""
120
Initialize MAC address field.
121
122
Args:
123
**kwargs: Base field options
124
"""
125
```
126
127
## Usage Examples
128
129
### URL Validation
130
131
```python
132
from schematics.models import Model
133
from schematics.types import URLType, StringType
134
135
class Website(Model):
136
name = StringType(required=True)
137
url = URLType(required=True)
138
verified_url = URLType(verify_exists=True) # Checks if URL is accessible
139
140
# Valid URLs
141
site = Website({
142
'name': 'Example Site',
143
'url': 'https://www.example.com/path?param=value',
144
'verified_url': 'https://httpbin.org/get' # Will be checked for accessibility
145
})
146
site.validate() # Success (assuming verified_url is accessible)
147
148
# Invalid URL format
149
invalid_site = Website({
150
'name': 'Bad Site',
151
'url': 'not-a-url', # Invalid URL format
152
})
153
# invalid_site.validate() would raise ValidationError
154
```
155
156
### Email Validation
157
158
```python
159
from schematics.types import EmailType
160
161
class UserProfile(Model):
162
username = StringType(required=True)
163
email = EmailType(required=True)
164
backup_email = EmailType()
165
166
# Valid email formats
167
user = UserProfile({
168
'username': 'john_doe',
169
'email': 'john.doe@example.com',
170
'backup_email': 'john+backup@gmail.com'
171
})
172
user.validate() # Success
173
174
# Invalid email formats
175
invalid_user = UserProfile({
176
'username': 'jane',
177
'email': 'invalid-email', # Missing @ and domain
178
})
179
# invalid_user.validate() would raise ValidationError
180
```
181
182
### IP Address Validation
183
184
```python
185
from schematics.types import IPAddressType, IPv4Type, IPv6Type
186
187
class NetworkConfig(Model):
188
any_ip = IPAddressType(required=True) # IPv4 or IPv6
189
server_ipv4 = IPv4Type(required=True) # IPv4 only
190
server_ipv6 = IPv6Type() # IPv6 only
191
192
# Mixed IP versions
193
config = NetworkConfig({
194
'any_ip': '192.168.1.1', # IPv4
195
'server_ipv4': '10.0.0.1', # IPv4
196
'server_ipv6': '2001:db8::1' # IPv6
197
})
198
config.validate() # Success
199
200
# Another valid configuration
201
config2 = NetworkConfig({
202
'any_ip': '::1', # IPv6 (localhost)
203
'server_ipv4': '127.0.0.1', # IPv4 localhost
204
})
205
config2.validate() # Success
206
```
207
208
### MAC Address Validation
209
210
```python
211
from schematics.types import MACAddressType
212
213
class NetworkInterface(Model):
214
name = StringType(required=True)
215
mac_address = MACAddressType(required=True)
216
217
# Various MAC address formats
218
interface1 = NetworkInterface({
219
'name': 'eth0',
220
'mac_address': '00:11:22:33:44:55' # Colon-separated
221
})
222
223
interface2 = NetworkInterface({
224
'name': 'wlan0',
225
'mac_address': '00-11-22-33-44-55' # Dash-separated
226
})
227
228
interface1.validate() # Success
229
interface2.validate() # Success
230
```
231
232
### Network Service Configuration
233
234
```python
235
from schematics.types import URLType, EmailType, IPv4Type, IntType
236
237
class ServiceConfig(Model):
238
service_name = StringType(required=True)
239
api_endpoint = URLType(required=True, verify_exists=True)
240
admin_email = EmailType(required=True)
241
server_ip = IPv4Type(required=True)
242
port = IntType(min_value=1, max_value=65535, required=True)
243
health_check_url = URLType()
244
245
# Complete service configuration
246
service = ServiceConfig({
247
'service_name': 'User Authentication API',
248
'api_endpoint': 'https://api.example.com/v1/auth',
249
'admin_email': 'admin@example.com',
250
'server_ip': '10.0.1.50',
251
'port': 8443,
252
'health_check_url': 'https://api.example.com/health'
253
})
254
255
service.validate() # Validates all network-related fields
256
257
# Export for configuration file
258
config_dict = service.to_primitive()
259
```
260
261
### Multi-Environment Network Settings
262
263
```python
264
class EnvironmentConfig(Model):
265
env_name = StringType(required=True, choices=['dev', 'staging', 'prod'])
266
database_urls = ListType(URLType(), min_size=1)
267
api_servers = ListType(IPv4Type())
268
notification_emails = ListType(EmailType())
269
load_balancer_ip = IPAddressType() # Can be IPv4 or IPv6
270
271
# Development environment
272
dev_config = EnvironmentConfig({
273
'env_name': 'dev',
274
'database_urls': [
275
'postgresql://user:pass@localhost:5432/myapp_dev'
276
],
277
'api_servers': ['127.0.0.1', '192.168.1.100'],
278
'notification_emails': ['dev-team@example.com'],
279
'load_balancer_ip': '192.168.1.10'
280
})
281
282
# Production environment
283
prod_config = EnvironmentConfig({
284
'env_name': 'prod',
285
'database_urls': [
286
'postgresql://user:pass@db1.example.com:5432/myapp',
287
'postgresql://user:pass@db2.example.com:5432/myapp'
288
],
289
'api_servers': ['10.0.1.10', '10.0.1.11', '10.0.1.12'],
290
'notification_emails': ['alerts@example.com', 'ops@example.com'],
291
'load_balancer_ip': '2001:db8::load-balancer' # IPv6
292
})
293
294
dev_config.validate() # Success
295
prod_config.validate() # Success
296
```