Replies: 6 comments 2 replies
-
One of the most significant issues with top level transformations is that one can transform object into something else: .build({
input: z.object({}).transform(() => []),
}) It's hard to track in runtime (requires to run those transformations), it breaks the types (augmentation of inputs from middlewares) and violates the core concept of the library — always to operate objects, @rottmann |
Beta Was this translation helpful? Give feedback.
-
That's what I thought too. I use a now a manual transform solution, so it breaks not the types and keeps openapi-docs: import camelize from 'camelize-ts'
import snakify from 'snakify-ts'
//...
export const Input = ... // zod schema
export type Input = Camelize<z.infer<typeof Input>>
export const Output = ... // zod schema
export type Output = Camelize<z.infer<typeof Output>>
//...
handler: async (args) => {
const input = camelize(args.input)
const result = await doSomethingWithCamalcasedData(input)
return snakify(result)
}, |
Beta Was this translation helpful? Give feedback.
-
There is some progress on this problem, @rottmann : I managed to make it work for the input schema. |
Beta Was this translation helpful? Give feedback.
-
The problem with transformations is that their output has no schema. So that roughly it should look this way: originalObjectSchema
.transform( transformer )
.pipe(
z.object(
transformer(originalObjectSchema.shape)
)
) But I'm still struggling to make it work with generic transformers like Moreover, those libraries do deep transformations by default, which is not suitable for transforming the original shape, since it contains schemas in values. Therefore, I'm leaning towards an explicit approach, an object schema where you can define the keys transformation manually. |
Beta Was this translation helpful? Give feedback.
-
Would you be interested in testing the feature? Your feedback on that matter would be very useful. In case you would, please find the documentation and examples here: |
Beta Was this translation helpful? Give feedback.
-
The transform-method with zod is absolutely sufficient for this use case and does the job 👍 |
Beta Was this translation helpful? Give feedback.
-
A REST-API uses normally underscores in keys for input and output data structures.
In the source code, I would like to work with camelCase params.
This post describes it really good: colinhacks/zod#2240 (comment)
Currently, top-level transforms are not allowed.
Input is
Work in src should be
Output should be
The transform should be set with EndpointsFactory, so everybody could transform it into personal style.
Don't know if this is too complicated to implement, or the Typing-Overhead would be too big.
What did you think?
Beta Was this translation helpful? Give feedback.
All reactions