Skip to content

Commit

Permalink
add ZodNumber.safe() & ZodNumber.isSafe. (#1753)
Browse files Browse the repository at this point in the history
* implement `ZodNumber.safe()`.

* implement `ZodNumber.isSafe`.

* test `ZodNumber.safe()` & `ZodNumber.isSafe`.

* add `ZodNumber.safe()` to README.md.

* test `ZodNumer.isSafe`.

* Remove isSafe

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
  • Loading branch information
igalklebanov and colinhacks authored Mar 4, 2023
1 parent e71cc52 commit 3af38fb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ z.number().nonpositive(); // <= 0
z.number().multipleOf(5); // Evenly divisible by 5. Alias .step(5)

z.number().finite(); // value must be finite, not Infinity or -Infinity
z.number().safe(); // value must be between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
```
Optionally, you can pass in a second argument to provide a custom error message.
Expand Down
1 change: 1 addition & 0 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ z.number().nonpositive(); // <= 0
z.number().multipleOf(5); // Evenly divisible by 5. Alias .step(5)

z.number().finite(); // value must be finite, not Infinity or -Infinity
z.number().safe(); // value must be between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
```
Optionally, you can pass in a second argument to provide a custom error message.
Expand Down
9 changes: 9 additions & 0 deletions deno/lib/__tests__/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const nonnegative = z.number().nonnegative();
const multipleOfFive = z.number().multipleOf(5);
const multipleOfNegativeFive = z.number().multipleOf(-5);
const finite = z.number().finite();
const safe = z.number().safe();
const stepPointOne = z.number().step(0.1);
const stepPointZeroZeroZeroOne = z.number().step(0.0001);
const stepSixPointFour = z.number().step(6.4);
Expand Down Expand Up @@ -58,6 +59,8 @@ test("passing validations", () => {
multipleOfNegativeFive.parse(-15);
multipleOfNegativeFive.parse(15);
finite.parse(123);
safe.parse(Number.MIN_SAFE_INTEGER);
safe.parse(Number.MAX_SAFE_INTEGER);
stepPointOne.parse(6);
stepPointOne.parse(6.1);
stepPointOne.parse(6.1);
Expand Down Expand Up @@ -85,6 +88,8 @@ test("failing validations", () => {
expect(() => multipleOfNegativeFive.parse(7.5)).toThrow();
expect(() => finite.parse(Infinity)).toThrow();
expect(() => finite.parse(-Infinity)).toThrow();
expect(() => safe.parse(Number.MIN_SAFE_INTEGER - 1)).toThrow();
expect(() => safe.parse(Number.MAX_SAFE_INTEGER + 1)).toThrow();

expect(() => stepPointOne.parse(6.11)).toThrow();
expect(() => stepPointOne.parse(6.1000000001)).toThrow();
Expand All @@ -111,6 +116,7 @@ test("min max getters", () => {
expect(minFive.min(10).minValue).toEqual(10);
expect(positive.minValue).toEqual(0);
expect(nonnegative.minValue).toEqual(0);
expect(safe.minValue).toEqual(Number.MIN_SAFE_INTEGER);

expect(z.number().maxValue).toBeNull;
expect(gtFive.maxValue).toBeNull;
Expand All @@ -127,6 +133,7 @@ test("min max getters", () => {
expect(maxFive.max(1).maxValue).toEqual(1);
expect(negative.maxValue).toEqual(0);
expect(nonpositive.maxValue).toEqual(0);
expect(safe.maxValue).toEqual(Number.MAX_SAFE_INTEGER);
});

test("int getter", () => {
Expand All @@ -143,6 +150,7 @@ test("int getter", () => {
expect(maxFive.isInt).toEqual(false);
expect(negative.isInt).toEqual(false);
expect(nonpositive.isInt).toEqual(false);
expect(safe.isInt).toEqual(false);

expect(intNum.isInt).toEqual(true);
expect(multipleOfFive.isInt).toEqual(true);
Expand All @@ -165,4 +173,5 @@ test("finite getter", () => {
expect(intNum.isFinite).toEqual(true);
expect(multipleOfFive.isFinite).toEqual(true);
expect(z.number().min(5).max(10).isFinite).toEqual(true);
expect(safe.isFinite).toEqual(true);
});
15 changes: 14 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> {
message: errorUtil.toString(message),
});
}
step = this.multipleOf;

finite(message?: errorUtil.ErrMessage) {
return this._addCheck({
Expand All @@ -1219,7 +1220,19 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> {
});
}

step = this.multipleOf;
safe(message?: errorUtil.ErrMessage) {
return this._addCheck({
kind: "min",
inclusive: true,
value: Number.MIN_SAFE_INTEGER,
message: errorUtil.toString(message),
})._addCheck({
kind: "max",
inclusive: true,
value: Number.MAX_SAFE_INTEGER,
message: errorUtil.toString(message),
});
}

get minValue() {
let min: number | null = null;
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const nonnegative = z.number().nonnegative();
const multipleOfFive = z.number().multipleOf(5);
const multipleOfNegativeFive = z.number().multipleOf(-5);
const finite = z.number().finite();
const safe = z.number().safe();
const stepPointOne = z.number().step(0.1);
const stepPointZeroZeroZeroOne = z.number().step(0.0001);
const stepSixPointFour = z.number().step(6.4);
Expand Down Expand Up @@ -57,6 +58,8 @@ test("passing validations", () => {
multipleOfNegativeFive.parse(-15);
multipleOfNegativeFive.parse(15);
finite.parse(123);
safe.parse(Number.MIN_SAFE_INTEGER);
safe.parse(Number.MAX_SAFE_INTEGER);
stepPointOne.parse(6);
stepPointOne.parse(6.1);
stepPointOne.parse(6.1);
Expand Down Expand Up @@ -84,6 +87,8 @@ test("failing validations", () => {
expect(() => multipleOfNegativeFive.parse(7.5)).toThrow();
expect(() => finite.parse(Infinity)).toThrow();
expect(() => finite.parse(-Infinity)).toThrow();
expect(() => safe.parse(Number.MIN_SAFE_INTEGER - 1)).toThrow();
expect(() => safe.parse(Number.MAX_SAFE_INTEGER + 1)).toThrow();

expect(() => stepPointOne.parse(6.11)).toThrow();
expect(() => stepPointOne.parse(6.1000000001)).toThrow();
Expand All @@ -110,6 +115,7 @@ test("min max getters", () => {
expect(minFive.min(10).minValue).toEqual(10);
expect(positive.minValue).toEqual(0);
expect(nonnegative.minValue).toEqual(0);
expect(safe.minValue).toEqual(Number.MIN_SAFE_INTEGER);

expect(z.number().maxValue).toBeNull;
expect(gtFive.maxValue).toBeNull;
Expand All @@ -126,6 +132,7 @@ test("min max getters", () => {
expect(maxFive.max(1).maxValue).toEqual(1);
expect(negative.maxValue).toEqual(0);
expect(nonpositive.maxValue).toEqual(0);
expect(safe.maxValue).toEqual(Number.MAX_SAFE_INTEGER);
});

test("int getter", () => {
Expand All @@ -142,6 +149,7 @@ test("int getter", () => {
expect(maxFive.isInt).toEqual(false);
expect(negative.isInt).toEqual(false);
expect(nonpositive.isInt).toEqual(false);
expect(safe.isInt).toEqual(false);

expect(intNum.isInt).toEqual(true);
expect(multipleOfFive.isInt).toEqual(true);
Expand All @@ -164,4 +172,5 @@ test("finite getter", () => {
expect(intNum.isFinite).toEqual(true);
expect(multipleOfFive.isFinite).toEqual(true);
expect(z.number().min(5).max(10).isFinite).toEqual(true);
expect(safe.isFinite).toEqual(true);
});
15 changes: 14 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> {
message: errorUtil.toString(message),
});
}
step = this.multipleOf;

finite(message?: errorUtil.ErrMessage) {
return this._addCheck({
Expand All @@ -1219,7 +1220,19 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> {
});
}

step = this.multipleOf;
safe(message?: errorUtil.ErrMessage) {
return this._addCheck({
kind: "min",
inclusive: true,
value: Number.MIN_SAFE_INTEGER,
message: errorUtil.toString(message),
})._addCheck({
kind: "max",
inclusive: true,
value: Number.MAX_SAFE_INTEGER,
message: errorUtil.toString(message),
});
}

get minValue() {
let min: number | null = null;
Expand Down

0 comments on commit 3af38fb

Please sign in to comment.