-
Notifications
You must be signed in to change notification settings - Fork 1
/
path-validation.spec.ts
93 lines (83 loc) · 2.55 KB
/
path-validation.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
/* eslint-disable unicorn/no-useless-undefined */
/* eslint-disable unicorn/no-null */
import { array, map, object, set } from "../composite";
import { nonNegative, number, string } from "../simple";
import { ValidationIssue, ValidationResult } from "../validation";
import {
PathValidationResult,
fromPathValidation,
toPathValidation,
} from "./path-validation";
const schema = nonNegative(number());
const issue = new ValidationIssue("nonNegative", undefined, -1);
it("handles valid results", () => {
check(undefined, { validations: [], hints: [] });
});
it("handles primitives", () => {
const validation = schema.validate({ a: -1 });
check(validation, { validations: [{ issue, path: "" }], hints: [] });
});
it("handles objects", () => {
const validation = object({ a: schema }).validate({ a: -1 });
check(validation, { validations: [{ issue, path: "a" }], hints: [] });
});
it("handles nested objects", () => {
const validation = object({
a: object({ a: schema }),
}).validate({ a: { a: -1 } });
check(validation, { validations: [{ issue, path: "a.a" }], hints: [] });
});
it("handles arrays", () => {
const validation = array(schema).validate([1, -1]);
check(validation, { validations: [{ issue, path: "[1]" }], hints: [] });
});
it("handles sets", () => {
const validation = set(schema).validate(new Set([1, -1]));
check(validation, {
validations: [{ issue, path: "[0]" }],
hints: [{ hint: "Set", path: "" }],
});
});
it("handles maps", () => {
const validation = map(string(), schema).validate(new Map([["key", -1]]));
check(validation, {
validations: [{ issue, path: "['key']" }],
hints: [{ hint: "Map", path: "" }],
});
});
it("handles larger validations objects", () => {
const validation = {
a: {
b: issue,
},
c: [issue, { d: issue }],
m: new Map([
["key", issue],
["object", { c: issue } as unknown],
]),
d: new Set([issue, { d: issue }]),
};
check(validation as unknown as ValidationResult<unknown>, {
validations: [
{ issue, path: "a.b" },
{ issue, path: "c[0]" },
{ issue, path: "c[1].d" },
{ issue, path: "m['key']" },
{ issue, path: "m['object'].c" },
{ issue, path: "d[0]" },
{ issue, path: "d[1].d" },
],
hints: [
{ hint: "Map", path: "m" },
{ hint: "Set", path: "d" },
],
});
});
function check<T>(
validation: ValidationResult<T>,
expected: PathValidationResult
) {
const path = toPathValidation(validation);
expect(path).toEqual(expected);
expect(fromPathValidation(path)).toEqual(validation);
}