diff --git a/packages/server/src/validation/validate.test.ts b/packages/server/src/validation/validate.test.ts new file mode 100644 index 0000000..0b07ce3 --- /dev/null +++ b/packages/server/src/validation/validate.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from "bun:test"; +import * as v from "valibot"; +import * as z from "zod"; +import { BunicornValidationError } from "../error/index.ts"; +import { __validate } from "./validate.ts"; + +// Mock schema for Valibot +const valibotSchema = v.string(); + +// Mock schema for Zod +const zodSchema = z.string(); + +describe("__validate", () => { + it("should validate using Valibot schema", () => { + const input = "valid string"; + const result = __validate(valibotSchema, input); + expect(result).toBe(input); + }); + + it("should throw BunicornValidationError for invalid Valibot input", () => { + const input = 123; + expect(() => __validate(valibotSchema, input)).toThrow( + BunicornValidationError, + ); + }); + + it("should validate using Zod schema", () => { + const input = "valid string"; + const result = __validate(zodSchema, input); + expect(result).toBe(input); + }); + + it("should throw BunicornValidationError for invalid Zod input", () => { + const input = 123; + expect(() => __validate(zodSchema, input)).toThrow(BunicornValidationError); + }); + + it("should validate using raw schema", () => { + const rawSchema = (input: unknown) => { + if (typeof input !== "string") { + throw new Error("Must be a string"); + } + return input; + }; + const input = "valid string"; + const result = __validate(rawSchema, input); + expect(result).toBe(input); + }); + + it("should throw BunicornValidationError for invalid raw schema input", () => { + const rawSchema = (input: unknown) => { + if (typeof input !== "string") { + throw new Error("Must be a string"); + } + return input; + }; + const input = 123; + expect(() => __validate(rawSchema, input)).toThrow(BunicornValidationError); + }); +}); diff --git a/packages/server/src/validation/validate.ts b/packages/server/src/validation/validate.ts index 824aaf3..8a361d8 100644 --- a/packages/server/src/validation/validate.ts +++ b/packages/server/src/validation/validate.ts @@ -28,33 +28,23 @@ export function __validate( } return result.data; } - return __validate( - __rawSchemaWrapper(schema as RawSchema) as unknown as BunicornSchema, - input, - ); + return __rawSchemaWrapper(schema).parse(input); } function __rawSchemaWrapper(schema: RawSchema) { return { - _parse: ( - input: unknown, - ): { output?: Output; issues?: FormattedIssue[] } => { + parse: (input: unknown) => { try { - return { - output: schema(input), - }; + return schema(input); } catch (e: any) { - return { - issues: [ - { - message: e.message ?? "Validation error", - validation: e.expected ?? "custom_validation", - path: [e.path ?? "_"], - }, - ], - }; + throw new BunicornValidationError([ + { + message: e.message ?? "Validation error", + validation: e.expected ?? "custom_validation", + path: [e.path ?? "_"], + }, + ]); } }, - schema: {}, - } as unknown as v.BaseSchema; + }; }