Skip to content

Commit 1ee59d9

Browse files
committed
simplify zod json preprocessor
1 parent 85ac530 commit 1ee59d9

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

lambdas/backend-api/src/__tests__/utils/zod-json-preprocessor.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ describe('parseJsonPreprocessor', () => {
1818
expect(val).toEqual({ name: 'example' });
1919
});
2020

21-
it('should return value when value is not a string', () => {
22-
const addIssue = jest.fn();
23-
24-
const context = mock<z.RefinementCtx>({ addIssue });
25-
26-
const val = parseJsonPreprocessor({}, context);
27-
28-
expect(addIssue).not.toHaveBeenCalled();
29-
30-
expect(val).toEqual({});
31-
});
32-
3321
it('should add error to context when unable to parse JSON', () => {
3422
const addIssue = jest.fn();
3523

lambdas/backend-api/src/utils/zod-json-preprocessor.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { z } from 'zod';
22

33
export const parseJsonPreprocessor = (value: unknown, ctx: z.RefinementCtx) => {
4-
if (typeof value === 'string') {
5-
try {
6-
return JSON.parse(value);
7-
} catch (error) {
8-
ctx.addIssue({
9-
code: z.ZodIssueCode.custom,
10-
message: (error as Error)?.message,
11-
});
12-
}
4+
try {
5+
return JSON.parse(value as string);
6+
} catch (error) {
7+
ctx.addIssue({
8+
code: z.ZodIssueCode.custom,
9+
message: (error as Error)?.message,
10+
});
1311
}
1412

1513
return value;

0 commit comments

Comments
 (0)