Skip to content

Commit

Permalink
Added coerce support (#18)
Browse files Browse the repository at this point in the history
* Add coerce

* fix: fix tests, use object.assign for adding coerce
  • Loading branch information
miunau authored Jun 11, 2024
1 parent e94bec4 commit 5c24b00
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/dezerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type DezerializersMap = {
};
const dezerializers = {
number: (shape) => {
let n = z.number();
let n = shape.coerce ? z.coerce.number() : z.number();
if (shape.min !== undefined) {
n = shape.minInclusive ? n.min(shape.min) : n.gt(shape.min);
}
Expand All @@ -136,7 +136,7 @@ const dezerializers = {
return n;
},
string: (shape) => {
let s = z.string();
let s = shape.coerce ? z.coerce.string() : z.string();
if (shape.min !== undefined) {
s = s.min(shape.min);
}
Expand Down Expand Up @@ -170,10 +170,10 @@ const dezerializers = {

return s;
},
boolean: () => z.boolean(),
boolean: (shape) => shape.coerce ? z.coerce.boolean() : z.boolean(),
nan: () => z.nan(),
bigInt: (shape) => {
let i = z.bigint();
let i = shape.coerce ? z.coerce.bigint() : z.bigint();
if (shape.min !== undefined) {
i = shape.minInclusive ? i.min(shape.min) : i.gt(shape.min);
}
Expand All @@ -186,7 +186,7 @@ const dezerializers = {
return i;
},
date: (shape) => {
let i = z.date();
let i = shape.coerce ? z.coerce.date() : z.date();
if (shape.min !== undefined) {
i = i.min(new Date(shape.min));
}
Expand Down
11 changes: 11 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,14 @@ test("discriminated union", () => {
reach: 42,
});
});

test("coerce", () => {
const schema = z.coerce.number();
expect(schema.parse("42")).toEqual(42);
const shape = zerialize(schema);
expect(shape).toEqual({
type: "number",
coerce: true,
});
expect(dezerialize(shape as SzType).parse("42")).toEqual(42);
});
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ValueOf } from "type-fest";

export type SzNumber = {
type: "number";
coerce?: boolean;
min?: number;
max?: number;
minInclusive?: boolean;
Expand All @@ -12,6 +13,7 @@ export type SzNumber = {
};
export type SzBigInt = {
type: "bigInt";
coerce?: boolean;
min?: bigint;
max?: bigint;
minInclusive?: boolean;
Expand All @@ -31,6 +33,7 @@ export const STRING_KINDS = new Set([

export type SzString = {
type: "string";
coerce?: boolean;
min?: number;
max?: number;
length?: number;
Expand Down Expand Up @@ -59,11 +62,12 @@ export type SzString = {

export type SzDate = {
type: "date";
coerce?: boolean;
min?: number;
max?: number;
};

export type SzBoolean = { type: "boolean" };
export type SzBoolean = { type: "boolean"; coerce?: boolean };
export type SzNaN = { type: "nan" };
export type SzUndefined = { type: "undefined" };
export type SzNull = { type: "null" };
Expand Down
23 changes: 18 additions & 5 deletions src/zerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ const zerializers = {
}),
{}
);
return { type: "number", ...checks };
return Object.assign(
{ type: "number", ...checks },
def.coerce ? { coerce: true } : {}
);
},
ZodString: (def) => {
const checks = def.checks.reduce(
Expand Down Expand Up @@ -209,9 +212,13 @@ const zerializers = {
}),
{}
);
return { type: "string", ...checks };
return Object.assign(
{ type: "string", ...checks },
def.coerce ? { coerce: true } : {}
);
},
ZodBoolean: () => ({ type: "boolean" }),
ZodBoolean: (def) =>
Object.assign({ type: "boolean" }, def.coerce ? { coerce: true } : {}),
ZodNaN: () => ({ type: "nan" }),
ZodBigInt: (def) => {
const checks = def.checks.reduce(
Expand All @@ -236,7 +243,10 @@ const zerializers = {
}),
{}
);
return { type: "bigInt", ...checks };
return Object.assign(
{ type: "bigInt", ...checks },
def.coerce ? { coerce: true } : {}
);
},
ZodDate: (def) => {
const checks = def.checks.reduce(
Expand All @@ -253,7 +263,10 @@ const zerializers = {
}),
{}
);
return { type: "date", ...checks };
return Object.assign(
{ type: "date", ...checks },
def.coerce ? { coerce: true } : {}
);
},
ZodUndefined: () => ({ type: "undefined" }),
ZodNull: () => ({ type: "null" }),
Expand Down

0 comments on commit 5c24b00

Please sign in to comment.