-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (158 loc) · 7.06 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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validationAdapter = exports.FormStateAdapter = exports.aliases = exports.content = exports.library = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _default = require('./content/en-us/default.js');
var _default2 = _interopRequireDefault(_default);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var library = exports.library = {
email: function email(value) {
return library.regex(value, /^\S+@\S+\.\S+$/);
},
equals: function equals(value, baseline) {
return value === baseline;
},
exists: function exists(value) {
return value !== undefined && value !== null;
},
greaterThan: function greaterThan(value, baseline) {
return library.required(value) && Number(value) > baseline;
},
integer: function integer(value) {
return library.regex(value, /^-?[0-9]+$/);
},
length: function length(value, len) {
return library.exists(value) && library.exists(value.length) && value.length === len;
},
lessThan: function lessThan(value, baseline) {
return library.required(value) && Number(value) < baseline;
},
max: function max(value, baseline) {
return library.required(value) && Number(value) <= baseline;
},
maxLength: function maxLength(value, len) {
return library.exists(value) && library.exists(value.length) && value.length <= len;
},
min: function min(value, baseline) {
return library.required(value) && Number(value) >= baseline;
},
minLength: function minLength(value, len) {
return library.exists(value) && library.exists(value.length) && value.length >= len;
},
number: function number(value) {
return library.required(value) && !Number.isNaN(Number(value));
},
numeric: function numeric(value) {
return library.regex(value, /^[0-9]+$/);
},
regex: function regex(value, pattern) {
return library.required(value) && pattern.test(value);
},
required: function required(value) {
return typeof value === 'string' && value.trim() !== '';
},
startsWith: function startsWith(value, searchString) {
return library.required(value) && library.exists(searchString) && value.substr(0, searchString.length) === searchString;
},
url: function url(value) {
// matches blank strings so you have a choice of pairing it with required
if (value === null || typeof value === 'string' && value.trim() === '') {
return true;
}
//
// https://gist.github.com/dperini/729294
//
var re_weburl = new RegExp("^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" + "(?:" +
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" + "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "|" +
// host name
'(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)' +
// domain name
'(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*' +
// TLD identifier
'(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' +
// TLD may end with dot
"\\.?" + ")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:[/?#]\\S*)?" + "$", "i");
return library.regex(value, re_weburl);
}
};
exports.content = _default2.default;
var aliases = exports.aliases = [{ name: 'equals', alias: 'eq' }, { name: 'greaterThan', alias: 'gt' }, { name: 'integer', alias: 'int' }, { name: 'length', alias: 'len' }, { name: 'lessThan', alias: 'lt' }, { name: 'max', alias: 'lte' }, { name: 'maxLength', alias: 'maxlen' }, { name: 'maxLength', alias: 'xlen' }, { name: 'min', alias: 'gte' }, { name: 'minLength', alias: 'minlen' }, { name: 'minLength', alias: 'nlen' }];
var FormStateAdapter = exports.FormStateAdapter = function () {
//
// public
//
function FormStateAdapter(validationLibrary, messageContent, aliases) {
_classCallCheck(this, FormStateAdapter);
this.validationLibrary = validationLibrary;
this.messageContent = messageContent;
this.aliases = aliases;
}
_createClass(FormStateAdapter, [{
key: 'plugInto',
value: function plugInto(FormState) {
if (this.validationLibrary.required) {
FormState.setRequired(this.createFormStateValidationFunction('required'));
}
var names = Object.keys(this.validationLibrary);
for (var i = 0, len = names.length; i < len; i++) {
var name = names[i];
FormState.registerValidation(name, this.createFormStateValidationFunction(name));
}
if (this.aliases && this.aliases.length) {
for (var _i = 0, _len = this.aliases.length; _i < _len; _i++) {
var alias = aliases[_i];
FormState.registerValidation(alias.alias, this.createFormStateValidationFunction(alias.name));
}
}
}
//
// "private"
//
}, {
key: 'createFormStateValidationFunction',
value: function createFormStateValidationFunction(name) {
return function () {
// value, label, additionalArgs
var args = [].slice.call(arguments);
var validate = this.validationLibrary[name];
if (!validate.apply(undefined, [args[0]].concat(_toConsumableArray(args.slice(2))))) {
return this.interpolateMessage(name, args.slice(1));
}
}.bind(this);
}
}, {
key: 'interpolateMessage',
value: function interpolateMessage(name, args) {
var message = this.messageContent && this.messageContent[name];
if (!message) {
return args[0] + ' is invalid';
}
for (var i = 0, len = args.length; i < len; i++) {
message = message.split('%' + (i + 1)).join('' + args[i]);
}
return message;
}
}]);
return FormStateAdapter;
}();
var validationAdapter = exports.validationAdapter = new FormStateAdapter(library, _default2.default, aliases);