Skip to content

Commit

Permalink
Add async validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
chimame committed Jun 12, 2024
1 parent d494a85 commit b3b3fce
Show file tree
Hide file tree
Showing 12 changed files with 874 additions and 33 deletions.
113 changes: 113 additions & 0 deletions tests/coercion/schema/intersectAsync.test.ts
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"],
},
});
});
});
105 changes: 105 additions & 0 deletions tests/coercion/schema/nonNullishAsync.test.ts
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"] },
});
});
});
96 changes: 96 additions & 0 deletions tests/coercion/schema/nonOptionalAsync.test.ts
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"],
},
});
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { describe, expect, test } from "vitest";
import { string, number, object, nullable, check, pipe } from "valibot";
import {
string,
number,
object,
nullable,
optional,
check,
pipe,
} from "valibot";
import { parseWithValibot } from "../../../parse";
import { createFormData } from "../../helpers/FormData";

describe("nullable", () => {
test("should pass also undefined", () => {
const schema = object({ age: nullable(number()) });
const output = parseWithValibot(createFormData("age", ""), { schema });
const output = parseWithValibot(createFormData("age", "20"), { schema });

expect(output).toMatchObject({
status: "success",
value: { age: undefined },
});
expect(
parseWithValibot(createFormData("age", "20"), { schema }),
).toMatchObject({
status: "success",
value: { age: 20 },
});
expect(
parseWithValibot(createFormData("age", "non number"), { schema }),
).toMatchObject({ error: { age: ["Invalid type"] } });
).toMatchObject({
error: { age: ["Invalid type: Expected number but received NaN"] },
});
});

test("should pass nullable with pipe", () => {
Expand All @@ -34,12 +38,6 @@ describe("nullable", () => {
),
});

const output1 = parseWithValibot(createFormData("age", ""), { schema });
expect(output1).toMatchObject({
status: "success",
value: { age: undefined },
});

const output2 = parseWithValibot(createFormData("age", "20"), { schema });
expect(output2).toMatchObject({
status: "success",
Expand All @@ -57,7 +55,9 @@ describe("nullable", () => {
test("should use default if required", () => {
const default_ = "default";

const schema1 = object({ name: nullable(string(), default_) });
const schema1 = object({
name: optional(nullable(string(), default_), null),
});
const output1 = parseWithValibot(createFormData("name", ""), {
schema: schema1,
});
Expand Down
Loading

0 comments on commit b3b3fce

Please sign in to comment.