Skip to content

feat: support for transformations#102

Open
gjuchault wants to merge 1 commit intoturkerdev:mainfrom
gjuchault:feat/support-for-transformations
Open

feat: support for transformations#102
gjuchault wants to merge 1 commit intoturkerdev:mainfrom
gjuchault:feat/support-for-transformations

Conversation

@gjuchault
Copy link
Copy Markdown

@gjuchault gjuchault commented Sep 18, 2024

This PR aims to solve the case where a zod schema that includes a transform would be provided

Before the changes

The res.send method would expect the z.infer of the schema (ie. the already transformed type), so the developer has to process the transform before (or there would be a typescript error)
The serializerCompiler would parse again the data (and thus try to transform again), which can fail

After the change

When you provide a zod transformation, the type provider now asks the data to be the input (non-transformed), so the serializerCompiler is doing the transformation

Example on the provided test:

On the provided test, we reshape a subkey from a Date to string with toISOString(). Without the PR, res.send expects myDate to be a string, which fails at runtime (the transform throws: String.prototype.toISOString() is not a function).
With the change, res.send expects a Date, which passes at runtime :)

Copy link
Copy Markdown

@rozzilla rozzilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

await app.close();
});

it('returns 200 for correct response', async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your contribution! can you explain the new test? which part of it would be failing before the change in this PR?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR comment is answering this:

On the provided test, we reshape a subkey from a Date to string with toISOString(). Without the PR, res.send expects myDate to be a string, which fails at runtime (the transform throws: String.prototype.toISOString() is not a function).
With the change, res.send expects a Date, which passes at runtime :)

Is there anything that can be made clearer? The input schema has a transform, so the zod input type is a { myDate: Date } and the zod output type { myDate: string }
With this PR, you have to call res.send( with a Date and it correctly uses the transform to generate a string

@gjuchault gjuchault force-pushed the feat/support-for-transformations branch from 1b6db10 to eb015d7 Compare September 26, 2024 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants