-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
validation.js
254 lines (197 loc) · 5.9 KB
/
validation.js
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
const _ = require('lodash')
const debug = require('debug')('cypress:server:validation')
const is = require('check-more-types')
const { commaListsOr } = require('common-tags')
// validation functions take a key and a value and should:
// - return true if it passes validation
// - return a error message if it fails validation
const str = JSON.stringify
/**
* Forms good Markdown-like string message.
* @param {string} key - The key that caused the error
* @param {string} type - The expected type name
* @param {any} value - The actual value
* @returns {string} Formatted error message
*/
const errMsg = (key, value, type) => {
return `Expected \`${key}\` to be ${type}. Instead the value was: \`${str(
value,
)}\``
}
const isFullyQualifiedUrl = (value) => {
return isString(value) && /^https?\:\/\//.test(value)
}
const isArrayOfStrings = (value) => {
return isArray(value) && _.every(value, isString)
}
const isFalse = (value) => {
return value === false
}
const { isArray } = _
const isNumber = _.isFinite
const { isString } = _
/**
* Validates a single browser object.
* @returns {string|true} Returns `true` if the object is matching browser object schema. Returns an error message if it does not.
*/
const isValidBrowser = (browser) => {
if (!is.unemptyString(browser.name)) {
return errMsg('name', browser, 'a non-empty string')
}
// TODO: this is duplicated with browsers/index
const knownBrowserFamilies = ['chromium', 'firefox']
if (!is.oneOf(knownBrowserFamilies)(browser.family)) {
return errMsg('family', browser, commaListsOr`either ${knownBrowserFamilies}`)
}
if (!is.unemptyString(browser.displayName)) {
return errMsg('displayName', browser, 'a non-empty string')
}
if (!is.unemptyString(browser.version)) {
return errMsg('version', browser, 'a non-empty string')
}
if (!is.string(browser.path)) {
return errMsg('path', browser, 'a string')
}
if (!is.string(browser.majorVersion) && !is.positive(browser.majorVersion)) {
return errMsg('majorVersion', browser, 'a string or a positive number')
}
return true
}
/**
* Validates the list of browsers.
*/
const isValidBrowserList = (key, browsers) => {
debug('browsers %o', browsers)
if (!browsers) {
return 'Missing browsers list'
}
if (!Array.isArray(browsers)) {
debug('browsers is not an array', typeof browsers)
return 'Browsers should be an array'
}
if (!browsers.length) {
return 'Expected at least one browser'
}
for (let k = 0; k < browsers.length; k += 1) {
const err = isValidBrowser(browsers[k])
if (err !== true) {
return `Found an error while validating the \`browsers\` list. ${err}`
}
}
return true
}
const isValidRetriesConfig = (key, value) => {
const optionalKeys = ['runMode', 'openMode']
const isValidRetryValue = (val) => _.isNull(val) || (Number.isInteger(val) && val >= 0)
const optionalKeysAreValid = (val, k) => optionalKeys.includes(k) && isValidRetryValue(val)
if (isValidRetryValue(value)) {
return true
}
if (_.isObject(value) && _.every(value, optionalKeysAreValid)) {
return true
}
return errMsg(key, value, 'a positive number or null or an object with keys "openMode" and "runMode" with values of numbers or nulls')
}
const isValidFirefoxGcInterval = (key, value) => {
const isIntervalValue = (val) => {
if (isNumber(val)) {
return val >= 0
}
return val == null
}
if (isIntervalValue(value)
|| (_.isEqual(_.keys(value), ['runMode', 'openMode'])
&& isIntervalValue(value.runMode)
&& isIntervalValue(value.openMode))) {
return true
}
return errMsg(key, value, 'a positive number or null or an object with "openMode" and "runMode" as keys and positive numbers or nulls as values')
}
const isOneOf = (...values) => {
return (key, value) => {
if (values.some((v) => {
if (typeof value === 'function') {
return value(v)
}
return v === value
})) {
return true
}
const strings = values.map(str).join(', ')
return errMsg(key, value, `one of these values: ${strings}`)
}
}
module.exports = {
isValidBrowser,
isValidBrowserList,
isValidFirefoxGcInterval,
isValidRetriesConfig,
isNumber (key, value) {
if (value == null || isNumber(value)) {
return true
}
return errMsg(key, value, 'a number')
},
isNumberOrFalse (key, value) {
if (isNumber(value) || isFalse(value)) {
return true
}
return errMsg(key, value, 'a number or false')
},
isFullyQualifiedUrl (key, value) {
if (value == null || isFullyQualifiedUrl(value)) {
return true
}
return errMsg(
key,
value,
'a fully qualified URL (starting with `http://` or `https://`)',
)
},
isBoolean (key, value) {
if (value == null || _.isBoolean(value)) {
return true
}
return errMsg(key, value, 'a boolean')
},
isPlainObject (key, value) {
if (value == null || _.isPlainObject(value)) {
return true
}
return errMsg(key, value, 'a plain object')
},
isString (key, value) {
if (value == null || isString(value)) {
return true
}
return errMsg(key, value, 'a string')
},
isArray (key, value) {
if (value == null || isArray(value)) {
return true
}
return errMsg(key, value, 'an array')
},
isStringOrFalse (key, value) {
if (isString(value) || isFalse(value)) {
return true
}
return errMsg(key, value, 'a string or false')
},
isStringOrArrayOfStrings (key, value) {
if (isString(value) || isArrayOfStrings(value)) {
return true
}
return errMsg(key, value, 'a string or an array of strings')
},
/**
* Checks if given value for a key is equal to one of the provided values.
* @example
```
validate = v.isOneOf("foo", "bar")
validate("example", "foo") // true
validate("example", "else") // error message string
```
*/
isOneOf,
}