From 6bee574ca35cc98646be2a63ca77053c633f8f59 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 3 Jan 2023 09:09:04 +0900 Subject: [PATCH] refactor(validator): show type error if get/head with form/json (#772) --- .eslintrc.cjs | 1 + deno_dist/middleware/validator/index.ts | 14 ++++++++++---- src/middleware/validator/index.ts | 14 ++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index d52c820e..00462e79 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -38,6 +38,7 @@ module.exports = defineConfig({ { types: { Function: false, + '{}': false, }, }, ], diff --git a/deno_dist/middleware/validator/index.ts b/deno_dist/middleware/validator/index.ts index b08511a0..eb55c0b2 100644 --- a/deno_dist/middleware/validator/index.ts +++ b/deno_dist/middleware/validator/index.ts @@ -7,17 +7,22 @@ type ValidatorHandler, R extends Route = Route, I next: Next ) => Promise | Response +type ValidationTypeKeysWithBody = 'form' | 'json' +type ValidationTypeByMethod = M extends 'get' | 'head' // GET and HEAD request must not have a body content. + ? Exclude + : keyof ValidationTypes + export const validator = < T, - U extends keyof ValidationTypes, + Method extends string, + U extends ValidationTypeByMethod, V extends { type: U; data: T }, - // eslint-disable-next-line @typescript-eslint/ban-types V2 = {}, E extends Partial = Environment >( type: U, validationFunc: (value: ValidationTypes[U], c: Context) => T | Response | Promise -): ValidatorHandler => { +): ValidatorHandler => { return async (c, next) => { let value = {} @@ -47,7 +52,8 @@ export const validator = < break } - const res = validationFunc(value, c) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const res = validationFunc(value, c as any) if (res instanceof Response || res instanceof Promise) { return res diff --git a/src/middleware/validator/index.ts b/src/middleware/validator/index.ts index e8decd8f..208621b0 100644 --- a/src/middleware/validator/index.ts +++ b/src/middleware/validator/index.ts @@ -7,17 +7,22 @@ type ValidatorHandler, R extends Route = Route, I next: Next ) => Promise | Response +type ValidationTypeKeysWithBody = 'form' | 'json' +type ValidationTypeByMethod = M extends 'get' | 'head' // GET and HEAD request must not have a body content. + ? Exclude + : keyof ValidationTypes + export const validator = < T, - U extends keyof ValidationTypes, + Method extends string, + U extends ValidationTypeByMethod, V extends { type: U; data: T }, - // eslint-disable-next-line @typescript-eslint/ban-types V2 = {}, E extends Partial = Environment >( type: U, validationFunc: (value: ValidationTypes[U], c: Context) => T | Response | Promise -): ValidatorHandler => { +): ValidatorHandler => { return async (c, next) => { let value = {} @@ -47,7 +52,8 @@ export const validator = < break } - const res = validationFunc(value, c) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const res = validationFunc(value, c as any) if (res instanceof Response || res instanceof Promise) { return res