-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
parser.ts
200 lines (187 loc) · 6.9 KB
/
parser.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
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
import * as z from './types/base';
import { ZodDef } from '.';
import { ZodError } from './ZodError';
function assertNever(x: never): never {
throw ZodError.fromString('Unexpected object: ' + x);
}
export type ParseParams = {
seen: { schema: any; objects: any[] }[];
};
// const seen: any[] = [];
// export const ZodParser = (schemaDef: z.ZodTypeDef) => (obj: any) => ZodParserFactory(schemaDef)(obj, { seen: [] });
export const ZodParser = (schemaDef: z.ZodTypeDef) => (obj: any, params: ParseParams = { seen: [] }) => {
const def: ZodDef = schemaDef as any;
// const { seen } = params;
// const y:ZodObject<any> = "asdf" as any;
// console.log(`visit ${schemaDef.t}: ${typeof obj} - ${obj.name || ''}`);
// if (!['number', 'string', 'boolean', 'undefined'].includes(typeof obj)) {
const schemaSeen = params.seen.find(x => x.schema === schemaDef);
if (schemaSeen) {
if (schemaSeen.objects.indexOf(obj) !== -1) {
console.log(`seen ${typeof obj} before: ${obj.name}`);
return obj;
} else {
schemaSeen.objects.push(obj);
}
} else {
params.seen.push({ schema: schemaDef, objects: [obj] });
}
// }
switch (def.t) {
case z.ZodTypes.string:
if (typeof obj !== 'string') throw ZodError.fromString(`Non-string type: ${typeof obj}`);
return obj as any;
case z.ZodTypes.number:
if (typeof obj !== 'number') throw ZodError.fromString(`Non-number type: ${typeof obj}`);
if (Number.isNaN(obj)) {
throw ZodError.fromString(`Non-number type: NaN`);
}
return obj as any;
case z.ZodTypes.boolean:
if (typeof obj !== 'boolean') throw ZodError.fromString(`Non-boolean type: ${typeof obj}`);
return obj as any;
case z.ZodTypes.undefined:
if (obj !== undefined) throw ZodError.fromString(`Non-undefined type:Found: ${typeof obj}`);
return undefined;
case z.ZodTypes.null:
if (obj !== null) throw ZodError.fromString(`Non-null type: ${typeof obj}`);
return null;
case z.ZodTypes.array:
if (!Array.isArray(obj)) throw ZodError.fromString(`Non-array type: ${typeof obj}`);
const arrayError = ZodError.create([]);
if (def.nonempty === true && obj.length === 0) {
throw ZodError.fromString('Array cannot be empty');
}
const parsedArray = obj.map((item, i) => {
try {
const parsedItem = def.type.parse(item, params);
return parsedItem;
} catch (err) {
arrayError.mergeChild(i, err);
}
});
if (!arrayError.empty) {
// throw ZodError.fromString(arrayErrors.join('\n\n'));
throw arrayError;
}
return parsedArray as any;
case z.ZodTypes.object:
if (typeof obj !== 'object') throw ZodError.fromString(`Non-object type: ${typeof obj}`);
if (Array.isArray(obj)) throw ZodError.fromString(`Non-object type: array`);
const shape = def.shape;
if (def.strict) {
const shapeKeys = Object.keys(def.shape);
const objKeys = Object.keys(obj);
const extraKeys = objKeys.filter(k => shapeKeys.indexOf(k) === -1);
if (extraKeys.length) {
throw ZodError.fromString(`Unexpected key(s) in object: ${extraKeys.map(k => `'${k}'`).join(', ')}`);
}
}
const parsedObject: any = {};
const objectError = ZodError.create([]);
for (const key in shape) {
try {
const parsedEntry = def.shape[key].parse(obj[key], params);
parsedObject[key] = parsedEntry;
} catch (err) {
objectError.mergeChild(key, err);
}
}
if (!objectError.empty) {
throw objectError; //ZodError.fromString(objectErrors.join('\n'));
}
return parsedObject;
case z.ZodTypes.union:
for (const option of def.options) {
try {
return option.parse(obj, params);
// return obj;
} catch (err) {}
}
throw ZodError.fromString(
`Type mismatch in union.\nReceived: ${JSON.stringify(obj, null, 2)}\n\nExpected: ${def.options
.map(x => x._def.t)
.join(' OR ')}`,
);
case z.ZodTypes.intersection:
const errors: string[] = [];
try {
def.left.parse(obj, params);
} catch (err) {
errors.push(`Left side of intersection: ${err.message}`);
}
try {
def.right.parse(obj, params);
} catch (err) {
errors.push(`Right side of intersection: ${err.message}`);
}
if (!errors.length) {
return obj;
}
throw ZodError.fromString(errors.join('\n'));
case z.ZodTypes.tuple:
if (!Array.isArray(obj)) {
// tupleError.addError('','Non-array type detected; invalid tuple.')
throw ZodError.fromString('Non-array type detected; invalid tuple.');
}
if (def.items.length !== obj.length) {
// tupleError.addError('',`Incorrect number of elements in tuple: expected ${def.items.length}, got ${obj.length}`)
throw ZodError.fromString(
`Incorrect number of elements in tuple: expected ${def.items.length}, got ${obj.length}`,
);
}
const tupleError = ZodError.create([]);
const parsedTuple: any[] = [];
for (const index in obj) {
const item = obj[index];
const itemParser = def.items[index];
try {
parsedTuple.push(itemParser.parse(item, params));
} catch (err) {
tupleError.mergeChild(index, err);
}
}
if (!tupleError.empty) {
throw tupleError;
}
return parsedTuple as any;
case z.ZodTypes.lazy:
const lazySchema = def.getter();
return lazySchema.parse(obj, params);
// return obj;
case z.ZodTypes.literal:
// const literalValue = def.value;
// if (typeof literalValue === 'object' && obj !== null) throw ZodError.fromString(`Can't process non-primitive literals.`);
// if (['string','']typeof obj === 'object') throw ZodError.fromString(`Invalid type: ${object}.`);
if (obj === def.value) return obj;
throw ZodError.fromString(`${obj} !== ${def.value}`);
case z.ZodTypes.enum:
if (def.values.indexOf(obj) === -1) {
throw ZodError.fromString(`"${obj}" does not match any value in enum`);
}
return obj;
// case z.ZodTypes.function:
// return obj;
case z.ZodTypes.record:
if (typeof obj !== 'object') throw ZodError.fromString(`Non-object type: ${typeof obj}`);
if (Array.isArray(obj)) throw ZodError.fromString(`Non-object type: array`);
const parsedRecord: any = {};
const recordError = new ZodError();
for (const key in obj) {
try {
parsedRecord[key] = def.valueType.parse(obj[key]);
} catch (err) {
recordError.mergeChild(key, err);
}
}
if (!recordError.empty) throw recordError;
return parsedRecord;
default:
// function
// return obj;
assertNever(def);
// break;
}
// assertNever();
// return obj;
};