-
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.
- Loading branch information
Showing
12 changed files
with
874 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { | ||
literal, | ||
objectAsync, | ||
string, | ||
intersectAsync, | ||
checkAsync, | ||
pipeAsync, | ||
} from "valibot"; | ||
import { parseWithValibot } from "../../../parse"; | ||
import { createFormData } from "../../helpers/FormData"; | ||
|
||
describe("intersectAsync", () => { | ||
test("should pass only intersect values", async () => { | ||
const schema1 = objectAsync({ | ||
intersect: intersectAsync([string(), literal("test")]), | ||
}); | ||
const input1 = createFormData("intersect", "test"); | ||
const output1 = await parseWithValibot(input1, { schema: schema1 }); | ||
expect(output1).toMatchObject({ | ||
status: "success", | ||
value: { intersect: "test" }, | ||
}); | ||
|
||
const errorInput1 = createFormData("intersect", "foo"); | ||
const errorOutput1 = await parseWithValibot(errorInput1, { | ||
schema: schema1, | ||
}); | ||
expect(errorOutput1).toMatchObject({ | ||
error: { | ||
intersect: ['Invalid type: Expected "test" but received "foo"'], | ||
}, | ||
}); | ||
const errorInput2 = createFormData("intersect", ""); | ||
const errorOutput2 = await parseWithValibot(errorInput2, { | ||
schema: schema1, | ||
}); | ||
expect(errorOutput2).toMatchObject({ | ||
error: { | ||
intersect: [ | ||
"Invalid type: Expected string but received undefined", | ||
'Invalid type: Expected "test" but received undefined', | ||
], | ||
}, | ||
}); | ||
|
||
const schema2 = intersectAsync([ | ||
objectAsync({ foo: string() }), | ||
objectAsync({ bar: string() }), | ||
]); | ||
const input2 = createFormData("foo", "test"); | ||
input2.append("bar", "test"); | ||
const output2 = await parseWithValibot(input2, { schema: schema2 }); | ||
expect(output2).toMatchObject({ | ||
status: "success", | ||
value: { foo: "test", bar: "test" }, | ||
}); | ||
const errorInput3 = createFormData("foo", "test"); | ||
const errorOutput3 = await parseWithValibot(errorInput3, { | ||
schema: schema2, | ||
}); | ||
expect(errorOutput3).toMatchObject({ | ||
error: { bar: ["Invalid type: Expected string but received undefined"] }, | ||
}); | ||
const errorInput4 = createFormData("bar", "test"); | ||
const errorOutput4 = await parseWithValibot(errorInput4, { | ||
schema: schema2, | ||
}); | ||
expect(errorOutput4).toMatchObject({ | ||
error: { foo: ["Invalid type: Expected string but received undefined"] }, | ||
}); | ||
}); | ||
|
||
test("should pass only intersect values with pipe", async () => { | ||
const schema = objectAsync({ | ||
intersect: pipeAsync( | ||
intersectAsync([string()]), | ||
checkAsync( | ||
async (value) => value === "test", | ||
"intersect must be equal to test", | ||
), | ||
), | ||
}); | ||
const input1 = createFormData("intersect", "test"); | ||
const output1 = await parseWithValibot(input1, { schema }); | ||
expect(output1).toMatchObject({ | ||
status: "success", | ||
value: { intersect: "test" }, | ||
}); | ||
|
||
const errorInput1 = createFormData("intersect", "foo"); | ||
const errorOutput1 = await parseWithValibot(errorInput1, { schema }); | ||
expect(errorOutput1).toMatchObject({ | ||
error: { | ||
intersect: ["intersect must be equal to test"], | ||
}, | ||
}); | ||
}); | ||
|
||
test("should throw only first issue", async () => { | ||
const schema = objectAsync({ | ||
intersect: intersectAsync([string(), literal("test")]), | ||
}); | ||
const errorInput = createFormData("intersect", ""); | ||
const info = { abortEarly: true }; | ||
const errorOutput = await parseWithValibot(errorInput, { schema, info }); | ||
expect(errorOutput).toMatchObject({ | ||
error: { | ||
intersect: ["Invalid type: Expected string but received undefined"], | ||
}, | ||
}); | ||
}); | ||
}); |
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,105 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { parseWithValibot } from "../../../parse"; | ||
import { | ||
objectAsync, | ||
number, | ||
optionalAsync, | ||
nonNullishAsync, | ||
unionAsync, | ||
undefined_, | ||
checkAsync, | ||
pipeAsync, | ||
} from "valibot"; | ||
import { createFormData } from "../../helpers/FormData"; | ||
|
||
describe("nonOptionalAsync", () => { | ||
test("should not pass undefined", async () => { | ||
const schema1 = objectAsync({ | ||
item: nonNullishAsync(optionalAsync(number())), | ||
}); | ||
const input1 = createFormData("item", "1"); | ||
const output1 = await parseWithValibot(input1, { schema: schema1 }); | ||
expect(output1).toMatchObject({ status: "success", value: { item: 1 } }); | ||
expect( | ||
await parseWithValibot(createFormData("item", "non Number"), { | ||
schema: schema1, | ||
}), | ||
).toMatchObject({ | ||
error: { item: ["Invalid type: Expected number but received NaN"] }, | ||
}); | ||
expect( | ||
await parseWithValibot(createFormData("item2", "non Param"), { | ||
schema: schema1, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: [ | ||
"Invalid type: Expected !null & !undefined but received undefined", | ||
], | ||
}, | ||
}); | ||
|
||
const schema2 = objectAsync({ | ||
item: nonNullishAsync(unionAsync([number(), undefined_()])), | ||
}); | ||
const output2 = await parseWithValibot(input1, { schema: schema2 }); | ||
expect(output2).toMatchObject({ status: "success", value: { item: 1 } }); | ||
expect( | ||
await parseWithValibot(createFormData("item", "non Number"), { | ||
schema: schema2, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: [ | ||
'Invalid type: Expected number | undefined but received "non Number"', | ||
], | ||
}, | ||
}); | ||
expect( | ||
await parseWithValibot(createFormData("item2", "non Param"), { | ||
schema: schema2, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: [ | ||
"Invalid type: Expected !null & !undefined but received undefined", | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
test("should pass nonNullish with pipe", async () => { | ||
const schema = objectAsync({ | ||
age: pipeAsync( | ||
nonNullishAsync(optionalAsync(number())), | ||
checkAsync((value) => value > 0, "age must be greater than 0"), | ||
), | ||
}); | ||
|
||
const output1 = await parseWithValibot(createFormData("age", ""), { | ||
schema, | ||
}); | ||
expect(output1).toMatchObject({ | ||
error: { | ||
age: [ | ||
"Invalid type: Expected !null & !undefined but received undefined", | ||
], | ||
}, | ||
}); | ||
|
||
const output2 = await parseWithValibot(createFormData("age", "20"), { | ||
schema, | ||
}); | ||
expect(output2).toMatchObject({ | ||
status: "success", | ||
value: { age: 20 }, | ||
}); | ||
|
||
const errorOutput = await parseWithValibot(createFormData("age", "0"), { | ||
schema, | ||
}); | ||
expect(errorOutput).toMatchObject({ | ||
error: { age: ["age must be greater than 0"] }, | ||
}); | ||
}); | ||
}); |
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,96 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { parseWithValibot } from "../../../parse"; | ||
import { | ||
objectAsync, | ||
number, | ||
optionalAsync, | ||
nonOptionalAsync, | ||
unionAsync, | ||
undefined_, | ||
checkAsync, | ||
pipeAsync, | ||
} from "valibot"; | ||
import { createFormData } from "../../helpers/FormData"; | ||
|
||
describe("nonOptionalAsync", () => { | ||
test("should not pass undefined", async () => { | ||
const schema1 = objectAsync({ | ||
item: nonOptionalAsync(optionalAsync(number())), | ||
}); | ||
const input1 = createFormData("item", "1"); | ||
const output1 = await parseWithValibot(input1, { schema: schema1 }); | ||
expect(output1).toMatchObject({ status: "success", value: { item: 1 } }); | ||
expect( | ||
await parseWithValibot(createFormData("item", "non Number"), { | ||
schema: schema1, | ||
}), | ||
).toMatchObject({ | ||
error: { item: ["Invalid type: Expected number but received NaN"] }, | ||
}); | ||
expect( | ||
await parseWithValibot(createFormData("item2", "non Param"), { | ||
schema: schema1, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: ["Invalid type: Expected !undefined but received undefined"], | ||
}, | ||
}); | ||
|
||
const schema2 = objectAsync({ | ||
item: nonOptionalAsync(unionAsync([number(), undefined_()])), | ||
}); | ||
const output2 = await parseWithValibot(input1, { schema: schema2 }); | ||
expect(output2).toMatchObject({ status: "success", value: { item: 1 } }); | ||
expect( | ||
await parseWithValibot(createFormData("item", "non Number"), { | ||
schema: schema2, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: [ | ||
'Invalid type: Expected number | undefined but received "non Number"', | ||
], | ||
}, | ||
}); | ||
expect( | ||
await parseWithValibot(createFormData("item2", "non Param"), { | ||
schema: schema2, | ||
}), | ||
).toMatchObject({ | ||
error: { | ||
item: ["Invalid type: Expected !undefined but received undefined"], | ||
}, | ||
}); | ||
}); | ||
|
||
test("should pass nonOptional with pipe", async () => { | ||
const schema = objectAsync({ | ||
age: pipeAsync( | ||
nonOptionalAsync(optionalAsync(number())), | ||
checkAsync(async (value) => value > 0, "age must be greater than 0"), | ||
), | ||
}); | ||
|
||
const output1 = await parseWithValibot(createFormData("age", "1"), { | ||
schema, | ||
}); | ||
expect(output1).toMatchObject({ status: "success", value: { age: 1 } }); | ||
|
||
const output2 = await parseWithValibot(createFormData("age", "0"), { | ||
schema, | ||
}); | ||
expect(output2).toMatchObject({ | ||
error: { age: ["age must be greater than 0"] }, | ||
}); | ||
|
||
const output3 = await parseWithValibot(createFormData("age", ""), { | ||
schema, | ||
}); | ||
expect(output3).toMatchObject({ | ||
error: { | ||
age: ["Invalid type: Expected !undefined but received undefined"], | ||
}, | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.