-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
matchers.ts
578 lines (532 loc) · 15.7 KB
/
matchers.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import { isNil, pickBy, times } from 'ramda';
import RandExp from 'randexp';
import {
ArrayContainsMatcher,
DateTimeMatcher,
Matcher,
MaxLikeMatcher,
MinLikeMatcher,
ProviderStateInjectedValue,
RulesMatcher,
V3RegexMatcher,
} from './types';
import { AnyJson, JsonMap } from '../common/jsonTypes';
export * from './types';
export function isMatcher(x: unknown): x is Matcher<unknown> {
return (
x != null &&
(x as Matcher<unknown>)['pact:matcher:type'] !== undefined &&
(x as Matcher<unknown>).value !== undefined
);
}
/**
* Value must match the given template
* @param template Template to base the comparison on
*/
export const like = <T>(template: T): Matcher<T> => ({
'pact:matcher:type': 'type',
value: template,
});
/**
* Object where the key itself is ignored, but the value template must match.
*
* @deprecated use eachKeyMatches or eachValueMatches
* @param keyTemplate Example key to use
* @param template Example value template to base the comparison on
*/
export const eachKeyLike = <T>(
keyTemplate: string,
template: T
): Matcher<T> => ({
'pact:matcher:type': 'values',
value: {
[keyTemplate]: template,
},
});
/**
* Object where the _keys_ must match the supplied matchers.
* The values for each key are ignored. That is, there can be 0 or more keys
* with any valid JSON identifier, so long as the names of the keys match the constraints.
*
* @param example Example object with key/values e.g. `{ foo: 'bar', baz: 'qux'}`
* @param matchers Matchers to apply to each key
*/
export const eachKeyMatches = (
example: Record<string, unknown>,
matchers: Matcher<string> | Matcher<string>[] = like('key')
): RulesMatcher<unknown> => ({
'pact:matcher:type': 'eachKey',
rules: Array.isArray(matchers) ? matchers : [matchers],
value: example,
});
/**
* Object where the _values_ must match the supplied matchers.
* The names of the keys are ignored. That is, there can be 0 or more keys
* with any valid JSON identifier, so long as the values match the constraints.
*
* @param example Example object with key/values e.g. `{ foo: 'bar', baz: 'qux'}`
* @param matchers Matchers to apply to each value
*/
export const eachValueMatches = <T>(
example: Record<string, T>,
matchers: Matcher<T> | Matcher<T>[]
): RulesMatcher<T> => ({
'pact:matcher:type': 'eachValue',
rules: Array.isArray(matchers) ? matchers : [matchers],
value: example,
// Unsure if the full object is provided, or just a template k/v pair
// value: {
// [keyTemplate]: template,
// },
});
/**
* Array where each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
*/
export const eachLike = <T>(template: T, min = 1): MinLikeMatcher<T[]> => {
const elements = min;
return {
min,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* An array that has to have at least one element and each element must match the given template
* @param template Template to base the comparison on
* @param count Number of examples to generate, defaults to one
*/
export const atLeastOneLike = <T>(
template: T,
count = 1
): MinLikeMatcher<T[]> => ({
min: 1,
'pact:matcher:type': 'type',
value: times(() => template, count),
});
/**
* An array that has to have at least the required number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
* @param count Number of examples to generate, defaults to min
*/
export const atLeastLike = <T>(
template: T,
min: number,
count?: number
): MinLikeMatcher<T[]> => {
const elements = count || min;
if (count && count < min) {
throw new Error(
`atLeastLike has a minimum of ${min} but ${count} elements were requested.` +
` Make sure the count is greater than or equal to the min.`
);
}
return {
min,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* An array that has to have at most the required number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param max Maximum number of elements required in the array
* @param count Number of examples to generate, defaults to one
*/
export const atMostLike = <T>(
template: T,
max: number,
count?: number
): MaxLikeMatcher<T[]> => {
const elements = count || 1;
if (count && count > max) {
throw new Error(
`atMostLike has a maximum of ${max} but ${count} elements where requested.` +
` Make sure the count is less than or equal to the max.`
);
}
return {
max,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* An array whose size is constrained to the minimum and maximum number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
* @param max Maximum number of elements required in the array
* @param count Number of examples to generate, defaults to one
*/
export const constrainedArrayLike = <T>(
template: T,
min: number,
max: number,
count?: number
): MinLikeMatcher<T[]> & MaxLikeMatcher<T[]> => {
const elements = count || min;
if (count) {
if (count < min) {
throw new Error(
`constrainedArrayLike has a minimum of ${min} but ${count} elements where requested.` +
` Make sure the count is greater than or equal to the min.`
);
} else if (count > max) {
throw new Error(
`constrainedArrayLike has a maximum of ${max} but ${count} elements where requested.` +
` Make sure the count is less than or equal to the max.`
);
}
}
return {
min,
max,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* Value must be a boolean
* @param b Boolean example value. Defaults to true if unsupplied
*/
export const boolean = (b = true): Matcher<boolean> => ({
'pact:matcher:type': 'type',
value: b,
});
/**
* Value must be an integer (must be a number and have no decimal places)
* @param int Example value. If omitted a random value will be generated.
*/
export const integer = (int?: number): Matcher<number> => {
if (Number.isInteger(int)) {
return {
'pact:matcher:type': 'integer',
value: int,
};
}
if (int) {
throw new Error(
`The integer matcher was passed '${int}' which is not an integer.`
);
}
return {
'pact:generator:type': 'RandomInt',
'pact:matcher:type': 'integer',
value: 101,
};
};
/**
* Value must be a decimal number (must be a number and have decimal places)
* @param num Example value. If omitted a random value will be generated.
*/
export const decimal = (num?: number): Matcher<number> => {
if (Number.isFinite(num)) {
return {
'pact:matcher:type': 'decimal',
value: num,
};
}
if (num) {
throw new Error(
`The decimal matcher was passed '${num}' which is not a number.`
);
}
return {
'pact:generator:type': 'RandomDecimal',
'pact:matcher:type': 'decimal',
value: 12.34,
};
};
/**
* Value must be a number
* @param num Example value. If omitted a random integer value will be generated.
*/
export function number(num?: number): Matcher<number> {
if (typeof num === 'number') {
return {
'pact:matcher:type': 'number',
value: num,
};
}
if (num) {
throw new Error(
`The number matcher was passed '${num}' which is not a number.`
);
}
return {
'pact:generator:type': 'RandomInt',
'pact:matcher:type': 'number',
value: 1234,
};
}
/**
* Value must be a string
* @param str Example value
*/
export function string(str = 'some string'): Matcher<string> {
return {
'pact:matcher:type': 'type',
value: str,
};
}
/**
* Value that must match the given regular expression
* @param pattern Regular Expression to match
* @param str Example value
*/
export function regex(pattern: RegExp | string, str: string): V3RegexMatcher {
if (pattern instanceof RegExp) {
return {
'pact:matcher:type': 'regex',
regex: pattern.source,
value: str,
};
}
return {
'pact:matcher:type': 'regex',
regex: pattern,
value: str,
};
}
/**
* Value that must be equal to the example. This is mainly used to reset the matching rules which cascade.
* @param value Example value
*/
export const equal = <T>(value: T): Matcher<T> => ({
'pact:matcher:type': 'equality',
value,
});
/**
* String value that must match the provided datetime format string.
* @param format Datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date and time will be generated.
*/
export function datetime(format: string, example: string): DateTimeMatcher {
if (!example) {
throw new Error(`you must provide an example datetime`);
}
return pickBy((v) => !isNil(v), {
'pact:generator:type': example ? undefined : 'DateTime',
'pact:matcher:type': 'timestamp',
format,
value: example,
});
}
/**
* String value that must match the provided datetime format string.
* @param format Datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date and time will be generated.
*/
export function timestamp(format: string, example: string): DateTimeMatcher {
if (!example) {
throw new Error(`you must provide an example timestamp`);
}
return datetime(format, example);
}
/**
* String value that must match the provided time format string.
* @param format Time format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system time will be generated.
*/
export function time(format: string, example: string): DateTimeMatcher {
if (!example) {
throw new Error(`you must provide an example time`);
}
return {
'pact:generator:type': 'Time',
'pact:matcher:type': 'time',
format,
value: example,
};
}
/**
* String value that must match the provided date format string.
* @param format Date format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date will be generated.
*/
export function date(format: string, example: string): DateTimeMatcher {
if (!example) {
throw new Error(`you must provide an example date`);
}
return {
format,
'pact:generator:type': 'Date',
'pact:matcher:type': 'date',
value: example,
};
}
/**
* Value that must include the example value as a substring.
* @param value String value to include
*/
export function includes(value: string): Matcher<string> {
return {
'pact:matcher:type': 'include',
value,
};
}
/**
* Value that must be null. This will only match the JSON Null value. For other content types, it will
* match if the attribute is missing.
*/
export function nullValue(): Matcher<null> {
return {
'pact:matcher:type': 'null',
value: null,
};
}
function stringFromRegex(r: RegExp): string {
return new RandExp(r).gen();
}
/**
* Matches a URL composed of a base path and a list of path fragments
* @param basePath Base path of the URL. If null, will use the base URL from the mock server.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url2(
basePath: string | null,
pathFragments: Array<string | V3RegexMatcher | RegExp>
): V3RegexMatcher {
const regexpr = [
'.*(',
...pathFragments.map((p) => {
if (p instanceof RegExp) {
return `\\/${p.source}`;
}
if (p instanceof Object && p['pact:matcher:type'] === 'regex') {
return `\\/${p.regex}`;
}
return `\\/${p.toString()}`;
}),
].join('');
const example = [
basePath || 'http://localhost:8080',
...pathFragments.map((p) => {
if (p instanceof RegExp) {
return `/${stringFromRegex(p)}`;
}
if (p instanceof Object && p['pact:matcher:type'] === 'regex') {
return `/${p.value}`;
}
return `/${p.toString()}`;
}),
].join('');
// Temporary fix for inconsistencies between matchers and generators. Matchers use "value" attribute for
// example values, while generators use "example"
if (basePath == null) {
return {
'pact:matcher:type': 'regex',
'pact:generator:type': 'MockServerURL',
regex: `${regexpr})$`,
value: example,
example,
};
}
return {
'pact:matcher:type': 'regex',
regex: `${regexpr})$`,
value: example,
};
}
/**
* Matches a URL composed of a list of path fragments. The base URL from the mock server will be used.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url(
pathFragments: Array<string | V3RegexMatcher | RegExp>
): V3RegexMatcher {
return url2(null, pathFragments);
}
/**
* Matches the items in an array against a number of variants. Matching is successful if each variant
* occurs once in the array. Variants may be objects containing matching rules.
*/
export function arrayContaining(...variants: unknown[]): ArrayContainsMatcher {
return {
'pact:matcher:type': 'arrayContains',
variants,
};
}
/**
* Marks an item to be injected from the provider state
* @param expression Expression to lookup in the provider state context
* @param exampleValue Example value to use in the consumer test
*/
export function fromProviderState<V>(
expression: string,
exampleValue: V
): ProviderStateInjectedValue<V> {
return {
'pact:matcher:type': 'type',
'pact:generator:type': 'ProviderState',
expression,
value: exampleValue,
};
}
/**
* Match a universally unique identifier (UUID). Random values will be used for examples if no example is given.
*/
export function uuid(example?: string): V3RegexMatcher {
const regexStr =
'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
if (example) {
const regexpr = new RegExp(`^${regexStr}$`);
if (!example.match(regexpr)) {
throw new Error(
`regex: Example value '${example}' does not match the UUID regular expression '${regexStr}'`
);
}
return {
'pact:matcher:type': 'regex',
regex: regexStr,
value: example,
};
}
return {
'pact:matcher:type': 'regex',
regex: regexStr,
'pact:generator:type': 'Uuid',
value: 'e2490de5-5bd3-43d5-b7c4-526e33f71304',
};
}
export const matcherValueOrString = (obj: unknown): string => {
if (typeof obj === 'string') return obj;
return JSON.stringify(obj);
};
/**
* Recurse the object removing any underlying matching guff, returning the raw
* example content.
*/
export function reify(input: unknown): AnyJson {
if (isMatcher(input)) {
return reify(input.value);
}
if (Array.isArray(input)) {
return input.map(reify);
}
if (typeof input === 'object') {
if (input === null) {
return input;
}
return Object.keys(input).reduce(
(acc: JsonMap, propName: keyof typeof input) => ({
...acc,
[propName]: reify(input[propName]),
}),
{}
);
}
if (
typeof input === 'number' ||
typeof input === 'string' ||
typeof input === 'boolean'
) {
return input;
}
throw new Error(
`Unable to strip matcher from a '${typeof input}', as it is not valid in a Pact description`
);
}
export { reify as extractPayload };