Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/zod/src/v3/tests/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { expect, test } from "vitest";
import * as z from "zod/v3";

const gtFive = z.number().gt(5);
const gteFive = z.number().gte(5);
const minFive = z.number().min(5);
const ltFive = z.number().lt(5);
const gteFive = z.number().gte(-5).gte(5);
const minFive = z.number().min(0).min(5);
const ltFive = z.number().lte(10).lt(5);
const lteFive = z.number().lte(5);
const maxFive = z.number().max(5);
const maxFive = z.number().max(10).max(5);
const intNum = z.number().int();
const positive = z.number().positive();
const negative = z.number().negative();
Expand Down
30 changes: 17 additions & 13 deletions packages/zod/src/v4/classic/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@ export const _ZodString: core.$constructor<_ZodString> = /*@__PURE__*/ core.$con
core.$ZodString.init(inst, def);
ZodType.init(inst, def);

inst.format = inst._zod.computed.format ?? null;
inst.minLength = inst._zod.computed.minimum ?? null;
inst.maxLength = inst._zod.computed.maximum ?? null;
const bag = inst._zod.bag;
inst.format = bag.format ?? null;
inst.minLength = bag.minimum ?? null;
inst.maxLength = bag.maximum ?? null;

// validations
inst.regex = (...args) => inst.check(checks.regex(...args));
Expand Down Expand Up @@ -719,7 +720,7 @@ export interface _ZodNumber<Input = unknown> extends ZodType {
step(value: number, params?: string | core.$ZodCheckMultipleOfParams): this;

/** @deprecated In v4 and later, z.number() does not allow infinite values by default. This is a no-op. */
finite(params?: any): this;
finite(params?: unknown): this;

minValue: number | null;
maxValue: number | null;
Expand Down Expand Up @@ -754,12 +755,14 @@ export const ZodNumber: core.$constructor<ZodNumber> = /*@__PURE__*/ core.$const
// inst.finite = (params) => inst.check(core.finite(params));
inst.finite = () => inst;

inst.minValue = inst._zod.computed.minimum ?? null;
inst.maxValue = inst._zod.computed.maximum ?? null;
inst.isInt =
(inst._zod.computed.format ?? "").includes("int") || Number.isSafeInteger(inst._zod.computed.multipleOf ?? 0.5);
const bag = inst._zod.bag;
inst.minValue =
Math.max(bag.minimum ?? Number.NEGATIVE_INFINITY, bag.exclusiveMinimum ?? Number.NEGATIVE_INFINITY) ?? null;
inst.maxValue =
Math.min(bag.maximum ?? Number.POSITIVE_INFINITY, bag.exclusiveMaximum ?? Number.POSITIVE_INFINITY) ?? null;
inst.isInt = (bag.format ?? "").includes("int") || Number.isSafeInteger(bag.multipleOf ?? 0.5);
inst.isFinite = true;
inst.format = inst._zod.computed.format ?? null;
inst.format = bag.format ?? null;
});

export function number(params?: string | core.$ZodNumberParams): ZodNumber {
Expand Down Expand Up @@ -864,9 +867,10 @@ export const ZodBigInt: core.$constructor<ZodBigInt> = /*@__PURE__*/ core.$const
inst.nonnegative = (params) => inst.check(checks.gte(BigInt(0), params));
inst.multipleOf = (value, params) => inst.check(checks.multipleOf(value, params));

inst.minValue = inst._zod.computed.minimum ?? null;
inst.maxValue = inst._zod.computed.maximum ?? null;
inst.format = inst._zod.computed.format ?? null;
const bag = inst._zod.bag;
inst.minValue = bag.minimum ?? null;
inst.maxValue = bag.maximum ?? null;
inst.format = bag.format ?? null;
});

export function bigint(params?: string | core.$ZodBigIntParams): ZodBigInt {
Expand Down Expand Up @@ -1014,7 +1018,7 @@ export const ZodDate: core.$constructor<ZodDate> = /*@__PURE__*/ core.$construct
inst.min = (value, params) => inst.check(checks.gte(value, params));
inst.max = (value, params) => inst.check(checks.lte(value, params));

const c = inst._zod.computed;
const c = inst._zod.bag;
inst.minDate = c.minimum ? new Date(c.minimum) : null;
inst.maxDate = c.maximum ? new Date(c.maximum) : null;
});
Expand Down
8 changes: 4 additions & 4 deletions packages/zod/src/v4/classic/tests/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test("catch with transform", () => {
expect(stringWithDefault.unwrap().out).toBeInstanceOf(z.ZodTransform);

type inp = z.input<typeof stringWithDefault>;
expectTypeOf<inp>().toEqualTypeOf<util.Loose<string>>();
expectTypeOf<inp>().toEqualTypeOf<string | util.Whatever>();
type out = z.output<typeof stringWithDefault>;
expectTypeOf<out>().toEqualTypeOf<string>();
});
Expand All @@ -59,7 +59,7 @@ test("catch on existing optional", () => {
expect(stringWithDefault.unwrap().unwrap()).toBeInstanceOf(z.ZodString);

type inp = z.input<typeof stringWithDefault>;
expectTypeOf<inp>().toEqualTypeOf<util.Loose<string | undefined>>();
expectTypeOf<inp>().toEqualTypeOf<string | undefined | util.Whatever>();
type out = z.output<typeof stringWithDefault>;
expectTypeOf<out>().toEqualTypeOf<string | undefined>();
});
Expand All @@ -68,7 +68,7 @@ test("optional on catch", () => {
const stringWithDefault = z.string().catch("asdf").optional();

type inp = z.input<typeof stringWithDefault>;
expectTypeOf<inp>().toEqualTypeOf<util.Loose<string | undefined>>();
expectTypeOf<inp>().toEqualTypeOf<string | util.Whatever>();
type out = z.output<typeof stringWithDefault>;
expectTypeOf<out>().toEqualTypeOf<string | undefined>();
});
Expand Down Expand Up @@ -102,7 +102,7 @@ test("nested", () => {
inner: "asdf",
});
type input = z.input<typeof outer>;
expectTypeOf<input>().toEqualTypeOf<util.Loose<{ inner: util.Loose<string> }>>();
expectTypeOf<input>().toEqualTypeOf<{ inner: string | util.Whatever } | util.Whatever>();
type out = z.output<typeof outer>;

