-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini.test.ts
More file actions
443 lines (379 loc) · 13.1 KB
/
Copy pathmini.test.ts
File metadata and controls
443 lines (379 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* Tests for src/mini.ts - Tree-shakable functional API
*/
import {
// Primitive validators
string,
number,
int,
int32,
boolean,
date,
bigint,
symbol,
stringbool,
// Complex validators
array,
object,
strictObject,
looseObject,
tuple,
record,
partialRecord,
looseRecord,
set,
map,
// Union & Intersection
union,
intersection,
discriminatedUnion,
xor,
// Literal & Enum
literal,
enumValidator,
// Special validators
any,
unknown,
voidValidator,
never,
nullValidator,
undefinedValidator,
nan,
// Utility validators
lazy,
json,
// Utility functions
optional,
nullable,
nullish,
preprocess,
// Coercion
coerce,
// NEVER constant
NEVER,
// Type exports
type Infer,
type Input,
type Output,
// Class exports
VldBase,
VldString,
} from '../src/mini';
describe('Mini API', () => {
describe('Primitive Validators', () => {
test('string() creates a string validator', () => {
const schema = string();
expect(schema.parse('hello')).toBe('hello');
expect(() => schema.parse(123)).toThrow();
});
test('number() creates a number validator', () => {
const schema = number();
expect(schema.parse(42)).toBe(42);
expect(() => schema.parse('42')).toThrow();
});
test('int() creates an integer validator', () => {
const schema = int();
expect(schema.parse(42)).toBe(42);
expect(() => schema.parse(42.5)).toThrow();
});
test('int32() creates an int32 validator', () => {
const schema = int32();
expect(schema.parse(42)).toBe(42);
expect(schema.parse(-2147483648)).toBe(-2147483648);
expect(schema.parse(2147483647)).toBe(2147483647);
expect(() => schema.parse(2147483648)).toThrow();
expect(() => schema.parse(-2147483649)).toThrow();
});
test('boolean() creates a boolean validator', () => {
const schema = boolean();
expect(schema.parse(true)).toBe(true);
expect(schema.parse(false)).toBe(false);
expect(() => schema.parse('true')).toThrow();
});
test('date() creates a date validator', () => {
const schema = date();
const now = new Date();
expect(schema.parse(now)).toEqual(now);
// Date validator accepts strings and numbers (passed to Date constructor)
expect(schema.parse('2023-01-01')).toBeInstanceOf(Date);
// Invalid date string should throw
expect(() => schema.parse('not-a-date')).toThrow();
// Non-date types should throw
expect(() => schema.parse(true)).toThrow();
});
test('bigint() creates a bigint validator', () => {
const schema = bigint();
expect(schema.parse(BigInt(123))).toBe(BigInt(123));
expect(() => schema.parse(123)).toThrow();
});
test('symbol() creates a symbol validator', () => {
const schema = symbol();
const sym = Symbol('test');
expect(schema.parse(sym)).toBe(sym);
expect(() => schema.parse('symbol')).toThrow();
});
test('stringbool() creates a string-to-boolean validator', () => {
const schema = stringbool();
expect(schema.parse('true')).toBe(true);
expect(schema.parse('false')).toBe(false);
});
test('stringbool() with custom options', () => {
const schema = stringbool({ truthy: ['yes', 'on'], falsy: ['no', 'off'], caseSensitive: false });
expect(schema.parse('YES')).toBe(true);
expect(schema.parse('no')).toBe(false);
});
});
describe('Complex Validators', () => {
test('array() creates an array validator', () => {
const schema = array(string());
expect(schema.parse(['a', 'b'])).toEqual(['a', 'b']);
expect(() => schema.parse([1, 2])).toThrow();
});
test('object() creates an object validator', () => {
const schema = object({
name: string(),
age: number(),
});
expect(schema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
expect(() => schema.parse({ name: 'John' })).toThrow();
});
test('strictObject() creates a strict object validator', () => {
const schema = strictObject({
name: string(),
});
expect(schema.parse({ name: 'John' })).toEqual({ name: 'John' });
expect(() => schema.parse({ name: 'John', extra: 'field' })).toThrow();
});
test('looseObject() creates a loose object validator', () => {
const schema = looseObject({
name: string(),
});
expect(schema.parse({ name: 'John', extra: 'field' })).toEqual({ name: 'John', extra: 'field' });
});
test('tuple() creates a tuple validator', () => {
const schema = tuple(string(), number());
expect(schema.parse(['hello', 42])).toEqual(['hello', 42]);
expect(() => schema.parse(['hello'])).toThrow();
});
test('record() creates a record validator', () => {
const schema = record(number());
expect(schema.parse({ a: 1, b: 2 })).toEqual({ a: 1, b: 2 });
expect(() => schema.parse({ a: 'one' })).toThrow();
});
test('partialRecord() creates a partial record validator', () => {
const schema = partialRecord(number());
expect(schema.parse({ a: 1, b: undefined })).toEqual({ a: 1, b: undefined });
});
test('looseRecord() creates a loose record validator', () => {
const schema = looseRecord(number());
expect(schema.parse({ a: 1 })).toEqual({ a: 1 });
});
test('set() creates a set validator', () => {
const schema = set(string());
const result = schema.parse(new Set(['a', 'b']));
expect(result).toBeInstanceOf(Set);
expect(result.has('a')).toBe(true);
});
test('map() creates a map validator', () => {
const schema = map(string(), number());
const input = new Map([['a', 1], ['b', 2]]);
const result = schema.parse(input);
expect(result).toBeInstanceOf(Map);
expect(result.get('a')).toBe(1);
});
});
describe('Union & Intersection', () => {
test('union() creates a union validator', () => {
const schema = union(string(), number());
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(42)).toBe(42);
expect(() => schema.parse(true)).toThrow();
});
test('intersection() creates an intersection validator', () => {
const schema = intersection(
object({ name: string() }),
object({ age: number() })
);
expect(schema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
});
test('discriminatedUnion() creates a discriminated union validator', () => {
const schema = discriminatedUnion(
'type',
object({ type: literal('a'), value: string() }),
object({ type: literal('b'), value: number() })
);
expect(schema.parse({ type: 'a', value: 'hello' })).toEqual({ type: 'a', value: 'hello' });
expect(schema.parse({ type: 'b', value: 42 })).toEqual({ type: 'b', value: 42 });
});
test('xor() creates an XOR validator', () => {
const schema = xor(
object({ a: string() }),
object({ b: number() })
);
expect(schema.parse({ a: 'hello' })).toEqual({ a: 'hello' });
expect(schema.parse({ b: 42 })).toEqual({ b: 42 });
});
});
describe('Literal & Enum', () => {
test('literal() creates a literal validator', () => {
const schema = literal('hello');
expect(schema.parse('hello')).toBe('hello');
expect(() => schema.parse('world')).toThrow();
});
test('literal() with different types', () => {
expect(literal(42).parse(42)).toBe(42);
expect(literal(true).parse(true)).toBe(true);
expect(literal(null).parse(null)).toBe(null);
expect(literal(undefined).parse(undefined)).toBe(undefined);
});
test('enumValidator() creates an enum validator', () => {
const schema = enumValidator('a', 'b', 'c');
expect(schema.parse('a')).toBe('a');
expect(schema.parse('b')).toBe('b');
expect(() => schema.parse('d')).toThrow();
});
});
describe('Special Validators', () => {
test('any() creates an any validator', () => {
const schema = any();
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(42)).toBe(42);
expect(schema.parse(null)).toBe(null);
});
test('unknown() creates an unknown validator', () => {
const schema = unknown();
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(42)).toBe(42);
});
test('voidValidator() creates a void validator', () => {
const schema = voidValidator();
expect(schema.parse(undefined)).toBe(undefined);
expect(() => schema.parse('hello')).toThrow();
});
test('never() creates a never validator', () => {
const schema = never();
expect(() => schema.parse('anything')).toThrow();
});
test('nullValidator() creates a null validator', () => {
const schema = nullValidator();
expect(schema.parse(null)).toBe(null);
expect(() => schema.parse(undefined)).toThrow();
});
test('undefinedValidator() creates an undefined validator', () => {
const schema = undefinedValidator();
expect(schema.parse(undefined)).toBe(undefined);
expect(() => schema.parse(null)).toThrow();
});
test('nan() creates a NaN validator', () => {
const schema = nan();
expect(schema.parse(NaN)).toBe(NaN);
expect(() => schema.parse(42)).toThrow();
});
});
describe('Utility Validators', () => {
test('lazy() creates a lazy validator', () => {
// Recursive schema for tree-like structures
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nodeSchema: any = lazy(() => object({
value: string(),
children: optional(array(nodeSchema)),
}));
const result = nodeSchema.parse({ value: 'root', children: [{ value: 'child' }] });
expect(result.value).toBe('root');
});
test('json() creates a JSON validator', () => {
const schema = json();
expect(schema.parse('{"a":1}')).toEqual({ a: 1 });
});
test('json() with schema', () => {
const schema = json(object({ a: number() }));
expect(schema.parse('{"a":1}')).toEqual({ a: 1 });
expect(() => schema.parse('{"a":"one"}')).toThrow();
});
});
describe('Utility Functions', () => {
test('optional() makes a validator optional', () => {
const schema = optional(string());
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(undefined)).toBe(undefined);
});
test('nullable() makes a validator nullable', () => {
const schema = nullable(string());
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(null)).toBe(null);
});
test('nullish() makes a validator nullish', () => {
const schema = nullish(string());
expect(schema.parse('hello')).toBe('hello');
expect(schema.parse(null)).toBe(null);
expect(schema.parse(undefined)).toBe(undefined);
});
test('preprocess() preprocesses input', () => {
const schema = preprocess(
(val) => String(val).trim(),
string()
);
expect(schema.parse(' hello ')).toBe('hello');
});
});
describe('Coercion Validators', () => {
test('coerce.string() coerces to string', () => {
const schema = coerce.string();
expect(schema.parse(42)).toBe('42');
expect(schema.parse(true)).toBe('true');
});
test('coerce.number() coerces to number', () => {
const schema = coerce.number();
expect(schema.parse('42')).toBe(42);
});
test('coerce.boolean() coerces to boolean', () => {
const schema = coerce.boolean();
expect(schema.parse(1)).toBe(true);
expect(schema.parse(0)).toBe(false);
});
test('coerce.date() coerces to date', () => {
const schema = coerce.date();
const result = schema.parse('2023-01-01');
expect(result).toBeInstanceOf(Date);
});
test('coerce.bigint() coerces to bigint', () => {
const schema = coerce.bigint();
expect(schema.parse('123')).toBe(BigInt(123));
});
});
describe('NEVER Constant', () => {
test('NEVER is a never validator instance', () => {
expect(() => NEVER.parse('anything')).toThrow();
});
});
describe('Type Exports', () => {
test('Infer type works correctly', () => {
const schema = object({ name: string(), age: number() });
type SchemaType = Infer<typeof schema>;
const value: SchemaType = { name: 'John', age: 30 };
expect(value.name).toBe('John');
});
test('Input type works correctly', () => {
const schema = string();
type InputType = Input<typeof schema>;
const value: InputType = 'hello';
expect(value).toBe('hello');
});
test('Output type works correctly', () => {
const schema = string();
type OutputType = Output<typeof schema>;
const value: OutputType = 'hello';
expect(value).toBe('hello');
});
});
describe('Class Exports', () => {
test('VldBase is exported', () => {
expect(VldBase).toBeDefined();
});
test('VldString is exported', () => {
expect(VldString).toBeDefined();
const schema = VldString.create();
expect(schema.parse('hello')).toBe('hello');
});
});
});