Skip to content

Commit

Permalink
add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ragokan committed Sep 17, 2024
1 parent de13eaf commit 4dd95b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
60 changes: 60 additions & 0 deletions packages/server/src/validation/validate.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
32 changes: 11 additions & 21 deletions packages/server/src/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,23 @@ export function __validate<T extends BunicornSchema>(
}
return result.data;
}
return __validate(
__rawSchemaWrapper(schema as RawSchema) as unknown as BunicornSchema,
input,
);
return __rawSchemaWrapper(schema).parse(input);
}

function __rawSchemaWrapper<Output>(schema: RawSchema<Output>) {
return {
_parse: (
input: unknown,
): { output?: Output; issues?: FormattedIssue[] } => {
parse: (input: unknown) => {
try {
return {
output: schema(input),
};
return schema(input);
} catch (e: any) {
return {
issues: [
<FormattedIssue>{
message: e.message ?? "Validation error",
validation: e.expected ?? "custom_validation",
path: [e.path ?? "_"],
},
],
};
throw new BunicornValidationError([
<FormattedIssue>{
message: e.message ?? "Validation error",
validation: e.expected ?? "custom_validation",
path: [e.path ?? "_"],
},
]);
}
},
schema: {},
} as unknown as v.BaseSchema<any, any, any>;
};
}

0 comments on commit 4dd95b0

Please sign in to comment.