string.js
13.8 KB
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
// Load modules
var Net = require('net');
var Hoek = require('hoek');
var Isemail = require('isemail');
var Any = require('./any');
var Ref = require('./ref');
var JoiDate = require('./date');
var Errors = require('./errors');
var Uri = require('./string/uri');
var Ip = require('./string/ip');
// Declare internals
var internals = {
uriRegex: Uri.createUriRegex(),
ipRegex: Ip.createIpRegex(['ipv4', 'ipv6', 'ipvfuture'], 'optional')
};
internals.String = function () {
Any.call(this);
this._type = 'string';
this._invalids.add('');
};
Hoek.inherits(internals.String, Any);
internals.compare = function (type, compare) {
return function (limit, encoding) {
var isRef = Ref.isRef(limit);
Hoek.assert((Hoek.isInteger(limit) && limit >= 0) || isRef, 'limit must be a positive integer or reference');
Hoek.assert(!encoding || Buffer.isEncoding(encoding), 'Invalid encoding:', encoding);
return this._test(type, limit, function (value, state, options) {
var compareTo;
if (isRef) {
compareTo = limit(state.parent, options);
if (!Hoek.isInteger(compareTo)) {
return Errors.create('string.ref', { ref: limit.key }, state, options);
}
}
else {
compareTo = limit;
}
if (compare(value, compareTo, encoding)) {
return null;
}
return Errors.create('string.' + type, { limit: compareTo, value: value, encoding: encoding }, state, options);
});
};
};
internals.String.prototype._base = function (value, state, options) {
if (typeof value === 'string' &&
options.convert) {
if (this._flags.case) {
value = (this._flags.case === 'upper' ? value.toLocaleUpperCase() : value.toLocaleLowerCase());
}
if (this._flags.trim) {
value = value.trim();
}
if (this._inner.replacements) {
for (var r = 0, rl = this._inner.replacements.length; r < rl; ++r) {
var replacement = this._inner.replacements[r];
value = value.replace(replacement.pattern, replacement.replacement);
}
}
}
return {
value: value,
errors: (typeof value === 'string') ? null : Errors.create('string.base', { value: value }, state, options)
};
};
internals.String.prototype.insensitive = function () {
var obj = this.clone();
obj._flags.insensitive = true;
return obj;
};
internals.String.prototype.min = internals.compare('min', function (value, limit, encoding) {
var length = encoding ? Buffer.byteLength(value, encoding) : value.length;
return length >= limit;
});
internals.String.prototype.max = internals.compare('max', function (value, limit, encoding) {
var length = encoding ? Buffer.byteLength(value, encoding) : value.length;
return length <= limit;
});
internals.String.prototype.creditCard = function () {
return this._test('creditCard', undefined, function (value, state, options) {
var i = value.length;
var sum = 0;
var mul = 1;
var char;
while (i--) {
char = value.charAt(i) * mul;
sum += char - (char > 9) * 9;
mul ^= 3;
}
var check = (sum % 10 === 0) && (sum > 0);
return check ? null : Errors.create('string.creditCard', { value: value }, state, options);
});
};
internals.String.prototype.length = internals.compare('length', function (value, limit, encoding) {
var length = encoding ? Buffer.byteLength(value, encoding) : value.length;
return length === limit;
});
internals.String.prototype.regex = function (pattern, name) {
Hoek.assert(pattern instanceof RegExp, 'pattern must be a RegExp');
pattern = new RegExp(pattern.source, pattern.ignoreCase ? 'i' : undefined); // Future version should break this and forbid unsupported regex flags
return this._test('regex', pattern, function (value, state, options) {
if (pattern.test(value)) {
return null;
}
return Errors.create((name ? 'string.regex.name' : 'string.regex.base'), { name: name, pattern: pattern, value: value }, state, options);
});
};
internals.String.prototype.alphanum = function () {
return this._test('alphanum', undefined, function (value, state, options) {
if (/^[a-zA-Z0-9]+$/.test(value)) {
return null;
}
return Errors.create('string.alphanum', { value: value }, state, options);
});
};
internals.String.prototype.token = function () {
return this._test('token', undefined, function (value, state, options) {
if (/^\w+$/.test(value)) {
return null;
}
return Errors.create('string.token', { value: value }, state, options);
});
};
internals.String.prototype.email = function (isEmailOptions) {
if (isEmailOptions) {
Hoek.assert(typeof isEmailOptions === 'object', 'email options must be an object');
Hoek.assert(typeof isEmailOptions.checkDNS === 'undefined', 'checkDNS option is not supported');
Hoek.assert(typeof isEmailOptions.tldWhitelist === 'undefined' ||
typeof isEmailOptions.tldWhitelist === 'object', 'tldWhitelist must be an array or object');
Hoek.assert(typeof isEmailOptions.minDomainAtoms === 'undefined' ||
Hoek.isInteger(isEmailOptions.minDomainAtoms) && isEmailOptions.minDomainAtoms > 0,
'minDomainAtoms must be a positive integer');
Hoek.assert(typeof isEmailOptions.errorLevel === 'undefined' || typeof isEmailOptions.errorLevel === 'boolean' ||
(Hoek.isInteger(isEmailOptions.errorLevel) && isEmailOptions.errorLevel >= 0),
'errorLevel must be a non-negative integer or boolean');
}
return this._test('email', isEmailOptions, function (value, state, options) {
try {
var result = Isemail(value, isEmailOptions);
if (result === true || result === 0) {
return null;
}
}
catch (e) {}
return Errors.create('string.email', { value: value }, state, options);
});
};
internals.String.prototype.ip = function (ipOptions) {
var regex = internals.ipRegex;
ipOptions = ipOptions || {};
Hoek.assert(typeof ipOptions === 'object', 'options must be an object');
if (ipOptions.cidr) {
Hoek.assert(typeof ipOptions.cidr === 'string', 'cidr must be a string');
ipOptions.cidr = ipOptions.cidr.toLowerCase();
Hoek.assert(ipOptions.cidr in Ip.cidrs, 'cidr must be one of ' + Object.keys(Ip.cidrs).join(', '));
// If we only received a `cidr` setting, create a regex for it. But we don't need to create one if `cidr` is "optional" since that is the default
if (!ipOptions.version && ipOptions.cidr !== 'optional') {
regex = Ip.createIpRegex(['ipv4', 'ipv6', 'ipvfuture'], ipOptions.cidr);
}
}
else {
// Set our default cidr strategy
ipOptions.cidr = 'optional';
}
if (ipOptions.version) {
if (!Array.isArray(ipOptions.version)) {
ipOptions.version = [ipOptions.version];
}
Hoek.assert(ipOptions.version.length >= 1, 'version must have at least 1 version specified');
var versions = [];
for (var i = 0, il = ipOptions.version.length; i < il; ++i) {
var version = ipOptions.version[i];
Hoek.assert(typeof version === 'string', 'version at position ' + i + ' must be a string');
version = version.toLowerCase();
Hoek.assert(Ip.versions[version], 'version at position ' + i + ' must be one of ' + Object.keys(Ip.versions).join(', '));
versions.push(version);
}
// Make sure we have a set of versions
versions = Hoek.unique(versions);
regex = Ip.createIpRegex(versions, ipOptions.cidr);
}
return this._test('ip', ipOptions, function (value, state, options) {
if (regex.test(value)) {
return null;
}
if (versions) {
return Errors.create('string.ipVersion', { value: value, cidr: ipOptions.cidr, version: versions }, state, options);
}
return Errors.create('string.ip', { value: value, cidr: ipOptions.cidr }, state, options);
});
};
internals.String.prototype.uri = function (uriOptions) {
var customScheme = '',
regex = internals.uriRegex;
if (uriOptions) {
Hoek.assert(typeof uriOptions === 'object', 'options must be an object');
if (uriOptions.scheme) {
Hoek.assert(uriOptions.scheme instanceof RegExp || typeof uriOptions.scheme === 'string' || Array.isArray(uriOptions.scheme), 'scheme must be a RegExp, String, or Array');
if (!Array.isArray(uriOptions.scheme)) {
uriOptions.scheme = [uriOptions.scheme];
}
Hoek.assert(uriOptions.scheme.length >= 1, 'scheme must have at least 1 scheme specified');
// Flatten the array into a string to be used to match the schemes.
for (var i = 0, il = uriOptions.scheme.length; i < il; ++i) {
var scheme = uriOptions.scheme[i];
Hoek.assert(scheme instanceof RegExp || typeof scheme === 'string', 'scheme at position ' + i + ' must be a RegExp or String');
// Add OR separators if a value already exists
customScheme += customScheme ? '|' : '';
// If someone wants to match HTTP or HTTPS for example then we need to support both RegExp and String so we don't escape their pattern unknowingly.
if (scheme instanceof RegExp) {
customScheme += scheme.source;
}
else {
Hoek.assert(/[a-zA-Z][a-zA-Z0-9+-\.]*/.test(scheme), 'scheme at position ' + i + ' must be a valid scheme');
customScheme += Hoek.escapeRegex(scheme);
}
}
}
}
if (customScheme) {
regex = Uri.createUriRegex(customScheme);
}
return this._test('uri', uriOptions, function (value, state, options) {
if (regex.test(value)) {
return null;
}
if (customScheme) {
return Errors.create('string.uriCustomScheme', { scheme: customScheme, value: value }, state, options);
}
return Errors.create('string.uri', { value: value }, state, options);
});
};
internals.String.prototype.isoDate = function () {
return this._test('isoDate', undefined, function (value, state, options) {
if (JoiDate._isIsoDate(value)) {
return null;
}
return Errors.create('string.isoDate', { value: value }, state, options);
});
};
internals.String.prototype.guid = function () {
var regex = /^[A-F0-9]{8}(?:-?[A-F0-9]{4}){3}-?[A-F0-9]{12}$/i;
var regex2 = /^\{[A-F0-9]{8}(?:-?[A-F0-9]{4}){3}-?[A-F0-9]{12}\}$/i;
return this._test('guid', undefined, function (value, state, options) {
if (regex.test(value) || regex2.test(value)) {
return null;
}
return Errors.create('string.guid', { value: value }, state, options);
});
};
internals.String.prototype.hex = function () {
var regex = /^[a-f0-9]+$/i;
return this._test('hex', regex, function (value, state, options) {
if (regex.test(value)) {
return null;
}
return Errors.create('string.hex', { value: value }, state, options);
});
};
internals.String.prototype.hostname = function () {
var regex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
return this._test('hostname', undefined, function (value, state, options) {
if ((value.length <= 255 && regex.test(value)) ||
Net.isIPv6(value)) {
return null;
}
return Errors.create('string.hostname', { value: value }, state, options);
});
};
internals.String.prototype.lowercase = function () {
var obj = this._test('lowercase', undefined, function (value, state, options) {
if (options.convert ||
value === value.toLocaleLowerCase()) {
return null;
}
return Errors.create('string.lowercase', { value: value }, state, options);
});
obj._flags.case = 'lower';
return obj;
};
internals.String.prototype.uppercase = function () {
var obj = this._test('uppercase', undefined, function (value, state, options) {
if (options.convert ||
value === value.toLocaleUpperCase()) {
return null;
}
return Errors.create('string.uppercase', { value: value }, state, options);
});
obj._flags.case = 'upper';
return obj;
};
internals.String.prototype.trim = function () {
var obj = this._test('trim', undefined, function (value, state, options) {
if (options.convert ||
value === value.trim()) {
return null;
}
return Errors.create('string.trim', { value: value }, state, options);
});
obj._flags.trim = true;
return obj;
};
internals.String.prototype.replace = function (pattern, replacement) {
if (typeof pattern === 'string') {
pattern = new RegExp(Hoek.escapeRegex(pattern), 'g');
}
Hoek.assert(pattern instanceof RegExp, 'pattern must be a RegExp');
Hoek.assert(typeof replacement === 'string', 'replacement must be a String');
// This can not be considere a test like trim, we can't "reject"
// anything from this rule, so just clone the current object
var obj = this.clone();
if (!obj._inner.replacements) {
obj._inner.replacements = [];
}
obj._inner.replacements.push({
pattern: pattern,
replacement: replacement
});
return obj;
};
module.exports = new internals.String();