-
Notifications
You must be signed in to change notification settings - Fork 59
/
superstruct.ts
69 lines (59 loc) 路 1.2 KB
/
superstruct.ts
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
import {
assert,
boolean,
mask,
number,
object,
string,
type,
} from 'superstruct';
import { addCase } from '../benchmarks';
const dataTypeSafe = type({
number: number(),
negNumber: number(),
maxNumber: number(),
string: string(),
longString: string(),
boolean: boolean(),
deeplyNested: type({
foo: string(),
num: number(),
bool: boolean(),
}),
});
const dataTypeStrict = object({
number: number(),
negNumber: number(),
maxNumber: number(),
string: string(),
longString: string(),
boolean: boolean(),
deeplyNested: object({
foo: string(),
num: number(),
bool: boolean(),
}),
});
addCase(
'superstruct',
'parseSafe',
data => {
assert(data, dataTypeSafe);
return mask(data, dataTypeSafe);
},
// can't get the `mask` stuff to work - its documented to remove any
// additional attributes that `type` ignored
{ disabled: true }
);
addCase('superstruct', 'parseStrict', data => {
assert(data, dataTypeStrict);
return data;
});
addCase('superstruct', 'assertLoose', data => {
assert(data, dataTypeSafe);
return true;
});
addCase('superstruct', 'assertStrict', data => {
assert(data, dataTypeStrict);
return true;
});