diff --git a/coercion.ts b/coercion.ts index 85f1ad7..e634ab0 100644 --- a/coercion.ts +++ b/coercion.ts @@ -218,63 +218,83 @@ export function enableTypeCoercion< wrap: optional, default: type.getDefault, }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "nullish") { schema = enableTypeCoercion(type.wrapped, { wrap: nullish, default: type.getDefault, }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "nullable") { schema = enableTypeCoercion(type.wrapped, { wrap: nullable, default: type.getDefault, }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "non_optional") { schema = enableTypeCoercion(type.wrapped, { wrap: nonOptional }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "non_nullish") { schema = enableTypeCoercion(type.wrapped, { wrap: nonNullish }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "non_nullable") { schema = enableTypeCoercion(type.wrapped, { wrap: nonNullable }); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "union") { // @ts-expect-error schema = union(type.options.map((option) => enableTypeCoercion(option))); - schema = options?.wrap - ? "default" in options - ? options.wrap(schema, options.default) - : options.wrap(schema) - : schema; + schema = coerce( + options?.wrap + ? "default" in options + ? options.wrap(schema, options.default) + : options.wrap(schema) + : schema, + (output) => coerceString(output), + ); } else if (type.type === "object") { const shape = Object.fromEntries( - // @ts-ignore Object.entries(type.entries).map(([key, def]) => [ key, enableTypeCoercion(def, { cache }), diff --git a/tests/optional.test.ts b/tests/optional.test.ts index 192bd00..690e48f 100644 --- a/tests/optional.test.ts +++ b/tests/optional.test.ts @@ -26,7 +26,7 @@ describe("optional", () => { expect(output1).toMatchObject({ error: {}, value: { name: "default" } }); const schema2 = object({ name: optional(string(), () => default_) }); - const output2 = parse(createFormData("name", ""), { schema: schema1 }); + const output2 = parse(createFormData("name", ""), { schema: schema2 }); expect(output2).toMatchObject({ error: {}, value: { name: "default" } }); }); }); diff --git a/tests/pipe.test.ts b/tests/pipe.test.ts new file mode 100644 index 0000000..e78d655 --- /dev/null +++ b/tests/pipe.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, test } from "vitest"; +import { string, object, optional, nullable } from "valibot"; +import { parse } from "../parse"; +import { createFormData } from "./helpers/FormData"; + +describe("pipe", () => { + test("should pass also undefined", () => { + const schema = object({ name: optional(nullable(string()), null) }); + const output = parse(createFormData("name", ""), { schema: schema }); + expect(output).toMatchObject({ error: {}, value: { name: null } }); + }); +});