-
Notifications
You must be signed in to change notification settings - Fork 1
/
optional.spec.ts
152 lines (122 loc) · 4.15 KB
/
optional.spec.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable unicorn/no-useless-undefined */
/* eslint-disable unicorn/no-null */
import { refineAsync } from "../refine";
import { coercedDate, string } from "../simple";
import { number } from "../simple/number";
import { translate } from "../validation";
import {
nullToUndefined,
nullable,
nullish,
optional,
required,
} from "./optional";
const asyncPositiveNumber = refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
);
describe("optional", () => {
const schema = optional(number());
it("accepts", () => {
expect(schema.accepts(undefined)).toBeTruthy();
expect(schema.accepts(1)).toBeTruthy();
expect(schema.accepts(null)).toBeFalsy();
});
it("validates", () => {
expect(schema.validate(undefined)).toBeUndefined();
expect(translate(schema.validate(null))).toEqual("value is required");
});
it("validates async", async () => {
const schema = optional(asyncPositiveNumber);
expect(await schema.validateAsync(1)).toBeUndefined();
expect(await schema.validateAsync(undefined)).toBeUndefined();
expect(translate(await schema.validateAsync(null))).toEqual(
"value is required"
);
});
it("parses", () => {
expect(optional(coercedDate()).parse(42).parsedValue).toEqual(new Date(42));
});
});
describe("required", () => {
const schema = required(optional(number()));
it("accepts", () => {
expect(schema.accepts(1)).toBeTruthy();
expect(schema.accepts(undefined)).toBeFalsy();
expect(schema.accepts(null)).toBeFalsy();
});
it("validates", () => {
expect(translate(schema.validate(undefined))).toEqual("value is required");
expect(translate(schema.validate(null))).toEqual("value is required");
});
it("validates async", async () => {
const schema = required(optional(asyncPositiveNumber));
expect(await schema.validateAsync(1)).toBeUndefined();
expect(translate(await schema.validateAsync(undefined))).toEqual(
"value is required"
);
expect(translate(await schema.validateAsync(null))).toEqual(
"value is required"
);
});
it("builds meta", () => {
expect(schema.meta().required).toBeTruthy();
});
it("parses", () => {
expect(required(optional(coercedDate())).parse(42).parsedValue).toEqual(
new Date(42)
);
});
});
describe("nullable", () => {
const schema = nullable(number());
it("accepts", () => {
expect(schema.accepts(1)).toBeTruthy();
expect(schema.accepts(null)).toBeTruthy();
expect(schema.accepts(undefined)).toBeFalsy();
});
it("validates", () => {
expect(schema.validate(null)).toBeUndefined();
expect(translate(schema.validate(undefined))).toEqual("value is required");
});
it("validates async", async () => {
const schema = nullable(asyncPositiveNumber);
expect(await schema.validateAsync(1)).toBeUndefined();
expect(await schema.validateAsync(null)).toBeUndefined();
expect(translate(await schema.validateAsync(undefined))).toEqual(
"value is required"
);
});
it("parses", () => {
expect(nullable(coercedDate()).parse(42).parsedValue).toEqual(new Date(42));
});
});
describe("nullish", () => {
const schema = nullish(number());
it("accepts", () => {
expect(schema.accepts(undefined)).toBeTruthy();
expect(schema.accepts(1)).toBeTruthy();
expect(schema.accepts(null)).toBeTruthy();
});
it("validates", () => {
expect(schema.validate(undefined)).toBeUndefined();
expect(schema.validate(null)).toBeUndefined();
});
it("validates async", async () => {
const schema = nullish(asyncPositiveNumber);
expect(await schema.validateAsync(1)).toBeUndefined();
expect(await schema.validateAsync(null)).toBeUndefined();
expect(translate(await schema.validateAsync(undefined))).toBeUndefined();
});
it("parses", () => {
expect(nullish(coercedDate()).parse(42).parsedValue).toEqual(new Date(42));
});
it("builds meta", () => {
expect(schema.meta().required).toBeFalsy();
});
});
describe("nullToUndefined", () => {
it("coerces", () => {
const schema = nullToUndefined(nullish(string()));
expect(schema.parse(null).parsedValue).toEqual(undefined);
});
});