-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
78 lines (61 loc) · 2.11 KB
/
index.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
(function () {
'use strict';
var validator = require('validator'),
moment = require('moment'),
dateTimeRegExp = require('./src/dateTimeRegExp'),
uriRegExp = require('./src/uriRegExp'),
durationRegExp = require('./src/durationRegExp'),
timeOffsetRegExp = require('./src/timeOffsetRegExp'),
dateRegExp = /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/,
guidRegExp =
/^\{?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?$/;
exports.date = function (value) {
if (dateRegExp.test(value) && moment(value, 'YYYY-MM-DD').isValid()) {
return null;
}
return 'A valid date in YYYY-MM-DD format expected';
};
exports['date-time'] = function (value) {
if (
dateTimeRegExp.test(value) &&
moment(value).isValid()
) {
return null;
}
return 'A valid ISO 8601 date/time string expected';
};
exports.email = function (value) {
if (validator.isEmail(value)) {
return null;
}
return 'E-mail address expected';
};
exports.uri = function (value) {
if (uriRegExp.test(value)) {
return null;
}
return 'URI expected';
};
exports.url = function (value) {
var tldRequired = value.indexOf('localhost') === -1;
if (validator.isURL(value, { 'require_tld': tldRequired })) {
return null;
}
return 'URL expected';
};
exports['credit-card-number'] = function (value) {
if (validator.isCreditCard(value)) {
return null;
}
return 'A valid credit card number format expected';
};
exports.duration = function (value) {
return durationRegExp.test(value) ? null : 'ISO 8601 duration is expected';
};
exports['time-offset'] = function (value) {
return timeOffsetRegExp.test(value) ? null : 'A signed ISO 8601 duration is expected';
};
exports.guid = function (value) {
return guidRegExp.test(value) ? null : 'A valid GUID is expected';
};
}());