-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert zod schema to camel case so parsing works
- Loading branch information
1 parent
21b3489
commit 5653d18
Showing
5 changed files
with
91 additions
and
16 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/node-lib/curriculum-api-2023/helpers/zodToCamelCase.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { z } from "zod"; | ||
|
||
import { zodToCamelCase } from "./zodToCamelCase"; | ||
|
||
import keysToCamelCase from "@/utils/snakeCaseConverter"; | ||
|
||
describe("zodToCamelCase", () => { | ||
it("converts a snake_case schema to camelCase", () => { | ||
const snake_case_schema = z.object({ | ||
test_param: z.string(), | ||
test_param2: z.number(), | ||
}); | ||
|
||
const snake_data = { | ||
test_param: "test", | ||
test_param2: 123, | ||
}; | ||
|
||
expect(snake_case_schema.parse(snake_data)).toEqual(snake_data); | ||
|
||
const camelData = keysToCamelCase(snake_data); | ||
|
||
const camelCaseSchema = zodToCamelCase(snake_case_schema); | ||
|
||
expect(camelCaseSchema.parse(camelData)).toEqual(camelData); | ||
}); | ||
|
||
it("converts a nested snake_case schema to camelCase", () => { | ||
const nested_schema = z.object({ | ||
test_param: z.string(), | ||
nested_param: z.object({ | ||
nested_param_2: z.number(), | ||
}), | ||
}); | ||
|
||
const nested_data = { | ||
test_param: "test", | ||
nested_param: { | ||
nested_param_2: 123, | ||
}, | ||
}; | ||
|
||
expect(nested_schema.parse(nested_data)).toEqual(nested_data); | ||
|
||
const camelData = keysToCamelCase(nested_data); | ||
|
||
const camelCaseSchema = zodToCamelCase(nested_schema); | ||
|
||
expect(camelCaseSchema.parse(camelData)).toEqual(camelData); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/node-lib/curriculum-api-2023/helpers/zodToCamelCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { z } from "zod"; | ||
|
||
import { convertKey } from "@/utils/snakeCaseConverter"; | ||
|
||
// Recursively transform Zod schema keys from snake_case to camelCase | ||
export const zodToCamelCase = <T extends z.ZodTypeAny>( | ||
schema: T, | ||
): z.ZodTypeAny => { | ||
if (!(schema._def.typeName === "ZodObject")) { | ||
throw new Error("zodToCamelCase only works with ZodObject schemas"); | ||
} | ||
const transformedShape: Record<string, z.ZodTypeAny> = {}; | ||
|
||
Object.keys(schema.shape).forEach((key) => { | ||
const camelKey = convertKey(key); | ||
const value = schema.shape[key]; | ||
|
||
// Recursively transform nested schemas | ||
transformedShape[camelKey] = | ||
value instanceof z.ZodObject ? zodToCamelCase(value) : value; | ||
}); | ||
|
||
return z.object(transformedShape); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters