UNPKG

8.04 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@radial/helpers')) :
3 typeof define === 'function' && define.amd ? define(['exports', '@radial/helpers'], factory) :
4 (factory((global.RadialValidation = {}),global.helpers));
5}(this, (function (exports,helpers) { 'use strict';
6
7 var setIn = helpers.setInMutable;
8 var ValidationException = /** @class */ (function () {
9 function ValidationException(errors) {
10 this.errors = errors;
11 }
12 return ValidationException;
13 }());
14 function validateValue(value, path, validators, data) {
15 var result = validators.map(function (validator) {
16 var isNil = value === undefined || value === null || value === NaN;
17 if (isNil && validator['allowNil'] !== true)
18 return Promise.resolve();
19 return Promise.resolve(validator(value, path, data, {}));
20 });
21 return Promise.all(result).then(function (errors) {
22 return errors.filter(function (error) {
23 return error !== undefined && error !== null;
24 });
25 }).then(function (err) {
26 return err.length == 0 ? Promise.resolve() : Promise.reject(err);
27 });
28 }
29 function validate(data, rules) {
30 if (data === void 0) { data = {}; }
31 if (rules === void 0) { rules = {}; }
32 if (Array.isArray(rules)) {
33 return validateValue(data, '', rules, data).then(function () {
34 return null;
35 })
36 .catch(function (err) {
37 return Promise.reject(new ValidationException(err));
38 });
39 }
40 var errors = {};
41 var results = Object.keys(rules).map(function (key) {
42 var val = helpers.getIn(data, key);
43 return validateValue(val, key, rules[key], data).catch(function (err) {
44 setIn(errors, key, err);
45 });
46 });
47 return new Promise(function (resolve, reject) {
48 Promise.all(results).then(function () {
49 if (Object.keys(errors).length == 0) {
50 return resolve();
51 }
52 reject(new ValidationException(errors));
53 });
54 });
55 }
56
57 function notBlank() {
58 var func = function (val, key) {
59 if (helpers.isBlank(val)) {
60 return helpers.interpolate(notBlank['message'], { key: key });
61 }
62 };
63 func['allowNil'] = true;
64 return func;
65 }
66 notBlank['message'] = 'This is required';
67 function notNull() {
68 return function (val, key) {
69 if (val === null || val === undefined) {
70 return helpers.interpolate(notNull['message'], { key: key });
71 }
72 };
73 }
74 notNull['message'] = 'This cannot be null';
75 function isTrue(val) {
76 return function (val) {
77 if (val !== true) {
78 return "This is not a true value.";
79 }
80 };
81 }
82 function isFalse() {
83 return function (val) {
84 if (val !== false) {
85 return "is not a false value.";
86 }
87 };
88 }
89
90 function equalTo(opt) {
91 return function (val, key) {
92 if (val != opt) {
93 return 'not equal to ' + opt;
94 }
95 };
96 }
97 function identicalTo(opt) {
98 return function (val, key) {
99 if (val !== opt) {
100 return 'not equal to ' + opt;
101 }
102 };
103 }
104
105 function length(min, max) {
106 return function (val, key) {
107 if (helpers.isBlank(val))
108 return;
109 if (val.length < min) {
110 return 'too short use at least ' + min + ' characters.';
111 }
112 if (val.length > max) {
113 return 'too long ' + max + ' characters allowed';
114 }
115 };
116 }
117 function email() {
118 return function (val, key) {
119 if (helpers.isBlank(val))
120 return;
121 var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
122 if (false === re.test(val)) {
123 return helpers.interpolate(email['message'], { key: key });
124 }
125 };
126 }
127 email['message'] = 'This is not valid email address';
128 function containsIn(values) {
129 return function (val, key) {
130 if (values.indexOf(val) === -1) {
131 return helpers.interpolate(containsIn['message'], { key: key });
132 }
133 };
134 }
135 containsIn['message'] = 'This is not a valid value';
136 function alpha(numeric) {
137 if (numeric === void 0) { numeric = false; }
138 return function (val, key) {
139 var regexp = numeric ? /^[0-9a-zA-Z]+$/ : /^[a-zA-Z]+$/;
140 if (val.match(regexp))
141 return;
142 return helpers.interpolate(alpha['message'], { key: key });
143 };
144 }
145 alpha['message'] = 'contains invalid characters';
146 function alphaSpace(numeric) {
147 if (numeric === void 0) { numeric = false; }
148 return function (val, key) {
149 var regexp = numeric ? /^[0-9a-z\s]+$/i : /^[a-z\s]+$/i;
150 if (val.match(regexp))
151 return;
152 return helpers.interpolate(alphaSpace['message'], { key: key });
153 };
154 }
155 alphaSpace['message'] = 'contains invalid characters';
156
157 function callback(cb) {
158 return function (val, key) {
159 return cb(val, cb);
160 };
161 }
162 function typeOf(string) {
163 return function (val, key) {
164 if (typeof val === string)
165 return null;
166 return 'invalid type';
167 };
168 }
169 function instanceOf(Type) {
170 return function (val, key) {
171 if (val instanceof Type)
172 return null;
173 return 'invalid type';
174 };
175 }
176
177 function isAfter(date, key) {
178 return function (val, _) {
179 if (val instanceof Date && date instanceof Date) {
180 if (val.getTime() > date.getTime())
181 return;
182 return _ + ' must be after ' + key;
183 }
184 };
185 }
186 function isBefore(date, key) {
187 return function (val, _) {
188 if (val instanceof Date && date instanceof Date) {
189 if (val.getTime() < date.getTime())
190 return;
191 return _ + ' must be before ' + key;
192 }
193 };
194 }
195
196 function forEach(rules) {
197 return function (val, key) {
198 if (Array.isArray(val) === false)
199 return;
200 var errors = {};
201 var promises = val.map(function (data, index) {
202 var path = '' + index;
203 var promise = validate(data, rules);
204 return promise.catch(function (err) {
205 helpers.setInMutable(errors, path, err.errors || err);
206 });
207 });
208 return Promise.all(promises).then(function () {
209 if (Object.keys(errors).length === 0)
210 return;
211 return errors;
212 });
213 };
214 }
215
216 exports.ValidationException = ValidationException;
217 exports.validate = validate;
218 exports.notBlank = notBlank;
219 exports.notNull = notNull;
220 exports.isTrue = isTrue;
221 exports.isFalse = isFalse;
222 exports.equalTo = equalTo;
223 exports.identicalTo = identicalTo;
224 exports.length = length;
225 exports.email = email;
226 exports.containsIn = containsIn;
227 exports.alpha = alpha;
228 exports.alphaSpace = alphaSpace;
229 exports.callback = callback;
230 exports.typeOf = typeOf;
231 exports.instanceOf = instanceOf;
232 exports.isAfter = isAfter;
233 exports.isBefore = isBefore;
234 exports.forEach = forEach;
235
236 Object.defineProperty(exports, '__esModule', { value: true });
237
238})));