-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.test.ts
79 lines (75 loc) · 1.84 KB
/
mod.test.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
import {
isSchema,
isValidSchema,
MaxDepthExceededError,
validate,
} from "./mod.ts";
import { assert, assertEquals, assertThrows } from "./dev_deps.ts";
import { testCases } from "./test_cases.test.ts";
Deno.test({
name: "Schema validity",
fn() {
for (const { description, schema, validSchema } of testCases) {
assertEquals(
isSchema(schema) && isValidSchema(schema),
validSchema,
`${
validSchema ? "Valid" : "Invalid"
} schema in test case with description "${description}" ${
validSchema ? "did not validate" : "was incorrectly validated"
}.`,
);
}
},
});
Deno.test({
name: "Validation supports limited depth",
fn() {
const schema = {
definitions: {
foo: {
ref: "foo",
},
},
ref: "foo",
};
const instance = null;
assertThrows(
() => validate(schema, instance, { maxDepth: 5, maxErrors: 0 }),
MaxDepthExceededError,
);
},
});
Deno.test({
name: "Validation supports limited errors",
fn() {
const schema = {
elements: {
type: "string" as const,
},
};
const instance = [null, null, null, null, null];
assertEquals(
validate(schema, instance, { maxDepth: 0, maxErrors: 3 }).length,
3,
);
},
});
Deno.test({
name: "Validate test cases",
fn() {
const validTestCases = testCases.filter((testCase) => testCase.validSchema);
for (const { description, schema, instance, errors } of validTestCases) {
assert(isSchema(schema));
assertEquals(
validate(schema, instance),
errors,
`${
errors.length ? "Invalid" : "Valid"
} instance in test case with description "${description}" ${
errors.length ? "incorrectly validated" : "did not validate"
}.`,
);
}
},
});