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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/light-balloons-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tuyau/core': patch
'@tuyau/utils': patch
---

Automatic 422 response if the request input is validated using `validateUsing`.
2 changes: 1 addition & 1 deletion packages/core/src/codegen/api_types_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export class ApiTypesGenerator {
const typeName = this.#generateTypeName(route)
typesByPattern[typeName] = {
request: schemaImport ? `MakeTuyauRequest<${schemaImport}>` : 'unknown',
response: `MakeTuyauResponse<import('${relativePath}').default['${routeHandler.method}']>`,
response: `MakeTuyauResponse<import('${relativePath}').default['${routeHandler.method}'], ${!!schemaImport}>`,
}

currentLevel.$url = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { InferInput } from '@vinejs/vine/types'

type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -25,7 +25,7 @@ import type { InferInput } from '@vinejs/vine/types'

type UsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/validators/get_users_validator.ts')['getUsersValidator']>>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'users': {
Expand Down Expand Up @@ -67,7 +67,7 @@ import type { InferInput } from '@vinejs/vine/types'

type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -89,7 +89,7 @@ import type { InferInput } from '@vinejs/vine/types'

type PostsGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'posts': {
Expand All @@ -111,7 +111,7 @@ import type { InferInput } from '@vinejs/vine/types'

type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -133,7 +133,7 @@ import type { InferInput } from '@vinejs/vine/types'

type PostsGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'posts': {
Expand All @@ -155,7 +155,7 @@ import type { InferInput } from '@vinejs/vine/types'

type UsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/validators/get_users_validator.ts')['default']>>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'users': {
Expand All @@ -177,7 +177,7 @@ import type { InferInput } from '@vinejs/vine/types'

type GetUsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/controllers/get_users_controller.ts')['getUsersValidator']>>
response: MakeTuyauResponse<import('../app/controllers/get_users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/get_users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'get_users': {
Expand Down
10 changes: 7 additions & 3 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ export type MakeOptional<T extends object> = UndefinedProps<T> & Omit<T, keyof U
/**
* Shortcut for computing the Tuyau response type
*/
export type MakeTuyauResponse<T extends (...args: any) => any> = Simplify<
Serialize<ConvertReturnTypeToRecordStatusResponse<Awaited<ReturnType<T>>>>
>
export type MakeTuyauResponse<
T extends (...args: any) => any,
HasSchema extends boolean = false,
> = Simplify<Serialize<ConvertReturnTypeToRecordStatusResponse<Awaited<ReturnType<T>>>>> &
(HasSchema extends true
? { 422: { errors: { message: string; rule: string; field: string }[] } }
: {})

/**
* Shortcut for computing the Tuyau request type
Expand Down