0
# Basic Data Validation
1
2
Core validators for fundamental data types including numeric ranges, string lengths, identifiers, and format validation. These validators provide the building blocks for most validation scenarios.
3
4
## Types
5
6
```python { .api }
7
PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime, None)
8
9
class AbsMin:
10
"""Represents unlimited minimum value (negative infinity)."""
11
pass
12
13
class AbsMax:
14
"""Represents unlimited maximum value (positive infinity)."""
15
pass
16
```
17
18
## Capabilities
19
20
### Value Range Validation
21
22
Validates that numeric values fall within specified minimum and maximum bounds.
23
24
```python { .api }
25
def between(value: PossibleValueTypes, /, *, min_val: Union[PossibleValueTypes, AbsMin, None] = None, max_val: Union[PossibleValueTypes, AbsMax, None] = None) -> Union[Literal[True], ValidationError]:
26
"""
27
Validate that a number is between minimum and/or maximum values.
28
29
Parameters:
30
- value: The numeric value to validate
31
- min_val: Minimum allowed value (inclusive), or None for no minimum
32
- max_val: Maximum allowed value (inclusive), or None for no maximum
33
34
Returns:
35
True if value is within bounds, ValidationError otherwise
36
"""
37
```
38
39
Usage examples:
40
41
```python
42
import validators
43
44
# Check if value is within range
45
validators.between(5, min_val=1, max_val=10) # True
46
validators.between(15, min_val=1, max_val=10) # ValidationError
47
48
# Check minimum only
49
validators.between(100, min_val=50) # True
50
51
# Check maximum only
52
validators.between(25, max_val=50) # True
53
```
54
55
### String Length Validation
56
57
Validates that string lengths fall within specified character count bounds.
58
59
```python { .api }
60
def length(value: str, /, *, min_val: Union[int, None] = None, max_val: Union[int, None] = None) -> Union[Literal[True], ValidationError]:
61
"""
62
Validate string length within specified range.
63
64
Parameters:
65
- value: The string to validate
66
- min_val: Minimum allowed length, or None for no minimum
67
- max_val: Maximum allowed length, or None for no maximum
68
69
Returns:
70
True if string length is within bounds, ValidationError otherwise
71
"""
72
```
73
74
Usage examples:
75
76
```python
77
import validators
78
79
# Password length validation
80
validators.length("mypassword", min_val=8, max_val=32) # True
81
validators.length("abc", min_val=8) # ValidationError
82
83
# Username length validation
84
validators.length("user123", min_val=3, max_val=20) # True
85
```
86
87
### UUID Validation
88
89
Validates UUID (Universally Unique Identifier) version 4 strings in standard format.
90
91
```python { .api }
92
def uuid(value: Union[str, UUID], /) -> Union[Literal[True], ValidationError]:
93
"""
94
Validate UUID version 4 strings.
95
96
Parameters:
97
- value: String or UUID object to validate
98
99
Returns:
100
True if value is a valid UUID, ValidationError otherwise
101
102
Accepts both string and UUID object inputs.
103
Validates proper UUID format: 8-4-4-4-12 hexadecimal digits.
104
"""
105
```
106
107
Usage examples:
108
109
```python
110
import validators
111
from uuid import UUID
112
113
# Valid UUID strings
114
validators.uuid('550e8400-e29b-41d4-a716-446655440000') # True
115
validators.uuid('6ba7b810-9dad-11d1-80b4-00c04fd430c8') # True
116
117
# UUID object
118
uuid_obj = UUID('550e8400-e29b-41d4-a716-446655440000')
119
validators.uuid(uuid_obj) # True
120
121
# Invalid formats
122
validators.uuid('not-a-uuid') # ValidationError
123
validators.uuid('550e8400-e29b-41d4-a716') # ValidationError (too short)
124
```
125
126
### URL Slug Validation
127
128
Validates URL-friendly slugs containing only lowercase letters, numbers, and hyphens.
129
130
```python { .api }
131
def slug(value: str, /) -> Union[Literal[True], ValidationError]:
132
"""
133
Validate URL slug format.
134
135
Parameters:
136
- value: String to validate as slug
137
138
Returns:
139
True if value is a valid slug, ValidationError otherwise
140
141
Valid slugs contain only:
142
- Lowercase letters (a-z)
143
- Numbers (0-9)
144
- Hyphens (-)
145
- Cannot start or end with hyphen
146
"""
147
```
148
149
Usage examples:
150
151
```python
152
import validators
153
154
# Valid slugs
155
validators.slug('my-blog-post') # True
156
validators.slug('product-123') # True
157
validators.slug('hello-world') # True
158
159
# Invalid slugs
160
validators.slug('My-Blog-Post') # ValidationError (uppercase)
161
validators.slug('hello_world') # ValidationError (underscore)
162
validators.slug('-start-dash') # ValidationError (starts with dash)
163
validators.slug('end-dash-') # ValidationError (ends with dash)
164
```