gell-dispatch 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +45 -0
- package/docs/superpowers/plans/2026-11-25-registry.md +215 -0
- package/docs/superpowers/specs/2026-11-25-registry-design.md +87 -0
- package/kind/action.js +6 -0
- package/kind/event.js +38 -0
- package/kind/snapshot.js +6 -0
- package/kind/type.js +21 -0
- package/middleware/arguments.js +37 -37
- package/package.json +7 -1
- package/registry.js +97 -0
- package/test/kind/test-action.js +81 -0
- package/test/kind/test-snapshot.js +81 -0
- package/test/kind/test-type.js +54 -0
- package/test/middleware/test-arguments.js +446 -11
- package/test/test-bus.js +4 -3
- package/test/test-registry.js +151 -0
- package/test/util/test-parameters.js +30 -0
- package/util/parameters.js +22 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const assert = require('assert');
|
|
2
2
|
const _ = require('lodash');
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const { compile } = require('gell-domain/type/compiler');
|
|
5
|
+
|
|
6
|
+
const middleware = require('../../middleware/arguments')(compile());
|
|
5
7
|
|
|
6
8
|
describe('arguments middleware', function() {
|
|
7
9
|
let event, resume, resumed;
|
|
@@ -14,7 +16,8 @@ describe('arguments middleware', function() {
|
|
|
14
16
|
params: {}
|
|
15
17
|
}
|
|
16
18
|
},
|
|
17
|
-
params: {}
|
|
19
|
+
params: {},
|
|
20
|
+
deps: this.deps
|
|
18
21
|
};
|
|
19
22
|
|
|
20
23
|
resume = function() {
|
|
@@ -24,6 +27,20 @@ describe('arguments middleware', function() {
|
|
|
24
27
|
resumed = false;
|
|
25
28
|
})
|
|
26
29
|
|
|
30
|
+
it('requires the compiled types', function() {
|
|
31
|
+
assert.throws(() => require('../../middleware/arguments')(), /compiled types is required/);
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('validates against the types it is given', async function() {
|
|
35
|
+
const word = { name: 'word', rules: { isWord: { test: val => /^\w+$/.test(val), message: () => 'must be one word' } }, default: ['isWord'] };
|
|
36
|
+
const withWord = require('../../middleware/arguments')(compile([word]));
|
|
37
|
+
|
|
38
|
+
event.__invocationSpec.triggerSpec.params.title = 'word';
|
|
39
|
+
event.params.title = 'two words';
|
|
40
|
+
|
|
41
|
+
await assert.rejects(async () => withWord(event, resume), /ParameterValidationError: title must be one word/);
|
|
42
|
+
})
|
|
43
|
+
|
|
27
44
|
describe('no parameters', function() {
|
|
28
45
|
beforeEach(function() {
|
|
29
46
|
})
|
|
@@ -51,8 +68,10 @@ describe('arguments middleware', function() {
|
|
|
51
68
|
assert.fail('should have thrown exception');
|
|
52
69
|
} catch (e) {
|
|
53
70
|
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
54
|
-
assert.strictEqual(e.
|
|
55
|
-
assert.strictEqual(e.
|
|
71
|
+
assert.strictEqual(e.status, 400);
|
|
72
|
+
assert.strictEqual(e.route, 'unittest.event');
|
|
73
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'message', rule: 'isString', message: 'message must be a string' }]);
|
|
74
|
+
assert.strictEqual(e.message, 'message must be a string');
|
|
56
75
|
}
|
|
57
76
|
|
|
58
77
|
assert(!resumed);
|
|
@@ -64,15 +83,31 @@ describe('arguments middleware', function() {
|
|
|
64
83
|
|
|
65
84
|
assert.fail('should have thrown exception');
|
|
66
85
|
} catch (e) {
|
|
67
|
-
assert.strictEqual(e.name, '
|
|
68
|
-
assert.strictEqual(e.
|
|
69
|
-
assert.
|
|
70
|
-
assert.strictEqual(e.message, '
|
|
86
|
+
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
87
|
+
assert.strictEqual(e.status, 400);
|
|
88
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'message', rule: 'required', message: 'message is required' }]);
|
|
89
|
+
assert.strictEqual(e.message, 'message is required');
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
assert(!resumed);
|
|
74
93
|
})
|
|
75
94
|
|
|
95
|
+
for (const blank of [null, '']) {
|
|
96
|
+
it(`fails if required argument is ${JSON.stringify(blank)}`, async function() {
|
|
97
|
+
event.params.message = blank;
|
|
98
|
+
|
|
99
|
+
await assert.rejects(async () => middleware(event, resume), e => e.name === 'ParameterValidationError' && e.status === 400 && e.errors[0].parameter === 'message' && e.errors[0].rule === 'required');
|
|
100
|
+
|
|
101
|
+
assert(!resumed);
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
it('answers a type failure with status 400', async function() {
|
|
106
|
+
event.params.message = 7;
|
|
107
|
+
|
|
108
|
+
await assert.rejects(async () => middleware(event, resume), e => e.name === 'ParameterValidationError' && e.status === 400);
|
|
109
|
+
})
|
|
110
|
+
|
|
76
111
|
describe('optional', function() {
|
|
77
112
|
beforeEach(function() {
|
|
78
113
|
event.__invocationSpec.triggerSpec.params.message.optional = true;
|
|
@@ -84,6 +119,14 @@ describe('arguments middleware', function() {
|
|
|
84
119
|
assert(resumed);
|
|
85
120
|
})
|
|
86
121
|
|
|
122
|
+
it('does not fail if argument is blank', async function() {
|
|
123
|
+
event.params.message = '';
|
|
124
|
+
|
|
125
|
+
await middleware(event, resume);
|
|
126
|
+
|
|
127
|
+
assert(resumed);
|
|
128
|
+
})
|
|
129
|
+
|
|
87
130
|
it('does not fail if argument is provided', async function() {
|
|
88
131
|
event.params.message = 'gello';
|
|
89
132
|
|
|
@@ -109,7 +152,7 @@ describe('arguments middleware', function() {
|
|
|
109
152
|
|
|
110
153
|
describe('max length', function() {
|
|
111
154
|
beforeEach(function() {
|
|
112
|
-
event.__invocationSpec.triggerSpec.params.message.
|
|
155
|
+
event.__invocationSpec.triggerSpec.params.message.validate = { maxLength: 10 };
|
|
113
156
|
event.params.message = 'gello world!';
|
|
114
157
|
})
|
|
115
158
|
|
|
@@ -120,11 +163,403 @@ describe('arguments middleware', function() {
|
|
|
120
163
|
assert.fail('should have thrown exception');
|
|
121
164
|
} catch (e) {
|
|
122
165
|
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
123
|
-
assert.strictEqual(e.
|
|
166
|
+
assert.strictEqual(e.errors[0].rule, 'maxLength');
|
|
167
|
+
assert.strictEqual(e.message, 'message must be at most 10 characters');
|
|
124
168
|
}
|
|
125
169
|
|
|
126
170
|
assert(!resumed);
|
|
127
171
|
})
|
|
128
172
|
})
|
|
129
173
|
})
|
|
130
|
-
|
|
174
|
+
|
|
175
|
+
describe('shorthand spec', function() {
|
|
176
|
+
beforeEach(function() {
|
|
177
|
+
event.__invocationSpec.triggerSpec.params.message = 'string';
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('fails if required argument is not provided', async function() {
|
|
181
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: message is required/);
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('fails if argument is not a string', async function() {
|
|
185
|
+
event.params.message = 7;
|
|
186
|
+
|
|
187
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError/);
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
describe('several parameters', function() {
|
|
192
|
+
beforeEach(function() {
|
|
193
|
+
Object.assign(event.__invocationSpec.triggerSpec.params, { title: 'string', id: { type: 'string' } });
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('reports every failure in one error', async function() {
|
|
197
|
+
event.params.id = 7;
|
|
198
|
+
|
|
199
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
200
|
+
assert.strictEqual(e.errors.length, 2);
|
|
201
|
+
assert.strictEqual(e.message, 'title is required; id must be a string');
|
|
202
|
+
|
|
203
|
+
return true;
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
assert(!resumed);
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('logs each failure', async function() {
|
|
210
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError/);
|
|
211
|
+
|
|
212
|
+
assert.deepStrictEqual(this.logger.messages, [
|
|
213
|
+
{ level: 'info', message: 'parameter validation failed (route="unittest.event"; parameter="title"; rule="required")' },
|
|
214
|
+
{ level: 'info', message: 'parameter validation failed (route="unittest.event"; parameter="id"; rule="required")' },
|
|
215
|
+
]);
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
describe('enum', function() {
|
|
220
|
+
beforeEach(function() {
|
|
221
|
+
event.__invocationSpec.triggerSpec.params.color = { type: 'string', optional: true, validate: { enum: ['red', 'blue'] } };
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
it('accepts a listed value', async function() {
|
|
225
|
+
event.params.color = 'red';
|
|
226
|
+
|
|
227
|
+
await middleware(event, resume);
|
|
228
|
+
|
|
229
|
+
assert(resumed);
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('does not check an omitted optional argument', async function() {
|
|
233
|
+
await middleware(event, resume);
|
|
234
|
+
|
|
235
|
+
assert(resumed);
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('fails for a value that is not listed', async function() {
|
|
239
|
+
event.params.color = 'plaid';
|
|
240
|
+
|
|
241
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
242
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'color', rule: 'enum', message: 'color must be one of red, blue' }]);
|
|
243
|
+
assert.strictEqual(e.status, 400);
|
|
244
|
+
|
|
245
|
+
return true;
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
assert(!resumed);
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
describe('string rules', function() {
|
|
253
|
+
function spec(validate, others={}) {
|
|
254
|
+
event.__invocationSpec.triggerSpec.params.title = { type: 'string', ...others, validate };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function rejects(title, expected) {
|
|
258
|
+
event.params.title = title;
|
|
259
|
+
|
|
260
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
261
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'title', ...expected }]);
|
|
262
|
+
|
|
263
|
+
return true;
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
assert(!resumed);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function accepts(title) {
|
|
270
|
+
event.params.title = title;
|
|
271
|
+
|
|
272
|
+
await middleware(event, resume);
|
|
273
|
+
|
|
274
|
+
assert(resumed);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
it('minLength 1 says the argument cannot be empty', async function() {
|
|
278
|
+
spec({ minLength: 1 }, { optional: true });
|
|
279
|
+
|
|
280
|
+
await rejects('', { rule: 'minLength', message: 'title cannot be empty' });
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it('minLength', async function() {
|
|
284
|
+
spec({ minLength: 3 });
|
|
285
|
+
|
|
286
|
+
await rejects('ab', { rule: 'minLength', message: 'title must be at least 3 characters' });
|
|
287
|
+
await accepts('abc');
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('maxLength', async function() {
|
|
291
|
+
spec({ maxLength: 2 });
|
|
292
|
+
|
|
293
|
+
await rejects('abc', { rule: 'maxLength', message: 'title must be at most 2 characters' });
|
|
294
|
+
await accepts('ab');
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('pattern (string)', async function() {
|
|
298
|
+
spec({ pattern: '^[A-Z]' });
|
|
299
|
+
|
|
300
|
+
await rejects('abc', { rule: 'pattern', message: 'title must match ^[A-Z]' });
|
|
301
|
+
await accepts('Abc');
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('reports only the first failing rule of an argument', async function() {
|
|
305
|
+
spec({ minLength: 3, pattern: '^[A-Z]' });
|
|
306
|
+
|
|
307
|
+
await rejects('a', { rule: 'minLength', message: 'title must be at least 3 characters' });
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
it('messages overrides a rule\'s default message', async function() {
|
|
311
|
+
spec({ pattern: '^\\S', messages: { pattern: 'title cannot start with a space' } });
|
|
312
|
+
|
|
313
|
+
await rejects(' abc', { rule: 'pattern', message: 'title cannot start with a space' });
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('does not apply string rules to an omitted optional argument', async function() {
|
|
317
|
+
spec({ minLength: 3, pattern: '^[A-Z]' }, { optional: true });
|
|
318
|
+
|
|
319
|
+
await accepts(undefined);
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
describe('validate function', function() {
|
|
324
|
+
let calls;
|
|
325
|
+
|
|
326
|
+
beforeEach(function() {
|
|
327
|
+
calls = [];
|
|
328
|
+
|
|
329
|
+
Object.assign(event.__invocationSpec.triggerSpec.params, {
|
|
330
|
+
startTs: 'number',
|
|
331
|
+
endTs: {
|
|
332
|
+
type: 'number',
|
|
333
|
+
validate: (endTs, params) => {
|
|
334
|
+
calls.push([endTs, params]);
|
|
335
|
+
|
|
336
|
+
return params.startTs < endTs || 'startTs must be before endTs';
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
it('passes the value and a map of the other arguments', async function() {
|
|
343
|
+
event.params = { startTs: 1, endTs: 2 };
|
|
344
|
+
|
|
345
|
+
await middleware(event, resume);
|
|
346
|
+
|
|
347
|
+
assert.deepStrictEqual(calls, [[2, { startTs: 1 }]]);
|
|
348
|
+
assert(resumed);
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('fails with the message the function returns', async function() {
|
|
352
|
+
event.params = { startTs: 2, endTs: 1 };
|
|
353
|
+
|
|
354
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
355
|
+
assert.strictEqual(e.status, 400);
|
|
356
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'endTs', rule: 'validate', message: 'startTs must be before endTs' }]);
|
|
357
|
+
|
|
358
|
+
return true;
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
assert(!resumed);
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
it('fails with a generic message when the function returns false', async function() {
|
|
365
|
+
event.__invocationSpec.triggerSpec.params.endTs.validate = () => false;
|
|
366
|
+
event.params = { startTs: 1, endTs: 2 };
|
|
367
|
+
|
|
368
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: endTs is not valid/);
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
it('passes when the function returns undefined', async function() {
|
|
372
|
+
event.__invocationSpec.triggerSpec.params.endTs.validate = () => undefined;
|
|
373
|
+
event.params = { startTs: 1, endTs: 2 };
|
|
374
|
+
|
|
375
|
+
await middleware(event, resume);
|
|
376
|
+
|
|
377
|
+
assert(resumed);
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
it('is not called for a missing required argument', async function() {
|
|
381
|
+
event.params = { startTs: 1 };
|
|
382
|
+
|
|
383
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: endTs is required/);
|
|
384
|
+
|
|
385
|
+
assert.deepStrictEqual(calls, []);
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('is not called for an omitted optional argument', async function() {
|
|
389
|
+
event.__invocationSpec.triggerSpec.params.endTs.optional = true;
|
|
390
|
+
event.params = { startTs: 1 };
|
|
391
|
+
|
|
392
|
+
await middleware(event, resume);
|
|
393
|
+
|
|
394
|
+
assert.deepStrictEqual(calls, []);
|
|
395
|
+
assert(resumed);
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
it('is not called when the type check fails', async function() {
|
|
399
|
+
event.__invocationSpec.triggerSpec.params.endTs.type = 'string';
|
|
400
|
+
event.params = { startTs: 1, endTs: 2 };
|
|
401
|
+
|
|
402
|
+
await assert.rejects(async () => middleware(event, resume), /endTs must be a string/);
|
|
403
|
+
|
|
404
|
+
assert.deepStrictEqual(calls, []);
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('propagates an error the function throws', async function() {
|
|
408
|
+
event.__invocationSpec.triggerSpec.params.endTs.validate = () => { throw new Error('broken check'); };
|
|
409
|
+
event.params = { startTs: 1, endTs: 2 };
|
|
410
|
+
|
|
411
|
+
await assert.rejects(async () => middleware(event, resume), /broken check/);
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
describe('number rules', function() {
|
|
416
|
+
beforeEach(function() {
|
|
417
|
+
event.__invocationSpec.triggerSpec.params.limit = { type: 'number', optional: true, validate: { min: 1, max: 1000 } };
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
it('fails a number out of range', async function() {
|
|
421
|
+
event.params.limit = '0';
|
|
422
|
+
|
|
423
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
424
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'limit', rule: 'min', message: 'limit must be at least 1' }]);
|
|
425
|
+
|
|
426
|
+
return true;
|
|
427
|
+
});
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
it('fails a value that is not a number', async function() {
|
|
431
|
+
event.params.limit = 'lots';
|
|
432
|
+
|
|
433
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: limit must be a number/);
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
it('passes a blank optional argument', async function() {
|
|
437
|
+
event.params.limit = '';
|
|
438
|
+
|
|
439
|
+
await middleware(event, resume);
|
|
440
|
+
|
|
441
|
+
assert(resumed);
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
it('reports a blank required argument as required', async function() {
|
|
445
|
+
event.__invocationSpec.triggerSpec.params.limit.optional = false;
|
|
446
|
+
event.params.limit = '';
|
|
447
|
+
|
|
448
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: limit is required/);
|
|
449
|
+
})
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
describe('enum by type', function() {
|
|
453
|
+
it('number enum matches a numeric string (GET argument)', async function() {
|
|
454
|
+
event.__invocationSpec.triggerSpec.params.size = { type: 'number', validate: { enum: [1, 2, 4] } };
|
|
455
|
+
event.params.size = '2';
|
|
456
|
+
|
|
457
|
+
await middleware(event, resume);
|
|
458
|
+
|
|
459
|
+
assert(resumed);
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
it('number enum fails a value that is not listed', async function() {
|
|
463
|
+
event.__invocationSpec.triggerSpec.params.size = { type: 'number', validate: { enum: [1, 2, 4] } };
|
|
464
|
+
event.params.size = 3;
|
|
465
|
+
|
|
466
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: size must be one of 1, 2, 4/);
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
it('an enum that is not an array is a metadata error, not a validation failure', async function() {
|
|
470
|
+
event.__invocationSpec.triggerSpec.params.size = { type: 'number', validate: { enum: 2 } };
|
|
471
|
+
event.params.size = 2;
|
|
472
|
+
|
|
473
|
+
await assert.rejects(async () => middleware(event, resume), e => e.name === 'AssertionError' && /rule argument is not an array \(type=number; rule=enum/.test(e.message));
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
it('a rule the type does not define is a metadata error, not a validation failure', async function() {
|
|
477
|
+
event.__invocationSpec.triggerSpec.params.pinned = { type: 'boolean', validate: { enum: [true] } };
|
|
478
|
+
event.params.pinned = true;
|
|
479
|
+
|
|
480
|
+
await assert.rejects(async () => middleware(event, resume), e => e.name === 'AssertionError' && /no rule defined \(type=boolean; rule=enum\)/.test(e.message));
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
it('a type without its own enum is matched exactly', async function() {
|
|
484
|
+
event.__invocationSpec.triggerSpec.params.id = { type: 'email', validate: { enum: ['a'] } };
|
|
485
|
+
event.params.id = 'b';
|
|
486
|
+
|
|
487
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: id must be one of a/);
|
|
488
|
+
})
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
describe('array items', function() {
|
|
492
|
+
function spec(items, others={}) {
|
|
493
|
+
event.__invocationSpec.triggerSpec.params.tags = { type: 'array', validate: { items, ...others } };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
it('checks each item against its type and rules', async function() {
|
|
497
|
+
spec({ type: 'string', validate: { maxLength: 3 } });
|
|
498
|
+
event.params.tags = ['a', 'abcd'];
|
|
499
|
+
|
|
500
|
+
await assert.rejects(async () => middleware(event, resume), e => {
|
|
501
|
+
assert.deepStrictEqual(e.errors, [{ parameter: 'tags', rule: 'items', message: 'tags[1] must be at most 3 characters' }]);
|
|
502
|
+
|
|
503
|
+
return true;
|
|
504
|
+
});
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
it('accepts string shorthand for the items spec', async function() {
|
|
508
|
+
spec('number');
|
|
509
|
+
event.params.tags = [1, '2', 'x'];
|
|
510
|
+
|
|
511
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: tags\[2\] must be a number/);
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
it('reports a missing item as required', async function() {
|
|
515
|
+
spec('string');
|
|
516
|
+
event.params.tags = ['a', null];
|
|
517
|
+
|
|
518
|
+
await assert.rejects(async () => middleware(event, resume), /tags\[1\] is required/);
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
it('checks an item enum', async function() {
|
|
522
|
+
spec({ type: 'string', validate: { enum: ['red', 'blue'] } });
|
|
523
|
+
event.params.tags = ['red', 'plaid'];
|
|
524
|
+
|
|
525
|
+
await assert.rejects(async () => middleware(event, resume), /tags\[1\] must be one of red, blue/);
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
it('runs an item validate function', async function() {
|
|
529
|
+
spec({ type: 'string', validate: tag => tag !== 'x' || 'x is reserved' });
|
|
530
|
+
event.params.tags = ['a', 'x'];
|
|
531
|
+
|
|
532
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: x is reserved/);
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
it('checks items of nested arrays', async function() {
|
|
536
|
+
spec({ type: 'array', validate: { items: 'number' } });
|
|
537
|
+
event.params.tags = [[1, 2], [3, 'x']];
|
|
538
|
+
|
|
539
|
+
await assert.rejects(async () => middleware(event, resume), /tags\[1\]\[1\] must be a number/);
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
it('checks the array rules before its items', async function() {
|
|
543
|
+
spec('string', { maxItems: 2 });
|
|
544
|
+
event.params.tags = [1, 2, 3];
|
|
545
|
+
|
|
546
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: tags must have at most 2 items$/);
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
it('reports only the first failing item', async function() {
|
|
550
|
+
spec('string');
|
|
551
|
+
event.params.tags = ['a', 1, 2];
|
|
552
|
+
|
|
553
|
+
await assert.rejects(async () => middleware(event, resume), /ParameterValidationError: tags\[1\] must be a string$/);
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
it('passes valid items', async function() {
|
|
557
|
+
spec({ type: 'string', validate: { maxLength: 3 } }, { maxItems: 2 });
|
|
558
|
+
event.params.tags = ['a', 'abc'];
|
|
559
|
+
|
|
560
|
+
await middleware(event, resume);
|
|
561
|
+
|
|
562
|
+
assert(resumed);
|
|
563
|
+
})
|
|
564
|
+
})
|
|
565
|
+
})
|
package/test/test-bus.js
CHANGED
|
@@ -2,12 +2,13 @@ const assert = require('assert');
|
|
|
2
2
|
const _ = require('lodash');
|
|
3
3
|
|
|
4
4
|
const { default: init } = require('gell-future');
|
|
5
|
+
const { compile } = require('gell-domain/type/compiler');
|
|
5
6
|
|
|
6
7
|
const bus = require('../bus');
|
|
7
8
|
const resolver = require('./resolver');
|
|
8
9
|
|
|
9
10
|
const attributeMiddleware = require('../middleware/attributes');
|
|
10
|
-
const argumentMiddleware = require('../middleware/arguments');
|
|
11
|
+
const argumentMiddleware = require('../middleware/arguments')(compile());
|
|
11
12
|
|
|
12
13
|
const EVENTS = {
|
|
13
14
|
nometadata: {
|
|
@@ -203,8 +204,8 @@ describe('event bus', function() {
|
|
|
203
204
|
|
|
204
205
|
// console.debug('#####', e);
|
|
205
206
|
|
|
206
|
-
assert.strictEqual(name, '
|
|
207
|
-
assert.strictEqual(message, '
|
|
207
|
+
assert.strictEqual(name, 'ParameterValidationError');
|
|
208
|
+
assert.strictEqual(message, 'message is required');
|
|
208
209
|
}
|
|
209
210
|
})
|
|
210
211
|
|