@internetderdinge/api 1.229.41 → 1.229.42
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import ApiError from '../utils/ApiError';
|
|
2
2
|
import httpStatus from 'http-status';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
+
const createValidationError = (raw) => new ApiError(httpStatus.BAD_REQUEST, 'Validation error', undefined, undefined, raw);
|
|
4
5
|
export const validateZod = (schema) => (req, res, next) => {
|
|
5
6
|
try {
|
|
6
7
|
schema.body || (schema.body = z.object({}));
|
|
@@ -14,10 +15,7 @@ export const validateZod = (schema) => (req, res, next) => {
|
|
|
14
15
|
};
|
|
15
16
|
// 2) if any failure, short-circuit
|
|
16
17
|
if (!result.body.success || !result.query.success || !result.params.success) {
|
|
17
|
-
|
|
18
|
-
return res.status(400).send(result);
|
|
19
|
-
}
|
|
20
|
-
return next(new ApiError(httpStatus.BAD_REQUEST, 'Validation error'));
|
|
18
|
+
return next(createValidationError(result));
|
|
21
19
|
}
|
|
22
20
|
// 3) merge parsed data back in
|
|
23
21
|
req.body = result.body.data;
|
|
@@ -27,7 +25,7 @@ export const validateZod = (schema) => (req, res, next) => {
|
|
|
27
25
|
}
|
|
28
26
|
catch (err) {
|
|
29
27
|
console.error('Zod validation error:', err);
|
|
30
|
-
return next(
|
|
28
|
+
return next(createValidationError(err));
|
|
31
29
|
}
|
|
32
30
|
};
|
|
33
31
|
export default validateZod;
|
package/package.json
CHANGED
|
@@ -10,6 +10,15 @@ interface Schema {
|
|
|
10
10
|
params?: ZodObject<ZodRawShape>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
const createValidationError = (raw: unknown) =>
|
|
14
|
+
new ApiError(
|
|
15
|
+
httpStatus.BAD_REQUEST,
|
|
16
|
+
'Validation error',
|
|
17
|
+
undefined,
|
|
18
|
+
undefined,
|
|
19
|
+
raw,
|
|
20
|
+
);
|
|
21
|
+
|
|
13
22
|
export const validateZod = (schema: Schema) => (req: Request, res: Response, next: NextFunction) => {
|
|
14
23
|
try {
|
|
15
24
|
schema.body ||= z.object({});
|
|
@@ -25,10 +34,7 @@ export const validateZod = (schema: Schema) => (req: Request, res: Response, nex
|
|
|
25
34
|
|
|
26
35
|
// 2) if any failure, short-circuit
|
|
27
36
|
if (!result.body.success || !result.query.success || !result.params.success) {
|
|
28
|
-
|
|
29
|
-
return res.status(400).send(result);
|
|
30
|
-
}
|
|
31
|
-
return next(new ApiError(httpStatus.BAD_REQUEST, 'Validation error'));
|
|
37
|
+
return next(createValidationError(result));
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
// 3) merge parsed data back in
|
|
@@ -39,15 +45,7 @@ export const validateZod = (schema: Schema) => (req: Request, res: Response, nex
|
|
|
39
45
|
return next();
|
|
40
46
|
} catch (err: any) {
|
|
41
47
|
console.error('Zod validation error:', err);
|
|
42
|
-
return next(
|
|
43
|
-
new ApiError(
|
|
44
|
-
httpStatus.BAD_REQUEST,
|
|
45
|
-
'Validation error',
|
|
46
|
-
undefined,
|
|
47
|
-
undefined,
|
|
48
|
-
process.env.NODE_ENV === 'development' ? err : undefined,
|
|
49
|
-
),
|
|
50
|
-
);
|
|
48
|
+
return next(createValidationError(err));
|
|
51
49
|
}
|
|
52
50
|
};
|
|
53
51
|
|