-
Notifications
You must be signed in to change notification settings - Fork 1
/
and.spec.ts
117 lines (108 loc) · 2.88 KB
/
and.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
import { array } from "../composite/array";
import { object } from "../composite/object";
import { refineAsync } from "../refine";
import { integer, number } from "../simple/number";
import { string } from "../simple/string";
import { procedure } from "../utility";
import { defaultValue, optional } from "../utility/optional";
import { translate } from "../validation";
import { and } from "./and";
const Named = object({
id: number(),
name: array(string()),
getAge: optional(procedure<[], number>()),
});
const Described = object({
id: number(),
description: defaultValue(optional(string()), "defaultValue"),
nested: object({
user: string(),
}),
});
const Empty = object({});
const schema = and(Named, Empty, 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: {} })
).toBeFalsy();
expect(
schema.accepts({ id: "", name: ["some", "string"], nested: {} })
).toBeFalsy();
});
it("validates", () => {
expect(
schema.validate({
id: 12,
name: ["some", "string"],
nested: { user: "3" },
})
).toBeUndefined();
expect(
translate(schema.validate({ id: "", name: ["some", "string"], nested: {} }))
).toEqual({
id: "value was of type string expected number",
nested: { user: "value is required" },
});
expect(
translate(schema.validate({ id: 12, name: ["some", "string"], nested: {} }))
).toEqual({
nested: { user: "value is required" },
});
});
it("validates with early exit", () => {
expect(
translate(
schema.validate(
{ id: "", name: ["some", "string"], nested: {} },
{ earlyExit: true }
)
)
).toEqual({
id: "value was of type string expected number",
});
});
it("validates async", async () => {
const schema = and(
integer(number()),
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
expect(await schema.validateAsync(1)).toBeUndefined();
expect(translate(await schema.validateAsync(1.2))).toEqual("integer");
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"],
description: "defaultValue",
nested: { user: "3" },
});
});
it("builds metadata", () => {
expect(schema.meta().type).toEqual("and");
expect(schema.meta().schemas[0]).toEqual(Named);
expect(schema.meta().schemas[1]).toEqual(Empty);
expect(schema.meta().schemas[2]).toEqual(Described);
});