0
# Native Type Validators
1
2
Native type validators provide validation for JavaScript's built-in types with sensible default values and runtime type checking.
3
4
## Capabilities
5
6
### Any Type Validator
7
8
Accepts any value without type checking. Useful for props that can accept multiple types.
9
10
```typescript { .api }
11
/**
12
* Creates a validator that accepts any value
13
* @returns VueTypeValidableDef that accepts any type
14
*/
15
static get any(): VueTypeValidableDef<any>;
16
17
// Standalone function
18
function any<T = any>(): VueTypeValidableDef<T>;
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import VueTypes, { any } from "vue-types";
25
26
// Via VueTypes class
27
const anyProp = VueTypes.any;
28
const anyRequired = VueTypes.any.isRequired;
29
30
// Via standalone function
31
const anyStandalone = any();
32
const anyWithDefault = any().def('default value');
33
```
34
35
### Function Validator
36
37
Validates function values with default function support.
38
39
```typescript { .api }
40
/**
41
* Creates a validator for function types
42
* @returns VueTypeValidableDef for function types
43
*/
44
static get func(): VueTypeValidableDef<Function>;
45
46
// Standalone function with generic support
47
function func<T extends (...args: any[]) => any>(): VueTypeValidableDef<T>;
48
```
49
50
**Usage Examples:**
51
52
```typescript
53
import VueTypes, { func } from "vue-types";
54
55
// Basic function validation
56
const onClick = VueTypes.func.isRequired;
57
const onUpdate = VueTypes.func.def(() => {});
58
59
// With type constraints
60
const typedHandler = func<(value: string) => void>().isRequired;
61
```
62
63
### Boolean Validator
64
65
Validates boolean values with configurable default.
66
67
```typescript { .api }
68
/**
69
* Creates a validator for boolean types
70
* @returns VueTypeValidableDef for boolean values
71
*/
72
static get bool(): VueTypeValidableDef<boolean>;
73
74
// Standalone function
75
function bool(): VueTypeValidableDef<boolean>;
76
```
77
78
**Usage Examples:**
79
80
```typescript
81
import VueTypes, { bool } from "vue-types";
82
83
// Boolean with sensible default (true)
84
const isActive = VueTypes.bool;
85
const isRequired = VueTypes.bool.isRequired;
86
87
// Custom default
88
const isVisible = bool().def(false);
89
```
90
91
### String Validator
92
93
Validates string values with empty string as default.
94
95
```typescript { .api }
96
/**
97
* Creates a validator for string types
98
* @returns VueTypeValidableDef for string values
99
*/
100
static get string(): VueTypeValidableDef<string>;
101
102
// Standalone function with generic constraint support
103
function string<T extends string = string>(): VueTypeValidableDef<T>;
104
```
105
106
**Usage Examples:**
107
108
```typescript
109
import VueTypes, { string } from "vue-types";
110
111
// Basic string validation
112
const title = VueTypes.string.isRequired;
113
const description = VueTypes.string.def('');
114
115
// With type constraints for literal types
116
const theme = string<'light' | 'dark'>().def('light');
117
```
118
119
### Number Validator
120
121
Validates numeric values with zero as default.
122
123
```typescript { .api }
124
/**
125
* Creates a validator for number types
126
* @returns VueTypeValidableDef for number values
127
*/
128
static get number(): VueTypeValidableDef<number>;
129
130
// Standalone function with generic constraint support
131
function number<T extends number = number>(): VueTypeValidableDef<T>;
132
```
133
134
**Usage Examples:**
135
136
```typescript
137
import VueTypes, { number } from "vue-types";
138
139
// Basic number validation
140
const count = VueTypes.number.def(0);
141
const price = VueTypes.number.isRequired;
142
143
// With custom validation
144
const positiveNumber = number().validate((value) => value > 0);
145
```
146
147
### Array Validator
148
149
Validates array values with empty array factory as default.
150
151
```typescript { .api }
152
/**
153
* Creates a validator for array types
154
* @returns VueTypeValidableDef for array values
155
*/
156
static get array(): VueTypeValidableDef<any[]>;
157
158
// Standalone function with generic element type support
159
function array<T>(): VueTypeValidableDef<T[]>;
160
```
161
162
**Usage Examples:**
163
164
```typescript
165
import VueTypes, { array } from "vue-types";
166
167
// Basic array validation
168
const items = VueTypes.array.def(() => []);
169
const tags = VueTypes.array.isRequired;
170
171
// With element type constraint
172
const stringArray = array<string>().def(() => []);
173
```
174
175
### Object Validator
176
177
Validates plain object values with empty object factory as default.
178
179
```typescript { .api }
180
/**
181
* Creates a validator for object types
182
* @returns VueTypeValidableDef for object values
183
*/
184
static get object(): VueTypeValidableDef<Record<string, any>>;
185
186
// Standalone function with generic constraint support
187
function object<T extends Record<string, any>>(): VueTypeValidableDef<T>;
188
```
189
190
**Usage Examples:**
191
192
```typescript
193
import VueTypes, { object } from "vue-types";
194
195
// Basic object validation
196
const config = VueTypes.object.def(() => ({}));
197
const user = VueTypes.object.isRequired;
198
199
// With type constraints
200
const settings = object<{ theme: string; lang: string }>().isRequired;
201
```
202
203
### Integer Validator
204
205
Validates integer values using Number type with custom validator for integer checking.
206
207
```typescript { .api }
208
/**
209
* Creates a validator for integer types
210
* @returns VueTypeDef for integer values (includes built-in validator)
211
*/
212
static get integer(): VueTypeDef<number>;
213
214
// Standalone function with generic constraint support
215
function integer<T extends number = number>(): VueTypeDef<T>;
216
```
217
218
**Usage Examples:**
219
220
```typescript
221
import VueTypes, { integer } from "vue-types";
222
223
// Integer validation with default
224
const pageSize = VueTypes.integer.def(10);
225
const currentPage = VueTypes.integer.isRequired;
226
227
// Standalone usage
228
const itemCount = integer().def(0);
229
```
230
231
### Symbol Validator
232
233
Validates symbol values with custom validator function.
234
235
```typescript { .api }
236
/**
237
* Creates a validator for symbol types
238
* @returns VueTypeDef for symbol values
239
*/
240
static get symbol(): VueTypeDef<symbol>;
241
242
// Standalone function
243
function symbol(): VueTypeDef<symbol>;
244
```
245
246
**Usage Examples:**
247
248
```typescript
249
import VueTypes, { symbol } from "vue-types";
250
251
// Symbol validation
252
const key = VueTypes.symbol.isRequired;
253
const identifier = symbol().def(Symbol('default'));
254
```
255
256
### Nullable Validator
257
258
Validates null values specifically, rejecting all other values including undefined.
259
260
```typescript { .api }
261
/**
262
* Creates a validator that only accepts null values
263
* @returns PropOptions for null values
264
*/
265
static get nullable(): PropOptions<null>;
266
267
// Standalone function
268
function nullable(): PropOptions<null>;
269
```
270
271
**Usage Examples:**
272
273
```typescript
274
import VueTypes, { nullable } from "vue-types";
275
276
// Null validation
277
const emptyValue = VueTypes.nullable;
278
const resetValue = nullable();
279
```
280
281
## Common Patterns
282
283
### Chaining Modifiers
284
285
All native validators support method chaining for configuration:
286
287
```typescript
288
// Required validation
289
const requiredString = VueTypes.string.isRequired;
290
291
// Default value
292
const stringWithDefault = VueTypes.string.def('hello');
293
294
// Custom validation (for validable types)
295
const emailString = VueTypes.string.validate((value) => value.includes('@'));
296
297
// Combined modifiers
298
const validatedRequired = VueTypes.string
299
.validate((value) => value.length > 0)
300
.isRequired;
301
```
302
303
### Default Value Factories
304
305
For complex default values, use factory functions to avoid reference sharing:
306
307
```typescript
308
// Arrays and objects should use factories
309
const items = VueTypes.array.def(() => []);
310
const config = VueTypes.object.def(() => ({ theme: 'light' }));
311
312
// Primitives can use direct values
313
const count = VueTypes.number.def(0);
314
const title = VueTypes.string.def('Untitled');
315
```
316
317
### TypeScript Integration
318
319
Native validators provide full TypeScript support:
320
321
```typescript
322
// Generic constraints for more specific types
323
const theme = string<'light' | 'dark'>().def('light');
324
const count = number<1 | 2 | 3 | 4 | 5>().def(1);
325
326
// Function type constraints
327
const handler = func<(event: MouseEvent) => void>().isRequired;
328
```