0
# Built-in Validators
1
2
Comprehensive collection of pre-built validation functions covering common validation scenarios including strings, numbers, conditionals, and network formats.
3
4
## Capabilities
5
6
### Basic Required Validation
7
8
Core required validation that handles various data types appropriately.
9
10
```javascript { .api }
11
/**
12
* Validates that a value is present and not empty
13
* - Arrays: must have length > 0
14
* - Strings: must have length > 0 after trimming
15
* - Objects: must have at least one enumerable property
16
* - Dates: must be valid date objects
17
* - Other values: must be truthy (except false, which is valid)
18
*/
19
function required(value: any): boolean;
20
```
21
22
**Usage:**
23
24
```javascript
25
import { required } from 'vuelidate/lib/validators'
26
27
validations: {
28
name: { required },
29
terms: { required } // for checkboxes - false fails, true passes
30
}
31
```
32
33
### String Format Validators
34
35
Validators for common string format requirements.
36
37
```javascript { .api }
38
/**
39
* Validates alphabetic characters only (a-z, A-Z)
40
*/
41
function alpha(value: any): boolean;
42
43
/**
44
* Validates alphanumeric characters only (a-z, A-Z, 0-9)
45
*/
46
function alphaNum(value: any): boolean;
47
48
/**
49
* Validates email format using comprehensive regex
50
*/
51
function email(value: any): boolean;
52
53
/**
54
* Validates URL format
55
*/
56
function url(value: any): boolean;
57
```
58
59
**Usage:**
60
61
```javascript
62
import { alpha, alphaNum, email, url } from 'vuelidate/lib/validators'
63
64
validations: {
65
firstName: { alpha },
66
username: { alphaNum },
67
email: { email },
68
website: { url }
69
}
70
```
71
72
### Numeric Validators
73
74
Validators for numeric values and constraints.
75
76
```javascript { .api }
77
/**
78
* Validates numeric values (including numeric strings)
79
*/
80
function numeric(value: any): boolean;
81
82
/**
83
* Validates integer values
84
*/
85
function integer(value: any): boolean;
86
87
/**
88
* Validates decimal numbers
89
*/
90
function decimal(value: any): boolean;
91
```
92
93
**Usage:**
94
95
```javascript
96
import { numeric, integer, decimal } from 'vuelidate/lib/validators'
97
98
validations: {
99
age: { numeric, integer },
100
price: { numeric, decimal },
101
quantity: { integer }
102
}
103
```
104
105
### Length Validators
106
107
Validators for length constraints on strings, arrays, and objects.
108
109
```javascript { .api }
110
/**
111
* Validates minimum length
112
* @param length - Minimum required length
113
*/
114
function minLength(length: number): (value: any) => boolean;
115
116
/**
117
* Validates maximum length
118
* @param length - Maximum allowed length
119
*/
120
function maxLength(length: number): (value: any) => boolean;
121
```
122
123
**Usage:**
124
125
```javascript
126
import { minLength, maxLength } from 'vuelidate/lib/validators'
127
128
validations: {
129
password: {
130
minLength: minLength(8),
131
maxLength: maxLength(50)
132
},
133
tags: {
134
minLength: minLength(1), // at least one tag
135
maxLength: maxLength(5) // max 5 tags
136
}
137
}
138
```
139
140
### Value Range Validators
141
142
Validators for numeric value ranges and comparisons.
143
144
```javascript { .api }
145
/**
146
* Validates minimum numeric value
147
* @param min - Minimum allowed value
148
*/
149
function minValue(min: number): (value: any) => boolean;
150
151
/**
152
* Validates maximum numeric value
153
* @param max - Maximum allowed value
154
*/
155
function maxValue(max: number): (value: any) => boolean;
156
157
/**
158
* Validates value is between min and max (inclusive)
159
* @param min - Minimum allowed value
160
* @param max - Maximum allowed value
161
*/
162
function between(min: number, max: number): (value: any) => boolean;
163
```
164
165
**Usage:**
166
167
```javascript
168
import { minValue, maxValue, between } from 'vuelidate/lib/validators'
169
170
validations: {
171
age: {
172
minValue: minValue(0),
173
maxValue: maxValue(120)
174
},
175
rating: {
176
between: between(1, 5)
177
}
178
}
179
```
180
181
### Conditional Validators
182
183
Validators that depend on other fields or conditions.
184
185
```javascript { .api }
186
/**
187
* Required only if referenced property is truthy
188
* @param prop - Property name or function to check
189
*/
190
function requiredIf(prop: string | Function): (value: any, parentVm?: any) => boolean;
191
192
/**
193
* Required unless referenced property is truthy
194
* @param prop - Property name or function to check
195
*/
196
function requiredUnless(prop: string | Function): (value: any, parentVm?: any) => boolean;
197
198
/**
199
* Validates value matches another property
200
* @param equalTo - Property name or function to compare against
201
*/
202
function sameAs(equalTo: string | Function): (value: any, parentVm?: any) => boolean;
203
```
204
205
**Usage:**
206
207
```javascript
208
import { requiredIf, requiredUnless, sameAs } from 'vuelidate/lib/validators'
209
210
validations: {
211
email: { required },
212
phone: {
213
requiredUnless: requiredUnless('email') // phone required unless email provided
214
},
215
password: { required, minLength: minLength(6) },
216
confirmPassword: {
217
requiredIf: requiredIf('password'),
218
sameAsPassword: sameAs('password')
219
},
220
companyName: {
221
requiredIf: requiredIf(function() {
222
return this.accountType === 'business'
223
})
224
}
225
}
226
```
227
228
### Network Format Validators
229
230
Validators for network addresses and identifiers.
231
232
```javascript { .api }
233
/**
234
* Validates IPv4 address format (e.g., "192.168.1.1")
235
* Does not validate IPv6 addresses
236
*/
237
function ipAddress(value: any): boolean;
238
239
/**
240
* Validates MAC address format with optional separator
241
* @param separator - Character separator between hex pairs (default: ':')
242
*/
243
function macAddress(separator?: string): (value: any) => boolean;
244
```
245
246
**Usage:**
247
248
```javascript
249
import { ipAddress, macAddress } from 'vuelidate/lib/validators'
250
251
validations: {
252
serverIp: { ipAddress },
253
deviceMac: { macAddress }, // default ':' separator
254
deviceMacDash: { macAddress: macAddress('-') }, // custom separator
255
deviceMacNone: { macAddress: macAddress('') } // no separator
256
}
257
```
258
259
### Logic Composition Validators
260
261
Validators for combining multiple validation rules with logical operations.
262
263
```javascript { .api }
264
/**
265
* Logical AND - all validators must pass
266
* @param validators - Variable number of validator functions
267
*/
268
function and(...validators: Function[]): (value: any, parentVm?: any) => boolean;
269
270
/**
271
* Logical OR - at least one validator must pass
272
* @param validators - Variable number of validator functions
273
*/
274
function or(...validators: Function[]): (value: any, parentVm?: any) => boolean;
275
276
/**
277
* Logical NOT - inverts the result of a validator
278
* @param validator - Validator function to negate
279
*/
280
function not(validator: Function): (value: any, parentVm?: any) => boolean;
281
```
282
283
**Usage:**
284
285
```javascript
286
import { and, or, not, alpha, numeric, minLength } from 'vuelidate/lib/validators'
287
288
validations: {
289
code: {
290
// Must be either alphabetic OR numeric, AND at least 3 characters
291
validCode: and(
292
or(alpha, numeric),
293
minLength(3)
294
)
295
},
296
username: {
297
// Must NOT be a numeric-only string
298
notNumericOnly: not(numeric)
299
}
300
}
301
```
302
303
## Validator Behavior
304
305
### Empty Value Handling
306
307
Most validators (except `required`) allow empty values to pass validation:
308
309
```javascript
310
// These will all pass validation for empty strings
311
const result1 = email('') // true - empty values allowed
312
const result2 = minLength(5)('') // true - empty values allowed
313
const result3 = required('') // false - required specifically checks for non-empty
314
315
// Combine with required for mandatory fields
316
validations: {
317
email: {
318
required, // Must be present
319
email // Must be valid email format when present
320
}
321
}
322
```
323
324
### Type Coercion
325
326
Validators handle type coercion appropriately for their context:
327
328
```javascript
329
// Numeric validators accept numeric strings
330
numeric('123') // true
331
integer('123') // true
332
minValue(10)('15') // true
333
334
// Length validators work on strings, arrays, and objects
335
minLength(3)('hello') // true - string length
336
minLength(2)([1, 2, 3]) // true - array length
337
minLength(1)({ a: 1, b: 2 }) // true - object property count
338
```