-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
144 lines (135 loc) · 4.31 KB
/
index.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
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
import {ObjectMembers, type Shape, TupleElementType, Types} from './types';
class ParserError extends Error {}
function fail(path: string, expected: string, data: unknown) {
return new ParserError(`Expected ${path} to be ${expected}, got ${data}`);
}
function validate(data: unknown, root: Shape) {
if(root == null) throw new Error('Please ensure that the ts-safe-cast transformer is run.');
function walk(data: unknown, shape: Shape, path: string): ParserError | undefined {
switch(shape) {
case Types.String: {
return typeof data === 'string' ? undefined : fail(path, 'a string', data);
}
case Types.Number: {
return typeof data === 'number' ? undefined : fail(path, 'a number', data);
}
case Types.Boolean: {
return typeof data === 'boolean' ? undefined : fail(path, 'a boolean', data);
}
case Types.Unknown: {
return;
}
case Types.Bigint: {
return typeof data === 'bigint' ? undefined : fail(path, 'a bigint', data);
}
case Types.Date: {
return data instanceof Date ? undefined : fail(path, 'a date', data);
}
}
switch(shape[0]) {
case Types.Object: {
if(!data || typeof data !== 'object') return fail(path, 'an object', data);
let object = data as Record<string, unknown>;
const [_, ...properties] = shape;
for(const property of properties) {
switch(property[0]) {
case ObjectMembers.Property:
const key = property[1];
const newPath = `${path}.${key}`;
if(!(key in object)) {
if(property[3]) break;
return fail(newPath, 'defined', 'undefined');
}
const error = walk(object[key], property[2], newPath);
if(error) return error;
break;
case ObjectMembers.IndexSignature:
for(const key in object) {
const error = walk(object[key], property[1], `${path}.${key}`);
if(error) return error;
}
}
}
return;
}
case Types.Array: {
if(!Array.isArray(data)) return fail(path, 'an array', data);
for(let i = 0; i < data.length; ++i) {
const error = walk(data[i], shape[1], `${path}.${i}`);
if(error) return error;
}
return;
}
case Types.Tuple: {
const [_, ...items] = shape;
if(!Array.isArray(data)) return fail(path, `a tuple`, data);
let min = 0, max = 0;
for(const [shape, type] of items) {
if(type) {
for(; max < data.length;) {
const error = walk(data[max], shape, `${path}.${max}`);
if(!error) ++max;
if(error || type === TupleElementType.Optional) break;
}
} else {
for(;;) {
const error = walk(data[min], shape, `${path}.${min}`);
++min;
if(!error) {
if(min >= max) max = min;
break;
}
else if(min > max) return error;
}
}
}
if(max < data.length) return fail(path, `a tuple`, data);
return;
}
case Types.Literal: {
return data === shape[1] ? undefined : fail(path, `${shape[1]}`, data);
}
case Types.Union: {
const [_, ...options] = shape;
const errors: ParserError[] = [];
for(const option of options) {
const error = walk(data, option, path);
if(!error) return;
errors.push(error);
}
return new ParserError(`Expected ${path} to be a union, but no branches matched: ${errors.map(error => `\n\t• ${error}`).join('')}`);
}
case Types.Intersection: {
const [_, ...members] = shape;
for(const member of members) {
const error = walk(data, member, path);
if(error) return error;
}
return;
}
case Types.Reference: {
const [_, ...rootPath] = shape;
let target = root;
for(let i = 0; i < rootPath.length; ++i)
// @ts-expect-error This is unsafe, but we trust the transformer here.
target = target[rootPath[i]];
return walk(data, target, path);
}
}
}
return walk(data, root, 'root');
}
export function is<T>(data: unknown, shape?: never): data is T {
return !validate(data, shape as unknown as Shape);
}
export function createIs<T>(shape?: never): (data: unknown) => data is T {
return (data): data is T => is(data, shape);
}
export function cast<T>(data: unknown, shape?: never): T {
const error = validate(data, shape as unknown as Shape);
if(error) throw error;
return data as T;
}
export function createCast<T>(shape?: never): (data: unknown) => T {
return (data) => cast(data, shape);
}