-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.test.ts
277 lines (249 loc) · 11.5 KB
/
validation.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Schema, validateSchema } from "../src/runtime/validation";
describe("validateSchema", () => {
test("simple use", () => {
expect(validateSchema({ a: "hey" }, { a: String })).toEqual([]);
expect(validateSchema({ a: 1 }, { a: Boolean })).toEqual(["'a' is not a boolean"]);
});
test("simple use, alternate form", () => {
expect(validateSchema({ a: "hey" }, { a: { type: String } })).toEqual([]);
expect(validateSchema({ a: 1 }, { a: { type: Boolean } })).toEqual(["'a' is not a boolean"]);
});
test("some particular edgecases as key name", () => {
expect(validateSchema({ shape: "hey" }, { shape: String })).toEqual([]);
expect(validateSchema({ shape: 1 }, { shape: Boolean })).toEqual(["'shape' is not a boolean"]);
expect(validateSchema({ element: "hey" }, { element: String })).toEqual([]);
expect(validateSchema({ element: 1 }, { element: Boolean })).toEqual([
"'element' is not a boolean",
]);
});
test("multiple errors", () => {
expect(validateSchema({ a: 1, b: 2 }, { a: Boolean, b: Boolean })).toEqual([
"'a' is not a boolean",
"'b' is not a boolean",
]);
});
test("missing key", () => {
expect(validateSchema({}, { a: Boolean })).toEqual(["'a' is missing (should be a boolean)"]);
});
test("additional key", () => {
expect(validateSchema({ b: 1 }, {})).toEqual(["unknown key 'b'"]);
});
test("undefined key", () => {
expect(validateSchema({ a: undefined }, { a: Boolean })).toEqual([
"'a' is undefined (should be a boolean)",
]);
expect(validateSchema({}, { a: Boolean })).toEqual(["'a' is missing (should be a boolean)"]);
});
test("can use '*' to denote any type", () => {
expect(validateSchema({ a: "hey" }, { a: "*" })).toEqual([]);
expect(validateSchema({}, { a: "*" })).toEqual(["'a' is missing"]);
});
test("an union of type", () => {
expect(validateSchema({ a: "hey" }, { a: [String, Boolean] })).toEqual([]);
expect(validateSchema({ a: 1 }, { a: [String, Boolean] })).toEqual([
"'a' is not a string or boolean",
]);
expect(validateSchema({ a: "hey" }, { a: { type: [String, Boolean] } })).toEqual([]);
});
test("another union of types", () => {
const schema: Schema = {
id: Number,
url: [Boolean, { type: Array, element: Number }],
};
expect(validateSchema({ a: "hey" }, schema)).toEqual([
"unknown key 'a'",
"'id' is missing (should be a number)",
"'url' is missing (should be a boolean or list of numbers)",
]);
expect(validateSchema({ id: 1 }, schema)).toEqual([
"'url' is missing (should be a boolean or list of numbers)",
]);
expect(validateSchema({ id: 1, url: true }, schema)).toEqual([]);
expect(validateSchema({ id: true, url: true }, schema)).toEqual(["'id' is not a number"]);
expect(validateSchema({ id: 3, url: 3 }, schema)).toEqual([
"'url' is not a boolean or list of numbers",
]);
});
test("simplified schema description", () => {
expect(validateSchema({ a: "hey" }, ["a"])).toEqual([]);
expect(validateSchema({ b: 1 }, ["a"])).toEqual(["unknown key 'b'", "'a' is missing"]);
});
test("simplified schema description with optional props and *", () => {
expect(validateSchema({ a: "hey" }, ["a", "b?", "*"])).toEqual([]);
expect(validateSchema({ a: "hey" }, ["a", "*"])).toEqual([]);
expect(validateSchema({ a: "hey", b: 1, c: 3 }, ["a", "*"])).toEqual([]);
});
test("simplified schema description with optional props", () => {
expect(validateSchema({ a: "hey" }, ["a", "b?"])).toEqual([]);
expect(validateSchema({ a: "hey", b: 1 }, ["a", "b?"])).toEqual([]);
});
test("object type description, with no type/optional key", () => {
expect(validateSchema({ a: "hey" }, { a: {} })).toEqual([]);
expect(validateSchema({ a: 1 }, { a: {} })).toEqual([]);
expect(validateSchema({}, { a: {} })).toEqual(["'a' is missing"]);
});
test("optional key", () => {
expect(validateSchema({}, { a: { optional: true } })).toEqual([]);
expect(validateSchema({}, { a: { type: Number, optional: true } })).toEqual([]);
expect(validateSchema({ a: undefined }, { a: { type: Number, optional: true } })).toEqual([]);
expect(validateSchema({ a: 2 }, { a: { optional: true } })).toEqual([]);
expect(validateSchema({ a: undefined }, { a: { optional: true } })).toEqual([]);
expect(validateSchema({ a: 2 }, { a: { type: Number, optional: true } })).toEqual([]);
expect(validateSchema({ a: 2 }, { a: { type: String, optional: true } })).toEqual([
"'a' is not a string",
]);
});
test("can validate dates", () => {
expect(validateSchema({ a: new Date() }, { a: Date })).toEqual([]);
expect(validateSchema({ a: 4 }, { a: Date })).toEqual(["'a' is not a date"]);
});
test("arrays with simple element description", () => {
const schema: Schema = { p: { type: Array, element: String } };
expect(validateSchema({ p: [] }, schema)).toEqual([]);
expect(validateSchema({ p: 1 }, schema)).toEqual(["'p' is not a list of strings"]);
expect(validateSchema({}, schema)).toEqual(["'p' is missing (should be a list of strings)"]);
expect(validateSchema({ p: undefined }, schema)).toEqual([
"'p' is undefined (should be a list of strings)",
]);
expect(validateSchema({ p: ["a"] }, schema)).toEqual([]);
expect(validateSchema({ p: [1] }, schema)).toEqual(["'p[0]' is not a string"]);
});
test("arrays with union type as element description", () => {
const schema: Schema = { p: { type: Array, element: [String, Boolean] } };
expect(validateSchema({ p: [] }, schema)).toEqual([]);
expect(validateSchema({ p: 1 }, schema)).toEqual(["'p' is not a list of string or booleans"]);
expect(validateSchema({}, schema)).toEqual([
"'p' is missing (should be a list of string or booleans)",
]);
expect(validateSchema({ p: undefined }, schema)).toEqual([
"'p' is undefined (should be a list of string or booleans)",
]);
expect(validateSchema({ p: ["a"] }, schema)).toEqual([]);
expect(validateSchema({ p: [1] }, schema)).toEqual(["'p[0]' is not a string or boolean"]);
expect(validateSchema({ p: [true, 1] }, schema)).toEqual(["'p[1]' is not a string or boolean"]);
});
test("objects with specified shape", () => {
const schema: Schema = { p: { type: Object, shape: { id: Number, url: String } } };
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
expect(validateSchema({ p: {} }, schema)).toEqual([
"'p' has not the correct shape ('id' is missing (should be a number), 'url' is missing (should be a string))",
]);
expect(validateSchema({ p: { id: 1, url: "asf" } }, schema)).toEqual([]);
expect(validateSchema({ p: { id: 1, url: 1 } }, schema)).toEqual([
"'p' has not the correct shape ('url' is not a string)",
]);
expect(validateSchema({ p: undefined }, schema)).toEqual([
"'p' is undefined (should be a object)",
]);
});
test("objects with more complex shape", () => {
const schema: Schema = {
p: {
type: Object,
shape: {
id: Number,
url: [Boolean, { type: Array, element: Number }],
},
},
};
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
expect(validateSchema({ p: {} }, schema)).toEqual([
"'p' has not the correct shape ('id' is missing (should be a number), 'url' is missing (should be a boolean or list of numbers))",
]);
expect(validateSchema({ p: { id: 1, url: "asf" } }, schema)).toEqual([
"'p' has not the correct shape ('url' is not a boolean or list of numbers)",
]);
expect(validateSchema({ p: { id: 1, url: true } }, schema)).toEqual([]);
expect(validateSchema({ p: undefined }, schema)).toEqual([
"'p' is undefined (should be a object)",
]);
});
test("objects with shape and *", () => {
const schema: Schema = { p: { type: Object, shape: { id: Number, "*": true } } };
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
expect(validateSchema({ p: {} }, schema)).toEqual([
"'p' has not the correct shape ('id' is missing (should be a number))",
]);
expect(validateSchema({ p: { id: 1 } }, schema)).toEqual([]);
expect(validateSchema({ p: { id: "asdf" } }, schema)).toEqual([
"'p' has not the correct shape ('id' is not a number)",
]);
expect(validateSchema({ p: { id: 1, url: 1 } }, schema)).toEqual([]);
expect(validateSchema({ p: undefined }, schema)).toEqual([
"'p' is undefined (should be a object)",
]);
});
test("can specify that additional keys are allowed", () => {
const schema: Schema = {
message: String,
"*": true,
};
expect(validateSchema({ message: "hey" }, schema)).toEqual([]);
expect(validateSchema({ message: "hey", otherKey: true }, schema)).toEqual([]);
});
test("array with element with shape", () => {
const schema: Schema = {
p: {
type: Array,
element: {
type: Object,
shape: {
num: { type: Number, optional: true },
},
},
},
};
expect(validateSchema({ p: 1 }, schema)).toEqual(["'p' is not a list of objects"]);
expect(validateSchema({ p: {} }, schema)).toEqual(["'p' is not a list of objects"]);
expect(validateSchema({ p: [] }, schema)).toEqual([]);
expect(validateSchema({ p: [{}] }, schema)).toEqual([]);
expect(validateSchema({ p: [{ num: 1 }] }, schema)).toEqual([]);
expect(validateSchema({ p: [{ num: true }] }, schema)).toEqual([
"'p[0]' has not the correct shape ('num' is not a number)",
]);
});
test("schema with custom validate function", () => {
const schema: Schema = {
size: {
validate: (e: string) => ["small", "medium", "large"].includes(e),
},
};
expect(validateSchema({ size: "small" }, schema)).toEqual([]);
expect(validateSchema({ size: "sall" }, schema)).toEqual(["'size' is not valid"]);
expect(validateSchema({ size: 1 }, schema)).toEqual(["'size' is not valid"]);
});
test("schema with custom validate function and type", () => {
const schema: Schema = {
size: {
type: String,
validate: (e: string) => ["small", "medium", "large"].includes(e),
},
};
expect(validateSchema({ size: "small" }, schema)).toEqual([]);
expect(validateSchema({ size: "sall" }, schema)).toEqual(["'size' is not valid"]);
expect(validateSchema({ size: 1 }, schema)).toEqual(["'size' is not a string"]);
});
test("value as type", () => {
expect(validateSchema({ a: false }, { a: { value: false } })).toEqual([]);
expect(validateSchema({ a: true }, { a: { value: false } })).toEqual([
"'a' is not equal to 'false'",
]);
});
test("value as type (some other values)", () => {
expect(validateSchema({ a: null }, { a: { value: null } })).toEqual([]);
expect(validateSchema({ a: false }, { a: { value: null } })).toEqual([
"'a' is not equal to 'null'",
]);
expect(validateSchema({ a: "hey" }, { a: { value: "hey" } })).toEqual([]);
expect(validateSchema({ a: true }, { a: { value: "hey" } })).toEqual([
"'a' is not equal to 'hey'",
]);
});
test("value as type work in union type", () => {
expect(validateSchema({ a: false }, { a: [String, { value: false }] })).toEqual([]);
expect(validateSchema({ a: true }, { a: [String, { value: false }] })).toEqual([
"'a' is not a string or false",
]);
expect(validateSchema({ a: "string" }, { a: [String, { value: false }] })).toEqual([]);
});
});