Skip to content

Commit

Permalink
fix: WrapSchema conversion process is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
chimame committed Feb 3, 2024
1 parent ea8b829 commit 6f4a86b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
92 changes: 56 additions & 36 deletions coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
2 changes: 1 addition & 1 deletion tests/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" } });
});
});
12 changes: 12 additions & 0 deletions tests/pipe.test.ts
Original file line number Diff line number Diff line change
@@ -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 } });
});
});

0 comments on commit 6f4a86b

Please sign in to comment.