-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
matchers.ts
232 lines (206 loc) · 7.14 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
/** @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, isUndefined } from "lodash";
// Note: The following regexes are Ruby formatted,
// so attempting to parse as JS without modification is probably not going to work as intended!
/* tslint:disable:max-line-length */
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{3}([+-][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 RFC3339_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]+$";
/* tslint:enable */
/**
* 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 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 }) {
const generate = opts.generate;
const matcher = opts.matcher;
if (isNil(generate) || isNil(matcher)) {
throw new Error(`Error creating a Pact Term.
Please provide an object containing "generate" and "matcher" properties`);
}
if (!validateExample(generate, matcher)) {
throw new Error(`Example '${generate}' does not match provided regular expression '${matcher}'`);
}
return {
data: {
generate,
matcher: {
json_class: "Regexp",
o: 0,
s: matcher,
},
},
json_class: "Pact::Term",
};
}
/**
* UUID v4 matcher.
* @param {string} uuuid - a UUID to use as an example.
*/
export function uuid(id?: 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) {
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) {
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) {
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) {
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) {
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) {
return term({
generate: time || "T22:44:30.652Z",
matcher: ISO8601_TIME_FORMAT,
});
}
/**
* RFC3339 Timestamp matcher, a subset of ISO8609
* @param {string} date - an RFC3339 Date and Time string, e.g. Mon, 31 Oct 2016 15:21:41 -0400
*/
export function rfc3339Timestamp(timestamp?: string) {
return term({
generate: timestamp || "Mon, 31 Oct 2016 15:21:41 -0400",
matcher: RFC3339_TIMESTAMP_FORMAT,
});
}
/**
* Hexadecimal Matcher.
* @param {string} hex - a hex value.
*/
export function hexadecimal(hex?: string) {
return term({
generate: hex || "3F",
matcher: HEX_FORMAT,
});
}
/**
* Decimal Matcher.
* @param {float} float - a decimal value.
*/
export function decimal(float?: number) {
return somethingLike<number>(float || 13.01);
}
/**
* Integer Matcher.
* @param {integer} int - an int value.
*/
export function integer(int?: number) {
return somethingLike<number>(int || 13);
}
/**
* Boolean Matcher.
*/
export function boolean() {
return somethingLike<boolean>(true);
}
/**
* The eachLike matcher
* @param {any} content
* @param {Object} opts
* @param {Number} opts.min
*/
export function eachLike<T>(content: T, opts?: { min: number }) {
if (isUndefined(content)) {
throw new Error("Error creating a Pact eachLike. Please provide a content argument");
}
if (opts && (isNil(opts.min) || opts.min < 1)) {
throw new Error("Error creating a Pact eachLike. Please provide opts.min that is > 0");
}
return {
contents: content,
json_class: "Pact::ArrayLike",
min: isUndefined(opts) ? 1 : opts.min,
};
}
/**
* The somethingLike matcher
* @param {any} value - the value to be somethingLike
*/
export function somethingLike<T>(value: T) {
if (isNil(value) || isFunction(value)) {
throw new Error("Error creating a Pact somethingLike Match. Value cannot be a function or undefined");
}
return {
contents: value,
json_class: "Pact::SomethingLike",
};
}
// Convenience alias'
export { somethingLike as like };
export { term as regex };
export interface MatcherResult {
json_class: string;
}