-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
matchers.ts
333 lines (293 loc) · 9.62 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
/** @module Matchers
*
* For specific matcher types (e.g. IpV6), the values generated are not random
* but are fixed, to prevent contract invalidation after each run of the consumer test.
*/
import { isFunction, isNil, isEmpty, isUndefined } from 'lodash';
import { times } from 'ramda';
import { AnyJson, JsonMap } from '../common/jsonTypes';
import MatcherError from '../errors/matcherError';
// Note: The following regexes are Ruby formatted,
// so attempting to parse as JS without modification is probably not going to work as intended!
export const EMAIL_FORMAT = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$';
export const ISO8601_DATE_FORMAT =
'^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)$';
export const ISO8601_DATETIME_FORMAT =
'^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d([+-][0-2]\\d:[0-5]\\d|Z)$';
export const ISO8601_DATETIME_WITH_MILLIS_FORMAT =
'^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d+([+-][0-2]\\d(:?[0-5]\\d)?|Z)$';
export const ISO8601_TIME_FORMAT =
'^(T\\d\\d:\\d\\d(:\\d\\d)?(\\.\\d+)?(([+-]\\d\\d:\\d\\d)|Z)?)?$';
export const RFC1123_TIMESTAMP_FORMAT =
'^(Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s\\d{2}\\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}\\s(\\+|-)\\d{4}$';
export const UUID_V4_FORMAT = '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$';
export const IPV4_FORMAT = '^(\\d{1,3}\\.)+\\d{1,3}$';
export const IPV6_FORMAT =
'^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$';
export const HEX_FORMAT = '^[0-9a-fA-F]+$';
export interface Matcher<T> {
value?: T;
'pact:matcher:type': string;
getValue(): T;
}
export interface ArrayMatcher<T> extends Matcher<T> {
'pact:matcher:type': string;
min?: number;
max?: number;
}
export function isMatcher(x: unknown): x is Matcher<AnyTemplate> {
return x != null && (x as Matcher<AnyTemplate>).getValue !== undefined;
}
export type AnyTemplate =
| AnyJson
| Matcher<AnyTemplate>
| ArrayMatcher<AnyTemplate>
| TemplateMap
| ArrayTemplate;
export type InterfaceToTemplate<O> = { [K in keyof O]: AnyTemplate };
interface TemplateMap {
[key: string]: AnyTemplate;
}
type ArrayTemplate = Array<AnyTemplate>;
/**
* Validates the given example against the regex.
*
* @param example Example to use in the matcher.
* @param matcher Regular expression to check.
*/
export function validateExample(example: string, matcher: string): boolean {
// Note we escape the double \\ as these get sent over the wire as JSON
return new RegExp(matcher.replace('\\\\', '\\')).test(example);
}
/**
* The eachLike matcher
* @param {any} template
* @param {Object} opts
* @param {Number} opts.min
*/
export function eachLike<T>(
template: T,
opts?: { min: number }
): ArrayMatcher<T[]> {
if (isUndefined(template)) {
throw new MatcherError(
'Error creating a Pact eachLike. Please provide a content argument'
);
}
if (opts && (isNil(opts.min) || opts.min < 1)) {
throw new MatcherError(
'Error creating a Pact eachLike. Please provide opts.min that is > 0'
);
}
const min = !isEmpty(opts) && opts ? opts.min : 1;
return {
value: times(() => template, min),
getValue: () => times(() => template, min),
'pact:matcher:type': 'type',
min,
};
}
/**
* The somethingLike matcher
* @param {any} value - the value to be somethingLike
*/
export function somethingLike<T>(value: T): Matcher<T> {
if (isNil(value) || isFunction(value)) {
throw new MatcherError(
'Error creating a Pact somethingLike Match. Value cannot be a function or undefined'
);
}
return {
value,
getValue: () => value,
'pact:matcher:type': 'type',
};
}
export interface RegexMatcher<T> extends Matcher<T> {
regex: string;
}
/**
* The term matcher. Also aliased to 'regex' for discoverability.
* @param {Object} opts
* @param {string} opts.generate - a value to represent the matched String
* @param {string} opts.matcher - a Regex representing the value
*/
export function term(opts: {
generate: string;
matcher: string;
}): RegexMatcher<string> {
const { generate, matcher } = opts;
if (isNil(generate) || isNil(matcher)) {
throw new MatcherError(`Error creating a Pact Term.
Please provide an object containing "generate" and "matcher" properties`);
}
if (!validateExample(generate, matcher)) {
throw new MatcherError(
`Example '${generate}' does not match provided regular expression '${matcher}'`
);
}
return {
getValue: () => generate,
value: generate,
regex: matcher,
'pact:matcher:type': 'regex',
};
}
/**
* Email address matcher.
* @param {string} address - a email address to use as an example
*/
export function email(address?: string): Matcher<string> {
return term({
generate: address || 'hello@pact.io',
matcher: EMAIL_FORMAT,
});
}
/**
* UUID v4 matcher.
* @param {string} id - a UUID to use as an example.
*/
export function uuid(id?: string): Matcher<string> {
return term({
generate: id || 'ce118b6e-d8e1-11e7-9296-cec278b6b50a',
matcher: UUID_V4_FORMAT,
});
}
/**
* IPv4 matcher.
* @param {string} ip - an IPv4 address to use as an example. Defaults to `127.0.0.13`
*/
export function ipv4Address(ip?: string): Matcher<string> {
return term({
generate: ip || '127.0.0.13',
matcher: IPV4_FORMAT,
});
}
/**
* IPv6 matcher.
* @param {string} ip - an IPv6 address to use as an example. Defaults to '::ffff:192.0.2.128'
*/
export function ipv6Address(ip?: string): Matcher<string> {
return term({
generate: ip || '::ffff:192.0.2.128',
matcher: IPV6_FORMAT,
});
}
/**
* ISO8601 DateTime matcher.
* @param {string} date - an ISO8601 Date and Time string
* e.g. 2015-08-06T16:53:10+01:00 are valid
*/
export function iso8601DateTime(date?: string): Matcher<string> {
return term({
generate: date || '2015-08-06T16:53:10+01:00',
matcher: ISO8601_DATETIME_FORMAT,
});
}
/**
* ISO8601 DateTime matcher with required millisecond precision
* @param {string} date - an ISO8601 Date and Time string, e.g. 2015-08-06T16:53:10.123+01:00
*/
export function iso8601DateTimeWithMillis(date?: string): Matcher<string> {
return term({
generate: date || '2015-08-06T16:53:10.123+01:00',
matcher: ISO8601_DATETIME_WITH_MILLIS_FORMAT,
});
}
/**
* ISO8601 Date matcher.
* @param {string} date - a basic yyyy-MM-dd date string e.g. 2000-09-31
*/
export function iso8601Date(date?: string): Matcher<string> {
return term({
generate: date || '2013-02-01',
matcher: ISO8601_DATE_FORMAT,
});
}
/**
* ISO8601 Time Matcher, matches a pattern of the format "'T'HH:mm:ss".
* @param {string} date - a ISO8601 formatted time string e.g. T22:44:30.652Z
*/
export function iso8601Time(time?: string): Matcher<string> {
return term({
generate: time || 'T22:44:30.652Z',
matcher: ISO8601_TIME_FORMAT,
});
}
/**
* RFC1123 Timestamp matcher "DAY, DD MON YYY hh:mm:ss"
*
* @param {string} date - an RFC1123 Date and Time string, e.g. Mon, 31 Oct 2016 15:21:41 -0400
*/
export function rfc1123Timestamp(timestamp?: string): Matcher<string> {
return term({
generate: timestamp || 'Mon, 31 Oct 2016 15:21:41 -0400',
matcher: RFC1123_TIMESTAMP_FORMAT,
});
}
/**
* Hexadecimal Matcher.
* @param {string} hex - a hex value.
*/
export function hexadecimal(hex?: string): Matcher<string> {
return term({
generate: hex || '3F',
matcher: HEX_FORMAT,
});
}
/**
* Decimal Matcher.
* @param {float} float - a decimal value.
*/
export function decimal(float?: number): Matcher<number> {
return somethingLike<number>(isNil(float) ? 13.01 : float);
}
/**
* Integer Matcher.
* @param {integer} int - an int value.
*/
export function integer(int?: number): Matcher<number> {
return somethingLike<number>(isNil(int) ? 13 : int);
}
/**
* Boolean Matcher.
*/
export function boolean(value = true): Matcher<boolean> {
return somethingLike<boolean>(value);
}
/**
* String Matcher.
*/
export function string(value = 'iloveorange'): Matcher<string> {
return somethingLike<string>(value);
}
// Convenience alias'
export { somethingLike as like };
export { term as regex };
// Recurse the object removing any underlying matching guff, returning
// the raw example content
export function extractPayload(value: AnyTemplate): AnyJson {
if (isMatcher(value)) {
return extractPayload(value.getValue());
}
if (Object.prototype.toString.call(value) === '[object Array]') {
return (value as Array<AnyTemplate>).map(extractPayload);
}
if (value !== null && typeof value === 'object') {
return Object.keys(value).reduce(
(acc: JsonMap, propName: string) => ({
...acc,
[propName]: extractPayload(
(value as Record<string, AnyTemplate>)[propName]
),
}),
{}
);
}
return value;
}
// Gets a matcher as JSON or the string value if it's not a matcher
export function matcherValueOrString(obj: unknown): string {
if (typeof obj === 'string') return obj;
return JSON.stringify(obj);
}