barejs 0.1.55 → 0.1.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/index.js +216 -132
- package/dist/types/core/bare.d.ts +2 -2
- package/dist/types/core/context.d.ts +5 -0
- package/dist/types/core/radix.d.ts +3 -5
- package/dist/types/core/router.d.ts +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/security/idempotency.d.ts +6 -1
- package/dist/types/security/store.d.ts +4 -1
- package/dist/types/utils/openapi.d.ts +1 -0
- package/llms.txt +3 -3
- package/package.json +2 -2
- package/src/client/index.ts +41 -13
- package/src/core/bare.ts +316 -95
- package/src/core/config.ts +3 -2
- package/src/core/context.ts +232 -54
- package/src/core/loader.ts +13 -3
- package/src/core/radix.ts +197 -110
- package/src/core/router.ts +39 -12
- package/src/index.ts +2 -2
- package/src/security/auth.ts +98 -9
- package/src/security/cors.ts +59 -9
- package/src/security/csrf.ts +6 -5
- package/src/security/hash.ts +9 -3
- package/src/security/idempotency.ts +169 -52
- package/src/security/rateLimit.ts +15 -5
- package/src/security/resilience.ts +34 -7
- package/src/security/store.ts +52 -8
- package/src/security/timeout.ts +5 -3
- package/src/security/validators.ts +574 -65
- package/src/utils/compress.ts +18 -13
- package/src/utils/openapi.ts +46 -8
- package/src/utils/static.ts +33 -7
|
@@ -62,53 +62,24 @@ export const typebox = (schema: any): Middleware => {
|
|
|
62
62
|
* Native Engine (n)
|
|
63
63
|
*/
|
|
64
64
|
export const native = (schema: any): Middleware => {
|
|
65
|
-
const
|
|
66
|
-
const keys = Object.keys(props);
|
|
67
|
-
const kLen = keys.length;
|
|
68
|
-
const reqSet = Array.isArray(schema.required) ? new Set<string>(schema.required) : null;
|
|
69
|
-
|
|
70
|
-
const matchesNativeType = (val: any, expected: string | undefined): boolean => {
|
|
71
|
-
if (expected === 'array') return Array.isArray(val);
|
|
72
|
-
if (expected === 'integer') return typeof val === 'number' && Number.isInteger(val);
|
|
73
|
-
if (expected === 'object') return val !== null && typeof val === 'object' && !Array.isArray(val);
|
|
74
|
-
if (expected === 'null') return val === null;
|
|
75
|
-
return typeof val === expected;
|
|
76
|
-
};
|
|
65
|
+
const check = compileNativeCheck(schema);
|
|
77
66
|
|
|
78
67
|
const mw: Middleware = async (ctx: any, next?: any) => {
|
|
79
68
|
try {
|
|
80
69
|
const isCtx = ctx instanceof Object && 'req' in ctx;
|
|
81
70
|
const body = isCtx ? await ctx.jsonBody() : await ctx.json();
|
|
82
71
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!reqSet || reqSet.has(k)) {
|
|
92
|
-
return errorJSON({
|
|
93
|
-
status: 'error',
|
|
94
|
-
code: 'TYPE_MISMATCH',
|
|
95
|
-
message: `Field '${k}' must be of type ${props[k]?.type}`,
|
|
96
|
-
field: k
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (!matchesNativeType(body[k], props[k]?.type)) {
|
|
102
|
-
return errorJSON({
|
|
103
|
-
status: 'error',
|
|
104
|
-
code: 'TYPE_MISMATCH',
|
|
105
|
-
message: `Field '${k}' must be of type ${props[k]?.type}`,
|
|
106
|
-
field: k
|
|
107
|
-
});
|
|
108
|
-
}
|
|
72
|
+
const res = check(body);
|
|
73
|
+
if (!res.ok) {
|
|
74
|
+
return errorJSON({
|
|
75
|
+
status: 'error',
|
|
76
|
+
code: res.error?.code || 'TYPE_MISMATCH',
|
|
77
|
+
message: res.error?.message || 'Validation failed',
|
|
78
|
+
...(res.error?.path ? { field: res.error.path } : {})
|
|
79
|
+
});
|
|
109
80
|
}
|
|
110
81
|
|
|
111
|
-
if (isCtx) (ctx as any).body =
|
|
82
|
+
if (isCtx) (ctx as any).body = res.data;
|
|
112
83
|
|
|
113
84
|
return next ? next() : undefined;
|
|
114
85
|
} catch (e: any) {
|
|
@@ -160,9 +131,16 @@ export const zod = (schema: z.ZodSchema): Middleware => {
|
|
|
160
131
|
*/
|
|
161
132
|
export const validate = (schema: any): Middleware => {
|
|
162
133
|
if (schema && '_def' in schema) return zod(schema);
|
|
163
|
-
if (schema && (schema.kind || schema
|
|
134
|
+
if (schema && (schema.kind || schema[Symbol.for('TypeBox.Kind')])) {
|
|
164
135
|
return typebox(schema);
|
|
165
136
|
}
|
|
137
|
+
if (schema && schema.type) {
|
|
138
|
+
try {
|
|
139
|
+
return typebox(schema);
|
|
140
|
+
} catch {
|
|
141
|
+
return native(schema);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
166
144
|
return native(schema);
|
|
167
145
|
};
|
|
168
146
|
|
|
@@ -186,6 +164,279 @@ export interface RouteSchemas {
|
|
|
186
164
|
maxBodySize?: number;
|
|
187
165
|
}
|
|
188
166
|
|
|
167
|
+
const matchesJsonType = (val: any, expected: string | string[] | undefined): boolean => {
|
|
168
|
+
if (expected === undefined) return true;
|
|
169
|
+
if (Array.isArray(expected)) return expected.some((t) => matchesJsonType(val, t));
|
|
170
|
+
if (expected === 'array') return Array.isArray(val);
|
|
171
|
+
if (expected === 'integer') return typeof val === 'number' && Number.isInteger(val);
|
|
172
|
+
if (expected === 'number') return typeof val === 'number' && Number.isFinite(val);
|
|
173
|
+
if (expected === 'object') return val !== null && typeof val === 'object' && !Array.isArray(val);
|
|
174
|
+
if (expected === 'null') return val === null;
|
|
175
|
+
return typeof val === expected;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const jsonDeepEqual = (a: any, b: any): boolean => {
|
|
179
|
+
if (a === b) return true;
|
|
180
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
181
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
182
|
+
if (Array.isArray(a)) {
|
|
183
|
+
if (a.length !== b.length) return false;
|
|
184
|
+
for (let i = 0; i < a.length; i++) if (!jsonDeepEqual(a[i], b[i])) return false;
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
const ka = Object.keys(a);
|
|
188
|
+
if (ka.length !== Object.keys(b).length) return false;
|
|
189
|
+
for (let i = 0; i < ka.length; i++) {
|
|
190
|
+
const k = ka[i]!;
|
|
191
|
+
if (!Object.hasOwn(b, k) || !jsonDeepEqual(a[k], b[k])) return false;
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const validateJsonSchemaNode = (
|
|
197
|
+
val: any,
|
|
198
|
+
schema: any,
|
|
199
|
+
path: string,
|
|
200
|
+
requireAllPropsIfOmitted: boolean
|
|
201
|
+
): { ok: boolean; error?: any } => {
|
|
202
|
+
if (!schema || typeof schema !== 'object') return { ok: true };
|
|
203
|
+
|
|
204
|
+
if (Object.hasOwn(schema, 'const') && !jsonDeepEqual(val, schema.const)) {
|
|
205
|
+
return { ok: false, error: { message: `Value at '${path || 'root'}' must equal ${JSON.stringify(schema.const)}`, path } };
|
|
206
|
+
}
|
|
207
|
+
if (Array.isArray(schema.enum) && !schema.enum.some((e: any) => jsonDeepEqual(e, val))) {
|
|
208
|
+
return { ok: false, error: { message: `Value at '${path || 'root'}' must be one of enum values`, path } };
|
|
209
|
+
}
|
|
210
|
+
if (schema.not && typeof schema.not === 'object' && validateJsonSchemaNode(val, schema.not, path, false).ok) {
|
|
211
|
+
return { ok: false, error: { message: `Value at '${path || 'root'}' must not match the 'not' schema`, path } };
|
|
212
|
+
}
|
|
213
|
+
if (Array.isArray(schema.allOf)) {
|
|
214
|
+
for (let i = 0; i < schema.allOf.length; i++) {
|
|
215
|
+
const r = validateJsonSchemaNode(val, schema.allOf[i], path, false);
|
|
216
|
+
if (!r.ok) return r;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (Array.isArray(schema.anyOf)) {
|
|
220
|
+
let matched = false;
|
|
221
|
+
for (let i = 0; i < schema.anyOf.length; i++) {
|
|
222
|
+
if (validateJsonSchemaNode(val, schema.anyOf[i], path, false).ok) {
|
|
223
|
+
matched = true;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (!matched) {
|
|
228
|
+
return { ok: false, error: { message: `Value at '${path || 'root'}' failed anyOf constraint`, path } };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (Array.isArray(schema.oneOf)) {
|
|
232
|
+
let count = 0;
|
|
233
|
+
for (let i = 0; i < schema.oneOf.length; i++) {
|
|
234
|
+
if (validateJsonSchemaNode(val, schema.oneOf[i], path, false).ok) count++;
|
|
235
|
+
}
|
|
236
|
+
if (count !== 1) {
|
|
237
|
+
return { ok: false, error: { message: `Value at '${path || 'root'}' must match exactly one schema in oneOf`, path } };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (!matchesJsonType(val, schema.type)) {
|
|
242
|
+
return { ok: false, error: { message: path ? `Field '${path}' must be of type ${schema.type}` : `Expected type ${schema.type}`, path } };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (typeof val === 'number') {
|
|
246
|
+
if (typeof schema.minimum === 'number' && val < schema.minimum) {
|
|
247
|
+
return { ok: false, error: { message: `Field '${path}' must be >= ${schema.minimum}`, path } };
|
|
248
|
+
}
|
|
249
|
+
if (typeof schema.maximum === 'number' && val > schema.maximum) {
|
|
250
|
+
return { ok: false, error: { message: `Field '${path}' must be <= ${schema.maximum}`, path } };
|
|
251
|
+
}
|
|
252
|
+
if (typeof schema.multipleOf === 'number' && schema.multipleOf > 0) {
|
|
253
|
+
const q = val / schema.multipleOf;
|
|
254
|
+
if (Math.abs(q - Math.round(q)) > 1e-9 * Math.max(1, Math.abs(q))) {
|
|
255
|
+
return { ok: false, error: { message: `Field '${path}' must be a multiple of ${schema.multipleOf}`, path } };
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (typeof schema.exclusiveMinimum === 'number' && val <= schema.exclusiveMinimum) {
|
|
259
|
+
return { ok: false, error: { message: `Field '${path}' must be > ${schema.exclusiveMinimum}`, path } };
|
|
260
|
+
}
|
|
261
|
+
if (typeof schema.exclusiveMaximum === 'number' && val >= schema.exclusiveMaximum) {
|
|
262
|
+
return { ok: false, error: { message: `Field '${path}' must be < ${schema.exclusiveMaximum}`, path } };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (typeof val === 'string') {
|
|
267
|
+
if (typeof schema.minLength === 'number' && val.length < schema.minLength) {
|
|
268
|
+
return { ok: false, error: { message: `Field '${path}' length must be >= ${schema.minLength}`, path } };
|
|
269
|
+
}
|
|
270
|
+
if (typeof schema.maxLength === 'number' && val.length > schema.maxLength) {
|
|
271
|
+
return { ok: false, error: { message: `Field '${path}' length must be <= ${schema.maxLength}`, path } };
|
|
272
|
+
}
|
|
273
|
+
if (typeof schema.pattern === 'string') {
|
|
274
|
+
try {
|
|
275
|
+
if (!new RegExp(schema.pattern).test(val)) {
|
|
276
|
+
return { ok: false, error: { message: `Field '${path}' does not match pattern`, path } };
|
|
277
|
+
}
|
|
278
|
+
} catch { /* ignore invalid regex */ }
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (Array.isArray(val)) {
|
|
283
|
+
if (typeof schema.minItems === 'number' && val.length < schema.minItems) {
|
|
284
|
+
return { ok: false, error: { message: `Field '${path}' must have at least ${schema.minItems} items`, path } };
|
|
285
|
+
}
|
|
286
|
+
if (typeof schema.maxItems === 'number' && val.length > schema.maxItems) {
|
|
287
|
+
return { ok: false, error: { message: `Field '${path}' must have at most ${schema.maxItems} items`, path } };
|
|
288
|
+
}
|
|
289
|
+
if (schema.uniqueItems === true) {
|
|
290
|
+
for (let i = 1; i < val.length; i++) {
|
|
291
|
+
for (let j = 0; j < i; j++) {
|
|
292
|
+
if (jsonDeepEqual(val[i], val[j])) {
|
|
293
|
+
return { ok: false, error: { message: `Field '${path}' must have unique items`, path } };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (Array.isArray(schema.items)) {
|
|
299
|
+
const n = Math.min(val.length, schema.items.length);
|
|
300
|
+
for (let i = 0; i < n; i++) {
|
|
301
|
+
const r = validateJsonSchemaNode(val[i], schema.items[i], path ? `${path}[${i}]` : `[${i}]`, false);
|
|
302
|
+
if (!r.ok) return r;
|
|
303
|
+
}
|
|
304
|
+
} else if (schema.items && typeof schema.items === 'object') {
|
|
305
|
+
for (let i = 0; i < val.length; i++) {
|
|
306
|
+
const r = validateJsonSchemaNode(val[i], schema.items, path ? `${path}[${i}]` : `[${i}]`, false);
|
|
307
|
+
if (!r.ok) return r;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (val !== null && typeof val === 'object' && !Array.isArray(val)) {
|
|
313
|
+
const props = schema.properties && typeof schema.properties === 'object' ? schema.properties : null;
|
|
314
|
+
const reqList = Array.isArray(schema.required) ? schema.required : null;
|
|
315
|
+
const reqSet = reqList ? new Set<string>(reqList) : null;
|
|
316
|
+
|
|
317
|
+
if (typeof schema.minProperties === 'number' || typeof schema.maxProperties === 'number') {
|
|
318
|
+
const n = Object.keys(val).length;
|
|
319
|
+
if (typeof schema.minProperties === 'number' && n < schema.minProperties) {
|
|
320
|
+
return { ok: false, error: { message: `Object at '${path || 'root'}' must have at least ${schema.minProperties} properties`, path } };
|
|
321
|
+
}
|
|
322
|
+
if (typeof schema.maxProperties === 'number' && n > schema.maxProperties) {
|
|
323
|
+
return { ok: false, error: { message: `Object at '${path || 'root'}' must have at most ${schema.maxProperties} properties`, path } };
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (reqList) {
|
|
328
|
+
for (let i = 0; i < reqList.length; i++) {
|
|
329
|
+
const rk = reqList[i]!;
|
|
330
|
+
if (!Object.hasOwn(val, rk) || val[rk] === undefined) {
|
|
331
|
+
const expectedType = props?.[rk]?.type;
|
|
332
|
+
return {
|
|
333
|
+
ok: false,
|
|
334
|
+
error: {
|
|
335
|
+
message: expectedType ? `Field '${rk}' must be of type ${expectedType}` : `Missing required field '${rk}'`,
|
|
336
|
+
path: path ? `${path}.${rk}` : rk
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (props) {
|
|
344
|
+
const pKeys = Object.keys(props);
|
|
345
|
+
for (let i = 0; i < pKeys.length; i++) {
|
|
346
|
+
const k = pKeys[i]!;
|
|
347
|
+
const hasKey = Object.hasOwn(val, k) && val[k] !== undefined;
|
|
348
|
+
if (!hasKey) {
|
|
349
|
+
if (requireAllPropsIfOmitted && !reqSet) {
|
|
350
|
+
return {
|
|
351
|
+
ok: false,
|
|
352
|
+
error: {
|
|
353
|
+
message: `Field '${k}' must be of type ${props[k]?.type}`,
|
|
354
|
+
path: path ? `${path}.${k}` : k
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const subPath = path ? `${path}.${k}` : k;
|
|
361
|
+
const r = validateJsonSchemaNode(val[k], props[k], subPath, false);
|
|
362
|
+
if (!r.ok) return r;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const matchedPatternKeys = new Set<string>();
|
|
367
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
368
|
+
const valKeys = Object.keys(val);
|
|
369
|
+
for (const [pat, sub] of Object.entries<any>(schema.patternProperties)) {
|
|
370
|
+
let re: RegExp;
|
|
371
|
+
try { re = new RegExp(pat); } catch { continue; }
|
|
372
|
+
for (let i = 0; i < valKeys.length; i++) {
|
|
373
|
+
const vk = valKeys[i]!;
|
|
374
|
+
if (!re.test(vk)) continue;
|
|
375
|
+
matchedPatternKeys.add(vk);
|
|
376
|
+
const r = validateJsonSchemaNode(val[vk], sub, path ? `${path}.${vk}` : vk, false);
|
|
377
|
+
if (!r.ok) return r;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (schema.dependentRequired && typeof schema.dependentRequired === 'object') {
|
|
382
|
+
for (const [dk, deps] of Object.entries<any>(schema.dependentRequired)) {
|
|
383
|
+
if (!Object.hasOwn(val, dk) || !Array.isArray(deps)) continue;
|
|
384
|
+
for (const d of deps) {
|
|
385
|
+
if (!Object.hasOwn(val, d)) {
|
|
386
|
+
return { ok: false, error: { message: `Field '${d}' is required when '${dk}' is present`, path: path ? `${path}.${d}` : d } };
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (schema.additionalProperties === false) {
|
|
393
|
+
const valKeys = Object.keys(val);
|
|
394
|
+
for (let i = 0; i < valKeys.length; i++) {
|
|
395
|
+
const vk = valKeys[i]!;
|
|
396
|
+
if ((!props || !Object.hasOwn(props, vk)) && !matchedPatternKeys.has(vk)) {
|
|
397
|
+
return {
|
|
398
|
+
ok: false,
|
|
399
|
+
error: {
|
|
400
|
+
code: 'ADDITIONAL_PROPERTIES',
|
|
401
|
+
message: `Unknown field '${vk}' is not allowed`,
|
|
402
|
+
path: path ? `${path}.${vk}` : vk
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
} else if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
408
|
+
const valKeys = Object.keys(val);
|
|
409
|
+
for (let i = 0; i < valKeys.length; i++) {
|
|
410
|
+
const vk = valKeys[i]!;
|
|
411
|
+
if ((!props || !Object.hasOwn(props, vk)) && !matchedPatternKeys.has(vk)) {
|
|
412
|
+
const r = validateJsonSchemaNode(val[vk], schema.additionalProperties, path ? `${path}.${vk}` : vk, false);
|
|
413
|
+
if (!r.ok) return r;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return { ok: true };
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const compileNativeCheck = (schema: any): ((val: any) => { ok: boolean; data?: any; error?: any }) => {
|
|
423
|
+
const hasCombinators = Boolean(schema?.anyOf || schema?.oneOf || schema?.allOf);
|
|
424
|
+
const topRequireAll = !hasCombinators && !Array.isArray(schema?.required) && Boolean(schema?.properties);
|
|
425
|
+
const topExpectsObj = !schema?.type || schema.type === 'object';
|
|
426
|
+
return (val: any) => {
|
|
427
|
+
if (!schema || typeof schema !== 'object') {
|
|
428
|
+
return (!val || typeof val !== 'object' || Array.isArray(val))
|
|
429
|
+
? { ok: false, error: { message: 'Invalid JSON object payload' } }
|
|
430
|
+
: { ok: true, data: val };
|
|
431
|
+
}
|
|
432
|
+
if (topExpectsObj && (!val || typeof val !== 'object' || Array.isArray(val)) && !hasCombinators) {
|
|
433
|
+
return { ok: false, error: { message: 'Invalid JSON object payload' } };
|
|
434
|
+
}
|
|
435
|
+
const r = validateJsonSchemaNode(val, schema, '', topRequireAll);
|
|
436
|
+
return r.ok ? { ok: true, data: val } : r;
|
|
437
|
+
};
|
|
438
|
+
};
|
|
439
|
+
|
|
189
440
|
const compileSingleSchema = (schema: any): ((val: any) => { ok: boolean; data?: any; error?: any }) => {
|
|
190
441
|
if (schema && '_def' in schema) {
|
|
191
442
|
return (val: any) => {
|
|
@@ -193,12 +444,54 @@ const compileSingleSchema = (schema: any): ((val: any) => { ok: boolean; data?:
|
|
|
193
444
|
return r.success ? { ok: true, data: r.data } : { ok: false, error: r.error.flatten() };
|
|
194
445
|
};
|
|
195
446
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
447
|
+
try {
|
|
448
|
+
const check = Compiler.TypeCompiler.Compile(schema);
|
|
449
|
+
return (val: any) => {
|
|
450
|
+
if (check.Check(val)) return { ok: true, data: val };
|
|
451
|
+
const first = check.Errors(val).First();
|
|
452
|
+
return { ok: false, error: { message: first?.message || 'Invalid input', path: first?.path } };
|
|
453
|
+
};
|
|
454
|
+
} catch {
|
|
455
|
+
return compileNativeCheck(schema);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const buildArrayProjectorStmts = (
|
|
460
|
+
itemsSchema: any,
|
|
461
|
+
srcArrVar: string,
|
|
462
|
+
dstAssignPrefix: string,
|
|
463
|
+
depth: number,
|
|
464
|
+
idx: number
|
|
465
|
+
): string | null => {
|
|
466
|
+
if (!itemsSchema) return null;
|
|
467
|
+
const arrOut = `ar_${depth}_${idx}`;
|
|
468
|
+
const arrIdx = `j_${depth}_${idx}`;
|
|
469
|
+
const itemVar = `ai_${depth}_${idx}`;
|
|
470
|
+
if (itemsSchema.type === 'array' && itemsSchema.items) {
|
|
471
|
+
const innerStmts = buildArrayProjectorStmts(itemsSchema.items, itemVar, `${arrOut}[${arrIdx}] = `, depth + 1, idx + 1);
|
|
472
|
+
if (innerStmts === null) return null;
|
|
473
|
+
return `if (Array.isArray(${srcArrVar})) {\n` +
|
|
474
|
+
` const ${arrOut} = new Array(${srcArrVar}.length);\n` +
|
|
475
|
+
` for (let ${arrIdx} = 0; ${arrIdx} < ${srcArrVar}.length; ${arrIdx}++) {\n` +
|
|
476
|
+
` const ${itemVar} = ${srcArrVar}[${arrIdx}];\n` +
|
|
477
|
+
` ${innerStmts}\n` +
|
|
478
|
+
` }\n` +
|
|
479
|
+
` ${dstAssignPrefix}${arrOut};\n` +
|
|
480
|
+
`} else {\n ${dstAssignPrefix}${srcArrVar};\n}`;
|
|
481
|
+
}
|
|
482
|
+
const itemDst = `ao_${depth}_${idx}`;
|
|
483
|
+
const itemStmts = buildProjectorStatements(itemsSchema, itemVar, itemDst, depth + 1);
|
|
484
|
+
if (itemStmts === null) return null;
|
|
485
|
+
return `if (Array.isArray(${srcArrVar})) {\n` +
|
|
486
|
+
` const ${arrOut} = new Array(${srcArrVar}.length);\n` +
|
|
487
|
+
` for (let ${arrIdx} = 0; ${arrIdx} < ${srcArrVar}.length; ${arrIdx}++) {\n` +
|
|
488
|
+
` const ${itemVar} = ${srcArrVar}[${arrIdx}];\n` +
|
|
489
|
+
` if (${itemVar} && typeof ${itemVar} === 'object' && !Array.isArray(${itemVar})) {\n` +
|
|
490
|
+
` const ${itemDst} = {};\n${itemStmts} ${arrOut}[${arrIdx}] = ${itemDst};\n` +
|
|
491
|
+
` } else {\n ${arrOut}[${arrIdx}] = ${itemVar};\n }\n` +
|
|
492
|
+
` }\n` +
|
|
493
|
+
` ${dstAssignPrefix}${arrOut};\n` +
|
|
494
|
+
`} else {\n ${dstAssignPrefix}${srcArrVar};\n}`;
|
|
202
495
|
};
|
|
203
496
|
|
|
204
497
|
const buildProjectorStatements = (schema: any, srcVar: string, dstVar: string, depth = 0): string | null => {
|
|
@@ -211,9 +504,18 @@ const buildProjectorStatements = (schema: any, srcVar: string, dstVar: string, d
|
|
|
211
504
|
const keyLit = JSON.stringify(k);
|
|
212
505
|
const valVar = `v_${depth}_${idx++}`;
|
|
213
506
|
const nestedDst = `d_${depth}_${idx}`;
|
|
507
|
+
stmts += `const ${valVar} = Object.hasOwn(${srcVar}, ${keyLit}) ? ${srcVar}[${keyLit}] : undefined;\n`;
|
|
508
|
+
|
|
509
|
+
if (subSchema?.type === 'array' && subSchema?.items) {
|
|
510
|
+
const arrBlock = buildArrayProjectorStmts(subSchema.items, valVar, `${dstVar}[${keyLit}] = `, depth, idx);
|
|
511
|
+
if (arrBlock !== null) {
|
|
512
|
+
stmts += `if (${valVar} !== undefined) {\n${arrBlock}\n}\n`;
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
214
517
|
const nestedStmts = buildProjectorStatements(subSchema, valVar, nestedDst, depth + 1);
|
|
215
|
-
|
|
216
|
-
if (nestedStmts) {
|
|
518
|
+
if (nestedStmts !== null) {
|
|
217
519
|
stmts += `if (${valVar} !== undefined) {\n`;
|
|
218
520
|
stmts += ` if (${valVar} && typeof ${valVar} === 'object' && !Array.isArray(${valVar})) {\n`;
|
|
219
521
|
stmts += ` const ${nestedDst} = {};\n${nestedStmts} ${dstVar}[${keyLit}] = ${nestedDst};\n`;
|
|
@@ -225,11 +527,139 @@ const buildProjectorStatements = (schema: any, srcVar: string, dstVar: string, d
|
|
|
225
527
|
return stmts;
|
|
226
528
|
};
|
|
227
529
|
|
|
530
|
+
const schemaNeedsFallbackProjector = (schema: any): boolean => {
|
|
531
|
+
if (!schema || typeof schema !== 'object') return false;
|
|
532
|
+
if (Array.isArray(schema.anyOf) || Array.isArray(schema.oneOf) || Array.isArray(schema.allOf)) return true;
|
|
533
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') return true;
|
|
534
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') return true;
|
|
535
|
+
if (Array.isArray(schema.items)) return true;
|
|
536
|
+
if (schema.type === 'array' && schema.items) return schemaNeedsFallbackProjector(schema.items);
|
|
537
|
+
if (schema.properties && typeof schema.properties === 'object') {
|
|
538
|
+
for (const sub of Object.values<any>(schema.properties)) {
|
|
539
|
+
if (schemaNeedsFallbackProjector(sub)) return true;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return false;
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
const collectAllOfObjectProps = (schema: any, outProps: Record<string, any>): boolean => {
|
|
546
|
+
let hasProps = false;
|
|
547
|
+
if (schema?.properties && typeof schema.properties === 'object') {
|
|
548
|
+
Object.assign(outProps, schema.properties);
|
|
549
|
+
hasProps = true;
|
|
550
|
+
}
|
|
551
|
+
if (Array.isArray(schema?.allOf)) {
|
|
552
|
+
for (const sub of schema.allOf) {
|
|
553
|
+
if (collectAllOfObjectProps(sub, outProps)) hasProps = true;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return hasProps;
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
const projectWithSchema = (val: any, schema: any): any => {
|
|
560
|
+
if (!schema || typeof schema !== 'object' || val === null || typeof val !== 'object') return val;
|
|
561
|
+
|
|
562
|
+
if (Array.isArray(val)) {
|
|
563
|
+
if (Array.isArray(schema.items)) {
|
|
564
|
+
return val.map((item, i) => (schema.items[i] ? projectWithSchema(item, schema.items[i]) : item));
|
|
565
|
+
}
|
|
566
|
+
if (schema.items && typeof schema.items === 'object') {
|
|
567
|
+
return val.map((item) => projectWithSchema(item, schema.items));
|
|
568
|
+
}
|
|
569
|
+
return val;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const branches = schema.anyOf || schema.oneOf;
|
|
573
|
+
if (Array.isArray(branches) && branches.length > 0) {
|
|
574
|
+
let matchedBranch: any = null;
|
|
575
|
+
for (const b of branches) {
|
|
576
|
+
if (b && typeof b === 'object' && compileSingleSchema(b)(val).ok) {
|
|
577
|
+
matchedBranch = b;
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (matchedBranch) {
|
|
582
|
+
if (schema.properties && typeof schema.properties === 'object') {
|
|
583
|
+
const merged = { ...schema.properties, ...(matchedBranch.properties || {}) };
|
|
584
|
+
const out: Record<string, any> = {};
|
|
585
|
+
for (const [k, sub] of Object.entries<any>(merged)) {
|
|
586
|
+
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
|
|
587
|
+
if (Object.hasOwn(val, k) && val[k] !== undefined) {
|
|
588
|
+
out[k] = projectWithSchema(val[k], sub);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return out;
|
|
592
|
+
}
|
|
593
|
+
return projectWithSchema(val, matchedBranch);
|
|
594
|
+
}
|
|
595
|
+
if (!schema.properties) return val;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (Array.isArray(schema.allOf)) {
|
|
599
|
+
const mergedProps: Record<string, any> = {};
|
|
600
|
+
if (collectAllOfObjectProps(schema, mergedProps)) {
|
|
601
|
+
const out: Record<string, any> = {};
|
|
602
|
+
for (const [k, sub] of Object.entries<any>(mergedProps)) {
|
|
603
|
+
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
|
|
604
|
+
if (Object.hasOwn(val, k) && val[k] !== undefined) {
|
|
605
|
+
out[k] = projectWithSchema(val[k], sub);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return out;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const hasProps = Boolean(schema.properties && typeof schema.properties === 'object');
|
|
613
|
+
const hasPatProps = Boolean(schema.patternProperties && typeof schema.patternProperties === 'object');
|
|
614
|
+
const hasAddProps = Boolean(schema.additionalProperties && typeof schema.additionalProperties === 'object');
|
|
615
|
+
if (hasProps || hasPatProps || hasAddProps) {
|
|
616
|
+
const out: Record<string, any> = {};
|
|
617
|
+
const props = hasProps ? schema.properties : null;
|
|
618
|
+
if (props) {
|
|
619
|
+
for (const [k, sub] of Object.entries<any>(props)) {
|
|
620
|
+
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
|
|
621
|
+
if (Object.hasOwn(val, k) && val[k] !== undefined) {
|
|
622
|
+
out[k] = projectWithSchema(val[k], sub);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (hasPatProps || hasAddProps) {
|
|
627
|
+
const patterns = hasPatProps
|
|
628
|
+
? Object.entries<any>(schema.patternProperties).map(([pat, sub]) => ({ re: new RegExp(pat), sub }))
|
|
629
|
+
: [];
|
|
630
|
+
for (const k of Object.keys(val)) {
|
|
631
|
+
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
|
|
632
|
+
if (val[k] === undefined || (props && Object.hasOwn(props, k))) continue;
|
|
633
|
+
let matched = false;
|
|
634
|
+
for (let i = 0; i < patterns.length; i++) {
|
|
635
|
+
if (patterns[i]!.re.test(k)) {
|
|
636
|
+
out[k] = projectWithSchema(val[k], patterns[i]!.sub);
|
|
637
|
+
matched = true;
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (!matched && hasAddProps) {
|
|
642
|
+
out[k] = projectWithSchema(val[k], schema.additionalProperties);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return out;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return val;
|
|
650
|
+
};
|
|
651
|
+
|
|
228
652
|
const compileProjector = (schema: any): ((val: any) => any) | null => {
|
|
229
653
|
if (!schema || '_def' in schema) return null; // Zod safeParse strips unknown keys natively
|
|
654
|
+
if (schemaNeedsFallbackProjector(schema)) {
|
|
655
|
+
return (val: any) => projectWithSchema(val, schema);
|
|
656
|
+
}
|
|
230
657
|
if (schema.type === 'array' && schema.items) {
|
|
658
|
+
if (schema.items.type === 'array') {
|
|
659
|
+
return (val: any) => projectWithSchema(val, schema);
|
|
660
|
+
}
|
|
231
661
|
const itemStmts = buildProjectorStatements(schema.items, 'item', 'outItem');
|
|
232
|
-
if (
|
|
662
|
+
if (itemStmts === null) return (val: any) => projectWithSchema(val, schema);
|
|
233
663
|
return new Function(
|
|
234
664
|
`return function(arr) {
|
|
235
665
|
if (!Array.isArray(arr)) return arr;
|
|
@@ -250,7 +680,7 @@ const compileProjector = (schema: any): ((val: any) => any) | null => {
|
|
|
250
680
|
)();
|
|
251
681
|
}
|
|
252
682
|
const objStmts = buildProjectorStatements(schema, 'o', 'out');
|
|
253
|
-
if (
|
|
683
|
+
if (objStmts === null) return (val: any) => projectWithSchema(val, schema);
|
|
254
684
|
return new Function(
|
|
255
685
|
`return function(o) {
|
|
256
686
|
if (!o || typeof o !== 'object' || Array.isArray(o)) return o;
|
|
@@ -278,14 +708,27 @@ export const createResponseFilter = (schema: any): ((val: any) => any) => {
|
|
|
278
708
|
|
|
279
709
|
const unwrapPropType = (propSchema: any): string | undefined => {
|
|
280
710
|
let cur = propSchema;
|
|
281
|
-
while (cur?._def
|
|
282
|
-
|
|
711
|
+
while (cur?._def) {
|
|
712
|
+
const tn = cur._def.typeName;
|
|
713
|
+
if (tn === 'ZodOptional' || tn === 'ZodDefault' || tn === 'ZodNullable') {
|
|
714
|
+
cur = cur._def.innerType;
|
|
715
|
+
} else if (tn === 'ZodEffects') {
|
|
716
|
+
cur = cur._def.schema;
|
|
717
|
+
} else {
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
283
720
|
}
|
|
284
721
|
return cur?.type || cur?._def?.typeName;
|
|
285
722
|
};
|
|
286
723
|
|
|
724
|
+
const NUMERIC_RE = /^\s*[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?\s*$/;
|
|
725
|
+
|
|
287
726
|
const coerceFields = (obj: Record<string, any>, schema: any): Record<string, any> => {
|
|
288
|
-
|
|
727
|
+
let cur = schema;
|
|
728
|
+
while (cur?._def?.typeName === 'ZodEffects') {
|
|
729
|
+
cur = cur._def.schema;
|
|
730
|
+
}
|
|
731
|
+
const props = cur?.properties || (cur?._def?.shape ? (typeof cur._def.shape === 'function' ? cur._def.shape() : cur._def.shape) : null);
|
|
289
732
|
if (!props) return obj;
|
|
290
733
|
const out: Record<string, any> = { ...obj };
|
|
291
734
|
for (const [k, propSchema] of Object.entries<any>(props)) {
|
|
@@ -294,7 +737,7 @@ const coerceFields = (obj: Record<string, any>, schema: any): Record<string, any
|
|
|
294
737
|
if (typeof val !== 'string') continue;
|
|
295
738
|
const tType = unwrapPropType(propSchema);
|
|
296
739
|
if (tType === 'number' || tType === 'integer' || tType === 'ZodNumber') {
|
|
297
|
-
if (
|
|
740
|
+
if (/^\s*[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?\s*$/.test(val)) {
|
|
298
741
|
const n = Number(val);
|
|
299
742
|
if (Number.isFinite(n)) out[k] = n;
|
|
300
743
|
}
|
|
@@ -312,8 +755,38 @@ export const schemaValidator = (schemas: RouteSchemas): Middleware => {
|
|
|
312
755
|
const paramsCheck = schemas.params ? compileSingleSchema(schemas.params) : null;
|
|
313
756
|
const headersCheck = schemas.headers ? compileSingleSchema(schemas.headers) : null;
|
|
314
757
|
const bodyProjector = schemas.stripUnknown && schemas.body ? compileProjector(schemas.body) : null;
|
|
758
|
+
const queryProjector = schemas.stripUnknown && schemas.query ? compileProjector(schemas.query) : null;
|
|
759
|
+
const paramsProjector = schemas.stripUnknown && schemas.params ? compileProjector(schemas.params) : null;
|
|
315
760
|
const responseFilter = schemas.response ? createResponseFilter(schemas.response) : null;
|
|
316
761
|
const maxBodySize = schemas.maxBodySize ?? 1048576;
|
|
762
|
+
const queryArrayKeys: string[] = [];
|
|
763
|
+
const queryArrayNumeric = new Set<string>();
|
|
764
|
+
{
|
|
765
|
+
let qs: any = schemas.query;
|
|
766
|
+
while (qs?._def?.schema) qs = qs._def.schema;
|
|
767
|
+
const qProps = qs?.properties || (qs?._def?.shape ? (typeof qs._def.shape === 'function' ? qs._def.shape() : qs._def.shape) : null);
|
|
768
|
+
const unwrapZod = (node: any): any => {
|
|
769
|
+
let cur = node;
|
|
770
|
+
while (cur?._def) {
|
|
771
|
+
const tn = cur._def.typeName;
|
|
772
|
+
if (tn === 'ZodOptional' || tn === 'ZodDefault' || tn === 'ZodNullable') cur = cur._def.innerType;
|
|
773
|
+
else if (tn === 'ZodEffects') cur = cur._def.schema;
|
|
774
|
+
else break;
|
|
775
|
+
}
|
|
776
|
+
return cur;
|
|
777
|
+
};
|
|
778
|
+
if (qProps) {
|
|
779
|
+
for (const [k, rawPs] of Object.entries<any>(qProps)) {
|
|
780
|
+
const ps = unwrapZod(rawPs);
|
|
781
|
+
const isArr = ps?.type === 'array' || ps?._def?.typeName === 'ZodArray';
|
|
782
|
+
if (!isArr) continue;
|
|
783
|
+
queryArrayKeys.push(k);
|
|
784
|
+
const inner = unwrapZod(ps.items ?? ps._def?.type);
|
|
785
|
+
const it = inner?.type ?? inner?._def?.typeName;
|
|
786
|
+
if (it === 'number' || it === 'integer' || it === 'ZodNumber') queryArrayNumeric.add(k);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
317
790
|
const hasSyncParts = Boolean(paramsCheck || queryCheck || headersCheck);
|
|
318
791
|
|
|
319
792
|
const validateSyncParts = (ctx: Context): Response | undefined => {
|
|
@@ -321,17 +794,26 @@ export const schemaValidator = (schemas: RouteSchemas): Middleware => {
|
|
|
321
794
|
const coerced = coerceFields(ctx.params, schemas.params);
|
|
322
795
|
const r = paramsCheck(coerced);
|
|
323
796
|
if (!r.ok) return errorJSON({ status: 'error', code: 'PARAMS_INVALID', error: r.error });
|
|
324
|
-
ctx.params = r.data;
|
|
797
|
+
ctx.params = paramsProjector ? paramsProjector(r.data) : r.data;
|
|
325
798
|
}
|
|
326
799
|
if (queryCheck) {
|
|
327
800
|
const coerced = coerceFields(ctx.query, schemas.query);
|
|
801
|
+
for (const k of queryArrayKeys) {
|
|
802
|
+
const all = ctx.queryAll(k);
|
|
803
|
+
if (all.length > 0) coerced[k] = all.map((v) => (queryArrayNumeric.has(k) && NUMERIC_RE.test(v) ? Number(v) : v));
|
|
804
|
+
}
|
|
328
805
|
const r = queryCheck(coerced);
|
|
329
806
|
if (!r.ok) return errorJSON({ status: 'error', code: 'QUERY_INVALID', error: r.error });
|
|
330
|
-
ctx._query = r.data;
|
|
807
|
+
ctx._query = queryProjector ? queryProjector(r.data) : r.data;
|
|
331
808
|
}
|
|
332
809
|
if (headersCheck) {
|
|
333
810
|
const hdrObj: Record<string, string> = {};
|
|
334
|
-
ctx.req.headers.forEach((v, k) => {
|
|
811
|
+
ctx.req.headers.forEach((v, k) => {
|
|
812
|
+
const lower = k.toLowerCase();
|
|
813
|
+
if (lower !== '__proto__' && lower !== 'constructor' && lower !== 'prototype') {
|
|
814
|
+
hdrObj[lower] = v;
|
|
815
|
+
}
|
|
816
|
+
});
|
|
335
817
|
const r = headersCheck(hdrObj);
|
|
336
818
|
if (!r.ok) return errorJSON({ status: 'error', code: 'HEADERS_INVALID', error: r.error });
|
|
337
819
|
}
|
|
@@ -351,26 +833,53 @@ export const schemaValidator = (schemas: RouteSchemas): Middleware => {
|
|
|
351
833
|
return syncMw;
|
|
352
834
|
}
|
|
353
835
|
|
|
354
|
-
const
|
|
836
|
+
const isZodBody = Boolean(schemas.body && '_def' in schemas.body);
|
|
837
|
+
const finishBodyValidation = (ctx: Context, body: any, next?: Next): any => {
|
|
838
|
+
let r: { ok: boolean; data?: any; error?: any };
|
|
839
|
+
try {
|
|
840
|
+
r = bodyCheck(body);
|
|
841
|
+
} catch {
|
|
842
|
+
if (isZodBody) {
|
|
843
|
+
return schemas.body.safeParseAsync(body).then((zr: any) => {
|
|
844
|
+
if (!zr.success) return errorJSON({ status: 'error', code: 'VALIDATION_FAILED', error: zr.error.flatten() });
|
|
845
|
+
ctx.body = bodyProjector ? bodyProjector(zr.data) : zr.data;
|
|
846
|
+
return next ? next() : undefined;
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
r = { ok: false, error: { message: 'Validation failed' } };
|
|
850
|
+
}
|
|
851
|
+
if (!r.ok) return errorJSON({ status: 'error', code: 'VALIDATION_FAILED', error: r.error });
|
|
852
|
+
ctx.body = bodyProjector ? bodyProjector(r.data) : r.data;
|
|
853
|
+
return next ? next() : undefined;
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
const mw: Middleware = (ctx: Context, next?: Next) => {
|
|
355
857
|
if (hasSyncParts) {
|
|
356
858
|
const errRes = validateSyncParts(ctx);
|
|
357
859
|
if (errRes) return errRes;
|
|
358
860
|
}
|
|
359
861
|
|
|
360
|
-
let
|
|
862
|
+
let bodyOrPromise: any;
|
|
361
863
|
try {
|
|
362
|
-
|
|
864
|
+
bodyOrPromise = (ctx as any).jsonBodySyncOrAsync(maxBodySize);
|
|
363
865
|
} catch (e: any) {
|
|
364
866
|
if (e?.status === 413) {
|
|
365
867
|
return errorJSON({ status: 'error', code: 'PAYLOAD_TOO_LARGE', message: 'Payload Too Large' }, 413);
|
|
366
868
|
}
|
|
367
869
|
return errorJSON({ status: 'error', message: 'Malformed JSON payload' });
|
|
368
870
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
871
|
+
if (bodyOrPromise && typeof bodyOrPromise.then === 'function') {
|
|
872
|
+
return bodyOrPromise.then(
|
|
873
|
+
(body: any) => finishBodyValidation(ctx, body, next),
|
|
874
|
+
(e: any) => {
|
|
875
|
+
if (e?.status === 413) {
|
|
876
|
+
return errorJSON({ status: 'error', code: 'PAYLOAD_TOO_LARGE', message: 'Payload Too Large' }, 413);
|
|
877
|
+
}
|
|
878
|
+
return errorJSON({ status: 'error', message: 'Malformed JSON payload' });
|
|
879
|
+
}
|
|
880
|
+
);
|
|
881
|
+
}
|
|
882
|
+
return finishBodyValidation(ctx, bodyOrPromise, next);
|
|
374
883
|
};
|
|
375
884
|
(mw as any).__schema = schemas.body;
|
|
376
885
|
(mw as any).__routeSchemas = schemas;
|