-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.spec.ts
135 lines (126 loc) · 3.65 KB
/
schema.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
/* eslint-disable unicorn/no-useless-undefined */
import { object } from "./composite/object";
import { or } from "./logic/or";
import { refine } from "./refine";
import { coerce, json, narrow, options, transform } from "./schema";
import { nan, number } from "./simple/number";
import { string } from "./simple/string";
import { optional } from "./utility";
import { translate } from "./validation";
describe("coerce", () => {
const schema = coerce(number(), Number);
it("accepts (without coercion)", () => {
expect(schema.accepts(1)).toBeTruthy();
expect(schema.accepts("")).toBeFalsy();
expect(schema.accepts(undefined)).toBeFalsy();
});
it("validates", () => {
expect(schema.validate("", { withCoercion: true })).toBeUndefined();
expect(
translate(schema.validate(undefined, { withCoercion: true }))
).toEqual("isNaN");
});
it("parses", () => {
expect(schema.parse("").parsedValue).toEqual(0);
expect(schema.parse("1").parsedValue).toEqual(1);
expect(
object({
nested: schema,
}).parse({ nested: "1" }).parsedValue
).toEqual({ nested: 1 });
});
});
describe("narrow", () => {
const schema = narrow(optional(or(number(), nan())), (v) =>
Number.isNaN(v) ? undefined : v
);
it("accepts", () => {
expect(schema.accepts(12)).toBeTruthy();
expect(schema.accepts(Number.NaN)).toBeTruthy();
expect(schema.accepts(undefined)).toBeTruthy();
});
it("parses", () => {
expect(schema.parse(12).parsedValue).toEqual(12);
expect(schema.parse(undefined).parsedValue).toEqual(undefined);
expect(schema.parse(Number.NaN).parsedValue).toEqual(undefined);
});
});
describe("transform", () => {
const transformRefineTransform = transform(
refine(
object({
age: transform(number(), (v) => ({ boxed: v })),
}),
(u, { validIf }) => ({
age: validIf(u.age >= 18, "should be at least 18"),
})
),
(v) => v.age.boxed
);
it("accepts", () => {
expect(transformRefineTransform.accepts({ age: 21 })).toBeTruthy();
expect(transformRefineTransform.accepts({ age: 12 })).toBeFalsy();
});
it("validates", () => {
expect(translate(transformRefineTransform.validate({ age: 12 }))).toEqual({
age: "should be at least 18",
});
});
it("parses", () => {
expect(transformRefineTransform.parse({ age: 21 }).parsedValue).toEqual(21);
});
});
describe("options", () => {
it("sets earlyExit", () => {
expect(
translate(
object({
outer: options(object({ a: string(), b: string() }), {
earlyExit: true,
}),
second: number(),
}).validate({
outer: {
a: 1,
b: 2,
},
})
)
).toEqual({
outer: { a: "value was of type number expected string" },
second: "value is required",
});
});
it("sets strip", () => {
expect(
object({
// disable strip mode
a: options(
object({
// enable strip mode again
b: options(object({ c: string() }), { strip: true }),
// here strip is still disable
d: object({
e: number(),
}),
}),
{ strip: false }
),
}).parse({
a: {
b: { c: "string", add: "inner.inner" },
d: { e: 1, add: "more" },
add: "inner",
},
add: "outer",
}).parsedValue
).toEqual({
a: { b: { c: "string" }, d: { e: 1, add: "more" }, add: "inner" },
});
});
});
describe("json", () => {
it("coerces from json", () => {
expect(json(number()).parse(JSON.stringify(12)).parsedValue).toEqual(12);
});
});