-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor.spec.ts
130 lines (121 loc) · 3.31 KB
/
xor.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
import { array } from "../composite/array";
import { object } from "../composite/object";
import { refineAsync } from "../refine";
import { number } from "../simple/number";
import { string } from "../simple/string";
import { defaultValue, optional } from "../utility/optional";
import { translate } from "../validation";
import { xor } from "./xor";
const Named = object({
id: number(),
name: array(string()),
});
const Described = object({
description: defaultValue(optional(string()), "default"),
nested: object({
user: string(),
}),
});
const String = string();
const schema = xor(Named, String, Described);
it("accepts", () => {
expect(
schema.accepts({
id: 12,
name: ["some", "string"],
})
).toBeTruthy();
expect(
schema.accepts({
id: 12,
name: ["some", "string"],
additional: true,
})
).toBeTruthy();
expect(
schema.accepts({ id: 12, name: ["some", "string"], nested: {} })
).toBeTruthy();
expect(
schema.accepts({ id: "", name: ["some", "string"], nested: {} })
).toBeFalsy();
});
it("validates", () => {
expect(
schema.validate({
id: 12,
name: ["some", "string"],
})
).toBeUndefined();
expect(
translate(
schema.validate({
id: 12,
name: ["some", "string"],
nested: { user: "3" },
})
)
).toEqual("xor");
expect(
translate(schema.validate({ id: "", name: ["some", "string"], nested: {} }))
).toEqual({
nested: { user: "value is required" },
});
});
it("validates async", async () => {
const schema = xor(
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v < 4, "must be smaller than 4"))
),
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
expect(await schema.validateAsync(0)).toBeUndefined();
expect(await schema.validateAsync(4)).toBeUndefined();
expect(translate(await schema.validateAsync(1))).toEqual("xor");
expect(translate(await schema.validateAsync(Number.NaN))).toEqual("isNaN");
});
it("parses", () => {
expect(
schema.parse({
nested: { user: "name" },
}).parsedValue
).toEqual({
description: "default",
nested: { user: "name" },
});
expect(
translate(
schema.parse({
id: 12,
name: ["some", "string"],
nested: { user: "3" },
}).validation
)
).toEqual("xor");
expect(translate(schema.parse({}).validation)).toEqual({
nested: "value is required",
});
});
it("parses async", async () => {
const schema = xor(
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v < 4, "must be smaller than 4"))
),
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
const { parsedValue } = await schema.parseAsync(0);
expect(parsedValue).toEqual(0);
const { validation } = await schema.parseAsync(1);
expect(translate(validation)).toEqual("xor");
const { validation: validation2 } = await schema.parseAsync(Number.NaN);
expect(translate(validation2)).toEqual("isNaN");
});
it("builds metadata", () => {
expect(schema.meta().type).toEqual("xor");
expect(schema.meta().schemas[0]).toEqual(Named);
expect(schema.meta().schemas[1]).toEqual(String);
expect(schema.meta().schemas[2]).toEqual(Described);
});