Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): automatic 422 response when using validator #15

Merged
merged 2 commits into from
Nov 4, 2024

Conversation

mrsafalpiya
Copy link
Contributor

It makes sense to add a 422 response automatically if the controller method uses a validator.

@Julien-R44
Copy link
Owner

Julien-R44 commented Nov 2, 2024

Thanks!

I think it would be better to include this type directly in MakeTuyauResponse type

It would be cleaner, rather than polluting the generated file that lives in the user's codebase.

Also, I think we should include 422 only if a call to validateUsing is found in the controller method. This check is already done, as it extracts the type of the request. Here: https://github.com/Julien-R44/tuyau/blob/main/packages/core/src/codegen/api_types_generator.ts#L395

If no validateUsing is found, then logically there will be no 422 return.

@mrsafalpiya
Copy link
Contributor Author

I think it would be better to include this type directly in MakeTuyauResponse type

Yes, it would look clearer, but I can't figure out how this would work. Would you happen to have any pointers you could provide?

Also, I think we should include 422 only if a call to validateUsing is found in the controller method.

I'm indeed checking if validateUsing was used first before adding the 422 signature here: https://github.com/Julien-R44/tuyau/pull/15/files#diff-b22cae109b6d4a97f4e7ba6fa1dec8add7fc4db8c7f2eff710a952c37e655ce0R420 (${schemaImport ? ... : ''})

@Julien-R44
Copy link
Owner

Yes, it would look clearer, but I can't figure out how this would work. Would you happen to have any pointers you could provide?

I think we can just make MakeTuyauResponse accept a second generic HasSchema which is a boolean. If this boolean is true then we include error 422 in the resulting type :

export type MakeTuyauResponse<T extends (...args: any) => any> = Simplify<

I'm indeed checking if validateUsing was used first before adding the 422 signature here: #15 (files) (${schemaImport ? ... : ''})

Ah yes sorry I don't know how I missed this! Thanks ahah

Otherwise, could you please run a pnpm changeset add and add a quick summary of your change? We use this to version packages and generate changelogs.

Thanks!!

@mrsafalpiya
Copy link
Contributor Author

I have added a commit with modified MakeTuyauResponse and generated changelog. Please review it.

@Julien-R44
Copy link
Owner

Looks perfect to me. Thanks a lot dude!

@Julien-R44 Julien-R44 merged commit 7678e36 into Julien-R44:main Nov 4, 2024
3 checks passed
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.

2 participants