-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support strictObject (#40)
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
}); | ||
}); | ||
}); |