expectTypeOf<out>().toEqualTypeOf<{ inner: string }>();
Expand Down
122 changes: 122 additions & 0 deletions packages/zod/src/v4/classic/tests/json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,80 @@ describe("toJSONSchema", () => {
}
`
);

expect(toJSONSchema(z.number().gt(5).gt(10))).toMatchInlineSnapshot(`
{
"exclusiveMinimum": 10,
"type": "number",
}
`);

expect(toJSONSchema(z.number().gt(5).gte(10))).toMatchInlineSnapshot(`
{
"minimum": 10,
"type": "number",
}
`);

expect(toJSONSchema(z.number().lt(5).lt(3))).toMatchInlineSnapshot(`
{
"exclusiveMaximum": 3,
"type": "number",
}
`);

expect(toJSONSchema(z.number().lt(5).lt(3).lte(2))).toMatchInlineSnapshot(`
{
"maximum": 2,
"type": "number",
}
`);

expect(toJSONSchema(z.number().lt(5).lte(3))).toMatchInlineSnapshot(`
{
"maximum": 3,
"type": "number",
}
`);

expect(toJSONSchema(z.number().gt(5).lt(10))).toMatchInlineSnapshot(`
{
"exclusiveMaximum": 10,
"exclusiveMinimum": 5,
"type": "number",
}
`);
expect(toJSONSchema(z.number().gte(5).lte(10))).toMatchInlineSnapshot(`
{
"maximum": 10,
"minimum": 5,
"type": "number",
}
`);
expect(toJSONSchema(z.number().positive())).toMatchInlineSnapshot(`
{
"exclusiveMinimum": 0,
"type": "number",
}
`);
expect(toJSONSchema(z.number().negative())).toMatchInlineSnapshot(`
{
"exclusiveMaximum": 0,
"type": "number",
}
`);
expect(toJSONSchema(z.number().nonpositive())).toMatchInlineSnapshot(`
{
"maximum": 0,
"type": "number",
}
`);
expect(toJSONSchema(z.number().nonnegative())).toMatchInlineSnapshot(`
{
"minimum": 0,
"type": "number",
}
`);
});

test("arrays", () => {
Expand Down Expand Up @@ -1544,3 +1618,51 @@ test("override with refs", () => {
}
`);
});

// test("number checks", () => {
// expect(z.toJSONSchema(z.number().int())).toMatchInlineSnapshot(`
// {
// "maximum": 9007199254740991,
// "minimum": -9007199254740991,
// "type": "integer",
// }
// `);
// expect(z.toJSONSchema(z.int())).toMatchInlineSnapshot(`
// {
// "maximum": 9007199254740991,
// "minimum": -9007199254740991,
// "type": "integer",
// }
// `);
// expect(z.toJSONSchema(z.int().positive())).toMatchInlineSnapshot(`
// {
// "exclusiveMinimum": 0,
// "maximum": 9007199254740991,
// "minimum": -9007199254740991,
// "type": "integer",
// }
// `);
// expect(z.toJSONSchema(z.int().nonnegative())).toMatchInlineSnapshot(`
// {
// "maximum": 9007199254740991,
// "minimum": 0,
// "type": "integer",
// }
// `);
// expect(z.toJSONSchema(z.int().gt(0))).toMatchInlineSnapshot(`
// {
// "exclusiveMinimum": 0,
// "maximum": 9007199254740991,
// "minimum": -9007199254740991,
// "type": "integer",
// }
// `);
// expect(z.toJSONSchema(z.int().gte(0))).toMatchInlineSnapshot(`
// {
// "maximum": 9007199254740991,
// "minimum": 0,
// "type": "integer",
// }
// `);

// });
12 changes: 6 additions & 6 deletions packages/zod/src/v4/classic/tests/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,37 @@ test("Infinity validation", () => {
});

test(".gt() validation", () => {
const schema = z.number().gt(5);
const schema = z.number().gt(0).gt(5);
expect(schema.parse(6)).toEqual(6);
expect(() => schema.parse(5)).toThrow();
});

test(".gte() validation", () => {
const schema = z.number().gte(5);
const schema = z.number().gt(0).gte(1).gte(5);
expect(schema.parse(5)).toEqual(5);
expect(() => schema.parse(4)).toThrow();
});

test(".min() validation", () => {
const schema = z.number().min(5);
const schema = z.number().min(0).min(5);
expect(schema.parse(5)).toEqual(5);
expect(() => schema.parse(4)).toThrow();
});

test(".lt() validation", () => {
const schema = z.number().lt(5);
const schema = z.number().lte(10).lt(5);
expect(schema.parse(4)).toEqual(4);
expect(() => schema.parse(5)).toThrow();
});

test(".lte() validation", () => {
const schema = z.number().lte(5);
const schema = z.number().lte(10).lte(5);
expect(schema.parse(5)).toEqual(5);
expect(() => schema.parse(6)).toThrow();
});

test(".max() validation", () => {
const schema = z.number().max(5);
const schema = z.number().max(10).max(5);
expect(schema.parse(5)).toEqual(5);
expect(() => schema.parse(6)).toThrow();
});
Expand Down
Loading