forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.test.ts
More file actions
56 lines (47 loc) · 1.65 KB
/
number.test.ts
File metadata and controls
56 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import * as z from "../index";
const gtFive = z.number().gt(5);
const gteFive = z.number().gte(5);
const ltFive = z.number().lt(5);
const lteFive = z.number().lte(5);
const intNum = z.number().int();
const multipleOfFive = z.number().multipleOf(5);
const stepPointOne = z.number().step(0.1);
const stepPointZeroZeroZeroOne = z.number().step(0.0001);
const stepSixPointFour = z.number().step(6.4);
test("passing validations", () => {
gtFive.parse(6);
gteFive.parse(5);
ltFive.parse(4);
lteFive.parse(5);
intNum.parse(4);
multipleOfFive.parse(15);
stepPointOne.parse(6);
stepPointOne.parse(6.1);
stepPointOne.parse(6.1);
stepSixPointFour.parse(12.8);
stepPointZeroZeroZeroOne.parse(3.01);
});
test("failing validations", () => {
expect(() => ltFive.parse(5)).toThrow();
expect(() => lteFive.parse(6)).toThrow();
expect(() => gtFive.parse(5)).toThrow();
expect(() => gteFive.parse(4)).toThrow();
expect(() => intNum.parse(3.14)).toThrow();
expect(() => multipleOfFive.parse(14.9)).toThrow();
expect(() => stepPointOne.parse(6.11)).toThrow();
expect(() => stepPointOne.parse(6.1000000001)).toThrow();
expect(() => stepSixPointFour.parse(6.41)).toThrow();
});
test("parse NaN", () => {
expect(() => z.number().parse(NaN)).toThrow();
});
test("min max getters", () => {
expect(z.number().int().isInt).toEqual(true);
expect(z.number().isInt).toEqual(false);
expect(z.number().min(5).minValue).toEqual(5);
expect(z.number().min(5).min(10).minValue).toEqual(10);
expect(z.number().max(5).maxValue).toEqual(5);
expect(z.number().max(5).max(1).maxValue).toEqual(1);
});