-
Notifications
You must be signed in to change notification settings - Fork 1
/
or.spec.ts
122 lines (113 loc) · 2.96 KB
/
or.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
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 { optional } from "../utility/optional";
import { translate } from "../validation";
import { or } from "./or";
const Named = object({
id: number(),
name: array(string()),
});
const Described = object({
id: number(),
description: optional(string()),
nested: object({
user: string(),
}),
});
const String = string();
const schema = or(Named, String, Described);
it("accepts", () => {
expect(
schema.accepts({
id: 12,
name: ["some", "string"],
nested: { user: "3" },
})
).toBeTruthy();
expect(
schema.accepts({
id: 12,
name: ["some", "string"],
nested: { user: "3", 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"],
nested: { user: "3" },
})
).toBeUndefined();
expect(
schema.validate({ id: 12, name: ["some", "string"], nested: {} })
).toBeUndefined();
expect(
translate(schema.validate({ id: "", name: ["some", "string"], nested: {} }))
).toEqual({
id: "value was of type string expected number",
nested: { user: "value is required" },
});
});
it("validates async", async () => {
const schema = or(
string(),
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
expect(await schema.validateAsync("")).toBeUndefined();
expect(await schema.validateAsync(1)).toBeUndefined();
expect(translate(await schema.validateAsync(0))).toEqual("must be positive");
});
it("parses", () => {
expect(
schema.parse({
id: 12,
name: ["some", "string"],
nested: { user: "3" },
}).parsedValue
).toEqual({
id: 12,
name: ["some", "string"],
});
expect(
translate(
schema.parse({
id: "12",
name: ["some", "string"],
nested: { user: "3" },
}).validation
)
).toEqual({
id: "value was of type string expected number",
});
});
it("parses async", async () => {
const schema = or(
string(),
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
const { parsedValue } = await schema.parseAsync(12);
expect(parsedValue).toEqual(12);
const { validation } = await schema.parseAsync(0);
expect(translate(validation)).toEqual("must be positive");
});
it("builds metadata", () => {
expect(schema.meta().type).toEqual("or");
expect(schema.meta().schemas[0]).toEqual(Named);
expect(schema.meta().schemas[1]).toEqual(String);
expect(schema.meta().schemas[2]).toEqual(Described);
});