Skip to content

Commit

Permalink
feat: Add support strictObject (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimame authored Nov 20, 2024
1 parent 20728b4 commit ddf625f
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export function enableTypeCoercion<
return generateReturnSchema(type, tupleWithRestSchema);
}
case "loose_object":
case "strict_object":
case "object": {
const objectSchema = {
...originalSchema,
Expand Down
72 changes: 72 additions & 0 deletions tests/coercion/schema/strictObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { check, forward, number, pipe, strictObject, string } from "valibot";
import { describe, expect, test } from "vitest";
import { parseWithValibot } from "../../../parse";
import { createFormData } from "../../helpers/FormData";

describe("strictObject", () => {
test("should pass only strict objects", () => {
const schema1 = strictObject({ key1: string(), key2: number() });
const input1 = createFormData("key1", "test");
input1.append("key2", "123");
const output1 = parseWithValibot(input1, { schema: schema1 });
expect(output1).toMatchObject({
status: "success",
value: { key1: "test", key2: 123 },
});

input1.append("key3", "");
const output2 = parseWithValibot(input1, { schema: schema1 });
expect(output2).toMatchObject({
error: {
key3: expect.anything(),
},
});

const input2 = createFormData("key1", "");
input2.append("key2", "123");
const output3 = parseWithValibot(input2, { schema: schema1 });
expect(output3).toMatchObject({
error: {
key1: expect.anything(),
},
});

const input3 = createFormData("key1", "string");
input3.set("key2", "non number");
const output4 = parseWithValibot(input3, { schema: schema1 });
expect(output4).toMatchObject({
error: {
key2: expect.anything(),
},
});
});

test("should pass objects with pipe", () => {
const schema = pipe(
strictObject({
key: string(),
}),
forward(
check(({ key }) => key !== "error name", "key is error"),
["key"],
),
);

const output = parseWithValibot(createFormData("key", "valid"), {
schema,
});
expect(output).toMatchObject({
status: "success",
value: { key: "valid" },
});

const errorOutput = parseWithValibot(createFormData("key", "error name"), {
schema,
});
expect(errorOutput).toMatchObject({
error: {
key: expect.anything(),
},
});
});
});
82 changes: 82 additions & 0 deletions tests/coercion/schema/strictObjectAsync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
checkAsync,
forwardAsync,
number,
pipeAsync,
strictObjectAsync,
string,
} from "valibot";
import { describe, expect, test } from "vitest";
import { parseWithValibot } from "../../../parse";
import { createFormData } from "../../helpers/FormData";

describe("strictObjectAsync", () => {
test("should pass only strict objects", async () => {
const schema1 = strictObjectAsync({ key1: string(), key2: number() });
const input1 = createFormData("key1", "test");
input1.append("key2", "123");
const output1 = await parseWithValibot(input1, { schema: schema1 });
expect(output1).toMatchObject({
status: "success",
value: { key1: "test", key2: 123 },
});

input1.append("key3", "");
const output2 = await parseWithValibot(input1, { schema: schema1 });
expect(output2).toMatchObject({
error: {
key3: expect.anything(),
},
});

const input2 = createFormData("key1", "");
input2.append("key2", "123");
const output3 = await parseWithValibot(input2, { schema: schema1 });
expect(output3).toMatchObject({
error: {
key1: expect.anything(),
},
});

const input3 = createFormData("key1", "string");
input3.set("key2", "non number");
const output4 = await parseWithValibot(input3, { schema: schema1 });
expect(output4).toMatchObject({
error: {
key2: expect.anything(),
},
});
});

test("should pass objects with pipe", async () => {
const schema = pipeAsync(
strictObjectAsync({
key: string(),
}),
forwardAsync(
checkAsync(async ({ key }) => key !== "error name", "key is error"),
["key"],
),
);

const output = await parseWithValibot(createFormData("key", "valid"), {
schema,
});
expect(output).toMatchObject({
status: "success",
value: { key: "valid" },
});

const errorOutput = await parseWithValibot(
createFormData("key", "error name"),
{
schema,
},
);
expect(errorOutput).toMatchObject({
error: {
key: expect.anything(),
},
});
});
});

0 comments on commit ddf625f

Please sign in to comment.