From 944986d82411b2486da560781d3b83c3e702efc4 Mon Sep 17 00:00:00 2001 From: mpppk Date: Mon, 16 Sep 2024 15:55:03 +0900 Subject: [PATCH 1/2] Add more test for validate --- src/common/validate.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/common/validate.ts | 15 ++++----------- 2 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/common/validate.test.ts diff --git a/src/common/validate.test.ts b/src/common/validate.test.ts new file mode 100644 index 0000000..8e1f43a --- /dev/null +++ b/src/common/validate.test.ts @@ -0,0 +1,34 @@ +import { describe, it, expect } from "vitest"; +import { getApiSpec } from "./validate"; +import { AnyApiEndpoints } from "./spec"; +describe("getApiSpec", () => { + const spec: AnyApiEndpoints = { + "/users": { + get: { + responses: { 200: { description: "ok" } }, + }, + }, + }; + it("should return path and method if valid path and method provided", () => { + const { data, error } = getApiSpec(spec, "/users", "get"); + expect(data).toEqual({ responses: { 200: { description: "ok" } } }); + expect(error).toBeUndefined(); + }); + it("should return error if invalid path provided", () => { + const { data, error } = getApiSpec(spec, "", "get"); + expect(data).toBeUndefined(); + expect(error).toEqual({ + actual: "", + message: "path does not exist in endpoints", + }); + }); + it("should return error if no exist method provided", () => { + const { data, error } = getApiSpec(spec, "/users", "post"); + expect(data).toBeUndefined(); + expect(error).toEqual({ + actual: "post", + message: "method does not exist in endpoint", + target: "method", + }); + }); +}); diff --git a/src/common/validate.ts b/src/common/validate.ts index 269bb45..1eaad5d 100644 --- a/src/common/validate.ts +++ b/src/common/validate.ts @@ -1,5 +1,5 @@ import { Result } from "../utils"; -import { AnyApiEndpoint, AnyApiEndpoints, isMethod, Method } from "./spec"; +import { AnyApiEndpoint, AnyApiEndpoints, Method } from "./spec"; import { ParsedQs } from "qs"; export type Validators< @@ -57,15 +57,8 @@ const validatePath = ( const validateMethod = ( endpoint: Endpoint, - method: M, + method: M & Method, ): Result => { - if (!isMethod(method)) { - return Result.error({ - target: "method", - actual: method, - message: `invalid method`, - }); - } if (endpoint[method] === undefined) { return Result.error({ target: "method", @@ -79,7 +72,7 @@ const validateMethod = ( const validatePathAndMethod = < E extends AnyApiEndpoints, Path extends string, - M extends string, + M extends string & Method, >( endpoints: E, maybePath: Path, @@ -98,7 +91,7 @@ const validatePathAndMethod = < export const getApiSpec = < E extends AnyApiEndpoints, Path extends string, - M extends string, + M extends string & Method, >( endpoints: E, maybePath: Path, From a3fc9796b904733617a7b4409d10ae95bde80c1b Mon Sep 17 00:00:00 2001 From: mpppk Date: Mon, 16 Sep 2024 15:59:50 +0900 Subject: [PATCH 2/2] Fix --- .idea/typed-api-spec.iml | 1 + src/valibot/index.ts | 13 ++++++++----- src/zod/index.ts | 13 ++++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.idea/typed-api-spec.iml b/.idea/typed-api-spec.iml index d177006..b3e73eb 100644 --- a/.idea/typed-api-spec.iml +++ b/.idea/typed-api-spec.iml @@ -8,6 +8,7 @@ + diff --git a/src/valibot/index.ts b/src/valibot/index.ts index d5c4df6..52e104f 100644 --- a/src/valibot/index.ts +++ b/src/valibot/index.ts @@ -4,6 +4,7 @@ import { BaseApiSpec, DefineApiResponses, DefineResponse, + isMethod, Method, StatusCode, } from "../common"; @@ -93,11 +94,13 @@ export const newValibotValidator = ( return ( input: ValidatorsInput, ) => { - const { data: spec, error } = getApiSpec( - endpoints, - input.path, - input.method?.toLowerCase(), - ); + const method = input.method?.toLowerCase(); + if (!isMethod(method)) { + return {} as E[Path][M] extends ValibotApiSpec + ? ValibotValidators + : Record; + } + const { data: spec, error } = getApiSpec(endpoints, input.path, method); if (error !== undefined) { return {} as E[Path][M] extends ValibotApiSpec ? ValibotValidators diff --git a/src/zod/index.ts b/src/zod/index.ts index c70b27e..5d0f34a 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -3,6 +3,7 @@ import { BaseApiSpec, DefineApiResponses, DefineResponse, + isMethod, Method, StatusCode, } from "../common"; @@ -89,11 +90,13 @@ export const newZodValidator = (endpoints: E) => { return ( input: ValidatorsInput, ) => { - const { data: spec, error } = getApiSpec( - endpoints, - input.path, - input.method?.toLowerCase(), - ); + const method = input.method?.toLowerCase(); + if (!isMethod(method)) { + return {} as E[Path][M] extends ZodApiSpec + ? ZodValidators + : Record; + } + const { data: spec, error } = getApiSpec(endpoints, input.path, method); if (error !== undefined) { return {} as E[Path][M] extends ZodApiSpec ? ZodValidators