0
# Built-in Type Validation
1
2
Validation for JavaScript built-in types including Date, Error, RegExp, Promise, Function, and Buffer with specialized validation methods.
3
4
## Capabilities
5
6
### Date Validation
7
8
Validation for Date objects with temporal comparison methods.
9
10
```typescript { .api }
11
/** Date validation predicate */
12
interface DatePredicate extends BasePredicate<Date> {
13
/** Date must be before the specified date */
14
before(date: Date): DatePredicate;
15
16
/** Date must be after the specified date */
17
after(date: Date): DatePredicate;
18
}
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import ow from 'ow';
25
26
const now = new Date();
27
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
28
const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000);
29
30
// Basic date validation
31
ow(now, ow.date);
32
33
// Temporal comparison
34
ow(yesterday, ow.date.before(now));
35
ow(tomorrow, ow.date.after(now));
36
37
// Combined validation
38
ow(now, ow.date.after(yesterday).before(tomorrow));
39
```
40
41
### Error Validation
42
43
Comprehensive validation for Error objects with property checking and built-in error type validation.
44
45
```typescript { .api }
46
/** Error validation predicate */
47
interface ErrorPredicate extends BasePredicate<Error> {
48
// Property validation
49
name(expected: string): ErrorPredicate;
50
message(expected: string): ErrorPredicate;
51
messageIncludes(message: string): ErrorPredicate;
52
hasKeys(...keys: string[]): ErrorPredicate;
53
instanceOf(instance: Function): ErrorPredicate;
54
55
// Built-in error types
56
readonly typeError: ErrorPredicate;
57
readonly evalError: ErrorPredicate;
58
readonly rangeError: ErrorPredicate;
59
readonly referenceError: ErrorPredicate;
60
readonly syntaxError: ErrorPredicate;
61
readonly uriError: ErrorPredicate;
62
}
63
```
64
65
**Property Validation Examples:**
66
67
```typescript
68
import ow from 'ow';
69
70
const error = new Error('Something went wrong');
71
error.code = 'ERR_CUSTOM';
72
error.statusCode = 500;
73
74
// Property validation
75
ow(error, ow.error.name('Error'));
76
ow(error, ow.error.message('Something went wrong'));
77
ow(error, ow.error.messageIncludes('went wrong'));
78
ow(error, ow.error.hasKeys('code', 'statusCode'));
79
80
// Custom error classes
81
class CustomError extends Error {
82
constructor(message: string, public code: string) {
83
super(message);
84
this.name = 'CustomError';
85
}
86
}
87
88
const customError = new CustomError('Custom error', 'ERR_001');
89
ow(customError, ow.error.instanceOf(CustomError));
90
ow(customError, ow.error.name('CustomError'));
91
```
92
93
**Built-in Error Type Examples:**
94
95
```typescript
96
import ow from 'ow';
97
98
// Built-in error types
99
ow(new TypeError('Type error'), ow.error.typeError);
100
ow(new EvalError('Eval error'), ow.error.evalError);
101
ow(new RangeError('Range error'), ow.error.rangeError);
102
ow(new ReferenceError('Reference error'), ow.error.referenceError);
103
ow(new SyntaxError('Syntax error'), ow.error.syntaxError);
104
ow(new URIError('URI error'), ow.error.uriError);
105
106
// Combined validation
107
ow(new TypeError('Invalid type'), ow.error.typeError.messageIncludes('type'));
108
```
109
110
### RegExp Validation
111
112
Validation for regular expression objects.
113
114
```typescript { .api }
115
/** RegExp validation predicate */
116
const regExp: Predicate<RegExp>;
117
```
118
119
**Usage Examples:**
120
121
```typescript
122
import ow from 'ow';
123
124
// Basic RegExp validation
125
ow(/pattern/, ow.regExp);
126
ow(new RegExp('pattern', 'gi'), ow.regExp);
127
128
// With custom validation
129
ow(/^[a-z]+$/i, ow.regExp.is(regex => regex.test('hello')));
130
```
131
132
### Promise Validation
133
134
Validation for Promise objects.
135
136
```typescript { .api }
137
/** Promise validation predicate */
138
const promise: Predicate<Promise<unknown>>;
139
```
140
141
**Usage Examples:**
142
143
```typescript
144
import ow from 'ow';
145
146
// Basic Promise validation
147
ow(Promise.resolve(42), ow.promise);
148
ow(fetch('/api/data'), ow.promise);
149
ow(new Promise(() => {}), ow.promise);
150
151
// Async function return values
152
async function fetchData() {
153
return { data: 'result' };
154
}
155
156
ow(fetchData(), ow.promise);
157
```
158
159
### Function Validation
160
161
Validation for function objects.
162
163
```typescript { .api }
164
/** Function validation predicate */
165
const function: Predicate<Function>;
166
```
167
168
**Usage Examples:**
169
170
```typescript
171
import ow from 'ow';
172
173
// Function validation
174
ow(() => {}, ow.function);
175
ow(function() {}, ow.function);
176
ow(async () => {}, ow.function);
177
ow(Math.max, ow.function);
178
179
// Method validation
180
const obj = {
181
method() { return 'result'; }
182
};
183
184
ow(obj.method, ow.function);
185
186
// Constructor validation
187
ow(Array, ow.function);
188
ow(Date, ow.function);
189
```
190
191
### Buffer Validation
192
193
Validation for Node.js Buffer objects.
194
195
```typescript { .api }
196
/** Buffer validation predicate */
197
const buffer: Predicate<Buffer>;
198
```
199
200
**Usage Examples:**
201
202
```typescript
203
import ow from 'ow';
204
205
// Buffer validation (Node.js only)
206
ow(Buffer.from('hello'), ow.buffer);
207
ow(Buffer.alloc(10), ow.buffer);
208
ow(Buffer.from([1, 2, 3, 4]), ow.buffer);
209
210
// With custom validation
211
ow(Buffer.from('hello'), ow.buffer.is(buf => buf.length === 5));
212
```
213
214
## Advanced Usage Examples
215
216
### Error Handling Patterns
217
218
```typescript
219
import ow from 'ow';
220
221
// Validate error objects in catch blocks
222
function processData(data: unknown) {
223
try {
224
ow(data, ow.object.exactShape({
225
id: ow.number.positive,
226
name: ow.string.nonEmpty
227
}));
228
229
// Process valid data
230
return { success: true, data };
231
} catch (error) {
232
// Validate error type and extract information
233
if (ow.isValid(error, ow.error.name('ArgumentError'))) {
234
return { success: false, error: error.message };
235
}
236
237
// Re-throw unknown errors
238
throw error;
239
}
240
}
241
```
242
243
### Date Range Validation
244
245
```typescript
246
import ow from 'ow';
247
248
// Validate date ranges
249
function validateDateRange(startDate: unknown, endDate: unknown) {
250
ow(startDate, 'startDate', ow.date);
251
ow(endDate, 'endDate', ow.date);
252
253
// Ensure end date is after start date
254
ow(endDate, 'endDate', ow.date.after(startDate as Date));
255
}
256
257
// Usage
258
const start = new Date('2023-01-01');
259
const end = new Date('2023-12-31');
260
validateDateRange(start, end);
261
```
262
263
### Function Parameter Validation
264
265
```typescript
266
import ow from 'ow';
267
268
// Validate function parameters
269
function createValidator(validatorFn: unknown, options: unknown) {
270
ow(validatorFn, 'validatorFn', ow.function);
271
ow(options, 'options', ow.object.partialShape({
272
async: ow.optional.boolean,
273
timeout: ow.optional.number.positive
274
}));
275
276
return {
277
validate: validatorFn as Function,
278
options: options as { async?: boolean; timeout?: number }
279
};
280
}
281
```
282
283
### Promise Chain Validation
284
285
```typescript
286
import ow from 'ow';
287
288
// Validate promise chains
289
async function processAsyncData(fetchPromise: unknown) {
290
ow(fetchPromise, 'fetchPromise', ow.promise);
291
292
const result = await (fetchPromise as Promise<unknown>);
293
294
ow(result, 'fetchResult', ow.object.hasKeys('data', 'status'));
295
296
return result;
297
}
298
```