@webiny/validation 0.0.0-unstable.990c3ab1b6 → 0.0.0-unstable.b02d94bba0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +27 -0
- package/index.js.map +1 -1
- package/package.json +5 -5
- package/validation.js +44 -7
- package/validation.js.map +1 -1
- package/validationError.js +5 -0
- package/validationError.js.map +1 -1
- package/validators/creditCard.js +14 -0
- package/validators/creditCard.js.map +1 -1
- package/validators/dateGte.js +11 -2
- package/validators/dateGte.js.map +1 -1
- package/validators/dateLte.js +11 -2
- package/validators/dateLte.js.map +1 -1
- package/validators/email.js +8 -2
- package/validators/email.js.map +1 -1
- package/validators/eq.js +7 -2
- package/validators/eq.js.map +1 -1
- package/validators/gt.js +6 -0
- package/validators/gt.js.map +1 -1
- package/validators/gte.js +6 -0
- package/validators/gte.js.map +1 -1
- package/validators/in.js +7 -0
- package/validators/in.js.map +1 -1
- package/validators/integer.js +7 -0
- package/validators/integer.js.map +1 -1
- package/validators/json.js +5 -0
- package/validators/json.js.map +1 -1
- package/validators/lt.js +7 -0
- package/validators/lt.js.map +1 -1
- package/validators/lte.js +7 -0
- package/validators/lte.js.map +1 -1
- package/validators/maxLength.js +13 -0
- package/validators/maxLength.js.map +1 -1
- package/validators/minLength.js +13 -0
- package/validators/minLength.js.map +1 -1
- package/validators/number.js +8 -0
- package/validators/number.js.map +1 -1
- package/validators/numeric.js +9 -0
- package/validators/numeric.js.map +1 -1
- package/validators/password.js +6 -0
- package/validators/password.js.map +1 -1
- package/validators/phone.js +7 -0
- package/validators/phone.js.map +1 -1
- package/validators/required.js +5 -0
- package/validators/required.js.map +1 -1
- package/validators/slug.js +7 -0
- package/validators/slug.js.map +1 -1
- package/validators/time/index.js +13 -0
- package/validators/time/index.js.map +1 -1
- package/validators/timeGte.js +12 -2
- package/validators/timeGte.js.map +1 -1
- package/validators/timeLte.js +12 -2
- package/validators/timeLte.js.map +1 -1
- package/validators/url.js +16 -8
- package/validators/url.js.map +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
@@ -17,31 +18,57 @@ Object.defineProperty(exports, "ValidationError", {
|
|
|
17
18
|
}
|
|
18
19
|
});
|
|
19
20
|
exports.validation = void 0;
|
|
21
|
+
|
|
20
22
|
var _validation = _interopRequireDefault(require("./validation"));
|
|
23
|
+
|
|
21
24
|
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
25
|
+
|
|
22
26
|
var _creditCard = _interopRequireDefault(require("./validators/creditCard"));
|
|
27
|
+
|
|
23
28
|
var _email = _interopRequireDefault(require("./validators/email"));
|
|
29
|
+
|
|
24
30
|
var _eq = _interopRequireDefault(require("./validators/eq"));
|
|
31
|
+
|
|
25
32
|
var _gt = _interopRequireDefault(require("./validators/gt"));
|
|
33
|
+
|
|
26
34
|
var _gte = _interopRequireDefault(require("./validators/gte"));
|
|
35
|
+
|
|
27
36
|
var _in = _interopRequireDefault(require("./validators/in"));
|
|
37
|
+
|
|
28
38
|
var _integer = _interopRequireDefault(require("./validators/integer"));
|
|
39
|
+
|
|
29
40
|
var _json = _interopRequireDefault(require("./validators/json"));
|
|
41
|
+
|
|
30
42
|
var _lt = _interopRequireDefault(require("./validators/lt"));
|
|
43
|
+
|
|
31
44
|
var _lte = _interopRequireDefault(require("./validators/lte"));
|
|
45
|
+
|
|
32
46
|
var _maxLength = _interopRequireDefault(require("./validators/maxLength"));
|
|
47
|
+
|
|
33
48
|
var _minLength = _interopRequireDefault(require("./validators/minLength"));
|
|
49
|
+
|
|
34
50
|
var _number = _interopRequireDefault(require("./validators/number"));
|
|
51
|
+
|
|
35
52
|
var _numeric = _interopRequireDefault(require("./validators/numeric"));
|
|
53
|
+
|
|
36
54
|
var _password = _interopRequireDefault(require("./validators/password"));
|
|
55
|
+
|
|
37
56
|
var _phone = _interopRequireDefault(require("./validators/phone"));
|
|
57
|
+
|
|
38
58
|
var _required = _interopRequireDefault(require("./validators/required"));
|
|
59
|
+
|
|
39
60
|
var _url = _interopRequireDefault(require("./validators/url"));
|
|
61
|
+
|
|
40
62
|
var _dateGte = _interopRequireDefault(require("./validators/dateGte"));
|
|
63
|
+
|
|
41
64
|
var _dateLte = _interopRequireDefault(require("./validators/dateLte"));
|
|
65
|
+
|
|
42
66
|
var _timeGte = _interopRequireDefault(require("./validators/timeGte"));
|
|
67
|
+
|
|
43
68
|
var _timeLte = _interopRequireDefault(require("./validators/timeLte"));
|
|
69
|
+
|
|
44
70
|
var _slug = _interopRequireDefault(require("./validators/slug"));
|
|
71
|
+
|
|
45
72
|
const validation = new _validation.default();
|
|
46
73
|
exports.validation = validation;
|
|
47
74
|
validation.setValidator("creditCard", _creditCard.default);
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["validation","Validation","setValidator","creditCard","email","eq","gt","gte","isIn","integer","json","lt","lte","maxLength","minLength","number","numeric","password","phone","required","url","dateGte","dateLte","timeGte","timeLte","slug"],"sources":["index.ts"],"sourcesContent":["import Validation from \"./validation\";\nimport ValidationError from \"./validationError\";\nimport creditCard from \"./validators/creditCard\";\nimport email from \"./validators/email\";\nimport eq from \"./validators/eq\";\nimport gt from \"./validators/gt\";\nimport gte from \"./validators/gte\";\nimport isIn from \"./validators/in\";\nimport integer from \"./validators/integer\";\nimport json from \"./validators/json\";\nimport lt from \"./validators/lt\";\nimport lte from \"./validators/lte\";\nimport maxLength from \"./validators/maxLength\";\nimport minLength from \"./validators/minLength\";\nimport number from \"./validators/number\";\nimport numeric from \"./validators/numeric\";\nimport password from \"./validators/password\";\nimport phone from \"./validators/phone\";\nimport required from \"./validators/required\";\nimport url from \"./validators/url\";\nimport dateGte from \"./validators/dateGte\";\nimport dateLte from \"./validators/dateLte\";\nimport timeGte from \"./validators/timeGte\";\nimport timeLte from \"./validators/timeLte\";\nimport slug from \"./validators/slug\";\n\nconst validation = new Validation();\nvalidation.setValidator(\"creditCard\", creditCard);\nvalidation.setValidator(\"email\", email);\nvalidation.setValidator(\"eq\", eq);\nvalidation.setValidator(\"gt\", gt);\nvalidation.setValidator(\"gte\", gte);\nvalidation.setValidator(\"in\", isIn);\nvalidation.setValidator(\"integer\", integer);\nvalidation.setValidator(\"json\", json);\nvalidation.setValidator(\"lt\", lt);\nvalidation.setValidator(\"lte\", lte);\nvalidation.setValidator(\"maxLength\", maxLength);\nvalidation.setValidator(\"minLength\", minLength);\nvalidation.setValidator(\"number\", number);\nvalidation.setValidator(\"numeric\", numeric);\nvalidation.setValidator(\"password\", password);\nvalidation.setValidator(\"phone\", phone);\nvalidation.setValidator(\"required\", required);\nvalidation.setValidator(\"url\", url);\nvalidation.setValidator(\"dateGte\", dateGte);\nvalidation.setValidator(\"dateLte\", dateLte);\nvalidation.setValidator(\"timeGte\", timeGte);\nvalidation.setValidator(\"timeLte\", timeLte);\nvalidation.setValidator(\"slug\", slug);\n\nexport { validation, Validation, ValidationError };\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["validation","Validation","setValidator","creditCard","email","eq","gt","gte","isIn","integer","json","lt","lte","maxLength","minLength","number","numeric","password","phone","required","url","dateGte","dateLte","timeGte","timeLte","slug"],"sources":["index.ts"],"sourcesContent":["import Validation from \"./validation\";\nimport ValidationError from \"./validationError\";\nimport creditCard from \"./validators/creditCard\";\nimport email from \"./validators/email\";\nimport eq from \"./validators/eq\";\nimport gt from \"./validators/gt\";\nimport gte from \"./validators/gte\";\nimport isIn from \"./validators/in\";\nimport integer from \"./validators/integer\";\nimport json from \"./validators/json\";\nimport lt from \"./validators/lt\";\nimport lte from \"./validators/lte\";\nimport maxLength from \"./validators/maxLength\";\nimport minLength from \"./validators/minLength\";\nimport number from \"./validators/number\";\nimport numeric from \"./validators/numeric\";\nimport password from \"./validators/password\";\nimport phone from \"./validators/phone\";\nimport required from \"./validators/required\";\nimport url from \"./validators/url\";\nimport dateGte from \"./validators/dateGte\";\nimport dateLte from \"./validators/dateLte\";\nimport timeGte from \"./validators/timeGte\";\nimport timeLte from \"./validators/timeLte\";\nimport slug from \"./validators/slug\";\n\nconst validation = new Validation();\nvalidation.setValidator(\"creditCard\", creditCard);\nvalidation.setValidator(\"email\", email);\nvalidation.setValidator(\"eq\", eq);\nvalidation.setValidator(\"gt\", gt);\nvalidation.setValidator(\"gte\", gte);\nvalidation.setValidator(\"in\", isIn);\nvalidation.setValidator(\"integer\", integer);\nvalidation.setValidator(\"json\", json);\nvalidation.setValidator(\"lt\", lt);\nvalidation.setValidator(\"lte\", lte);\nvalidation.setValidator(\"maxLength\", maxLength);\nvalidation.setValidator(\"minLength\", minLength);\nvalidation.setValidator(\"number\", number);\nvalidation.setValidator(\"numeric\", numeric);\nvalidation.setValidator(\"password\", password);\nvalidation.setValidator(\"phone\", phone);\nvalidation.setValidator(\"required\", required);\nvalidation.setValidator(\"url\", url);\nvalidation.setValidator(\"dateGte\", dateGte);\nvalidation.setValidator(\"dateLte\", dateLte);\nvalidation.setValidator(\"timeGte\", timeGte);\nvalidation.setValidator(\"timeLte\", timeLte);\nvalidation.setValidator(\"slug\", slug);\n\nexport { validation, Validation, ValidationError };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAEA,MAAMA,UAAU,GAAG,IAAIC,mBAAJ,EAAnB;;AACAD,UAAU,CAACE,YAAX,CAAwB,YAAxB,EAAsCC,mBAAtC;AACAH,UAAU,CAACE,YAAX,CAAwB,OAAxB,EAAiCE,cAAjC;AACAJ,UAAU,CAACE,YAAX,CAAwB,IAAxB,EAA8BG,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
|
|
3
|
+
"version": "0.0.0-unstable.b02d94bba0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
],
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@babel/runtime": "7.20.
|
|
17
|
+
"@babel/runtime": "7.20.13",
|
|
18
18
|
"isnumeric": "0.3.3",
|
|
19
19
|
"lodash": "4.17.21"
|
|
20
20
|
},
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@babel/preset-env": "^7.19.4",
|
|
25
25
|
"@babel/preset-typescript": "^7.18.6",
|
|
26
26
|
"@types/lodash": "^4.14.190",
|
|
27
|
-
"@webiny/cli": "^0.0.0-unstable.
|
|
28
|
-
"@webiny/project-utils": "^0.0.0-unstable.
|
|
27
|
+
"@webiny/cli": "^0.0.0-unstable.b02d94bba0",
|
|
28
|
+
"@webiny/project-utils": "^0.0.0-unstable.b02d94bba0",
|
|
29
29
|
"babel-plugin-lodash": "^3.3.4",
|
|
30
30
|
"jest": "^28.1.0",
|
|
31
31
|
"merge": "^1.2.1",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"build": "yarn webiny run build",
|
|
42
42
|
"watch": "yarn webiny run watch"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "b02d94bba01eb4d2b17732279216fbc1c7ab5cd5"
|
|
45
45
|
}
|
package/validation.js
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
9
12
|
var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
|
13
|
+
|
|
10
14
|
var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty"));
|
|
15
|
+
|
|
11
16
|
var _isString2 = _interopRequireDefault(require("lodash/isString"));
|
|
17
|
+
|
|
12
18
|
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
19
|
+
|
|
13
20
|
const entries = validators => {
|
|
14
21
|
return Object.entries(validators);
|
|
15
22
|
};
|
|
23
|
+
|
|
16
24
|
const invalidRules = "Validators must be specified as a string (eg. required,minLength:10,email).";
|
|
17
25
|
const createdValidators = {
|
|
18
26
|
async: {},
|
|
19
27
|
sync: {}
|
|
20
28
|
};
|
|
21
|
-
|
|
22
29
|
/**
|
|
23
30
|
* Main class of Validation library.
|
|
24
31
|
* Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
@@ -30,40 +37,42 @@ const createdValidators = {
|
|
|
30
37
|
* // `validation` is a preconfigured instance of Validation class.
|
|
31
38
|
* // From here you can either add new validators or use it as-is.
|
|
32
39
|
*/
|
|
40
|
+
|
|
33
41
|
class Validation {
|
|
34
42
|
/**
|
|
35
43
|
* Contains a list of all set validators.
|
|
36
44
|
* @private
|
|
37
45
|
*/
|
|
38
|
-
|
|
39
46
|
constructor() {
|
|
40
47
|
(0, _defineProperty2.default)(this, "__validators", void 0);
|
|
41
48
|
this.__validators = {};
|
|
42
49
|
}
|
|
43
|
-
|
|
44
50
|
/**
|
|
45
51
|
* Add new validator.
|
|
46
52
|
* @param name Validator name.
|
|
47
53
|
* @param callable Validator function which throws a ValidationError if validation fails.
|
|
48
54
|
* @returns {Validation}
|
|
49
55
|
*/
|
|
56
|
+
|
|
57
|
+
|
|
50
58
|
setValidator(name, callable) {
|
|
51
59
|
this.__validators[name] = callable;
|
|
52
60
|
return this;
|
|
53
61
|
}
|
|
54
|
-
|
|
55
62
|
/**
|
|
56
63
|
* Get validator function by name.
|
|
57
64
|
* @param name Validator name.
|
|
58
65
|
* @returns {Validator} A validator function.
|
|
59
66
|
*/
|
|
67
|
+
|
|
68
|
+
|
|
60
69
|
getValidator(name) {
|
|
61
70
|
if (!this.__validators[name]) {
|
|
62
71
|
throw new _validationError.default("Validator `" + name + "` does not exist!", name);
|
|
63
72
|
}
|
|
73
|
+
|
|
64
74
|
return this.__validators[name];
|
|
65
75
|
}
|
|
66
|
-
|
|
67
76
|
/**
|
|
68
77
|
* Asynchronously validates value.
|
|
69
78
|
* @param value Value to validate.
|
|
@@ -71,29 +80,37 @@ class Validation {
|
|
|
71
80
|
* @param [options] Validation options.
|
|
72
81
|
* @returns {Promise<boolean | ValidationError>}
|
|
73
82
|
*/
|
|
83
|
+
|
|
84
|
+
|
|
74
85
|
async validate(value, validators, options = {}) {
|
|
75
86
|
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
76
87
|
return true;
|
|
77
88
|
}
|
|
89
|
+
|
|
78
90
|
if (!(0, _isString2.default)(validators)) {
|
|
79
91
|
throw new Error(invalidRules);
|
|
80
92
|
}
|
|
93
|
+
|
|
81
94
|
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
95
|
+
|
|
82
96
|
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
83
97
|
const validator = this.getValidator(name);
|
|
98
|
+
|
|
84
99
|
try {
|
|
85
100
|
await validator(value, params);
|
|
86
101
|
} catch (e) {
|
|
87
102
|
const validationError = new _validationError.default(e.message, name, value);
|
|
103
|
+
|
|
88
104
|
if (options.throw === false) {
|
|
89
105
|
return validationError;
|
|
90
106
|
}
|
|
107
|
+
|
|
91
108
|
throw validationError;
|
|
92
109
|
}
|
|
93
110
|
}
|
|
111
|
+
|
|
94
112
|
return true;
|
|
95
113
|
}
|
|
96
|
-
|
|
97
114
|
/**
|
|
98
115
|
* Synchronously validates value.
|
|
99
116
|
* @param value Value to validate.
|
|
@@ -101,62 +118,82 @@ class Validation {
|
|
|
101
118
|
* @param [options] Validation options.
|
|
102
119
|
* @returns {Promise<boolean | ValidationError>}
|
|
103
120
|
*/
|
|
121
|
+
|
|
122
|
+
|
|
104
123
|
validateSync(value, validators, options = {}) {
|
|
105
124
|
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
106
125
|
return true;
|
|
107
126
|
}
|
|
127
|
+
|
|
108
128
|
if (!(0, _isString2.default)(validators)) {
|
|
109
129
|
throw new Error(invalidRules);
|
|
110
130
|
}
|
|
131
|
+
|
|
111
132
|
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
133
|
+
|
|
112
134
|
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
113
135
|
const validator = this.getValidator(name);
|
|
136
|
+
|
|
114
137
|
try {
|
|
115
138
|
validator(value, params);
|
|
116
139
|
} catch (e) {
|
|
117
140
|
const validationError = new _validationError.default(e.message, name, value);
|
|
141
|
+
|
|
118
142
|
if (options.throw === false) {
|
|
119
143
|
return validationError;
|
|
120
144
|
}
|
|
145
|
+
|
|
121
146
|
throw validationError;
|
|
122
147
|
}
|
|
123
148
|
}
|
|
149
|
+
|
|
124
150
|
return true;
|
|
125
151
|
}
|
|
152
|
+
|
|
126
153
|
create(validators) {
|
|
127
154
|
if (createdValidators.async[validators]) {
|
|
128
155
|
return createdValidators.async[validators];
|
|
129
156
|
}
|
|
157
|
+
|
|
130
158
|
createdValidators.async[validators] = value => this.validate(value, validators);
|
|
159
|
+
|
|
131
160
|
return createdValidators.async[validators];
|
|
132
161
|
}
|
|
162
|
+
|
|
133
163
|
createSync(validators) {
|
|
134
164
|
if (createdValidators.sync[validators]) {
|
|
135
165
|
return createdValidators.sync[validators];
|
|
136
166
|
}
|
|
167
|
+
|
|
137
168
|
createdValidators.sync[validators] = value => this.validateSync(value, validators);
|
|
169
|
+
|
|
138
170
|
return createdValidators.sync[validators];
|
|
139
171
|
}
|
|
140
|
-
|
|
141
172
|
/**
|
|
142
173
|
* Parses a string of validators with parameters.
|
|
143
174
|
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
144
175
|
* @returns {ParsedValidators}
|
|
145
176
|
* @private
|
|
146
177
|
*/
|
|
178
|
+
|
|
179
|
+
|
|
147
180
|
__parseValidateProperty(validators) {
|
|
148
181
|
const validate = validators.split(",");
|
|
149
182
|
const parsedValidators = {};
|
|
150
183
|
validate.forEach(v => {
|
|
151
184
|
const params = (0, _trim2.default)(v).split(":");
|
|
152
185
|
const vName = params.shift();
|
|
186
|
+
|
|
153
187
|
if (!vName) {
|
|
154
188
|
return;
|
|
155
189
|
}
|
|
190
|
+
|
|
156
191
|
parsedValidators[vName] = params;
|
|
157
192
|
});
|
|
158
193
|
return parsedValidators;
|
|
159
194
|
}
|
|
195
|
+
|
|
160
196
|
}
|
|
197
|
+
|
|
161
198
|
var _default = Validation;
|
|
162
199
|
exports.default = _default;
|
package/validation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["entries","validators","Object","invalidRules","createdValidators","async","sync","Validation","constructor","__validators","setValidator","name","callable","getValidator","ValidationError","validate","value","options","Error","parsedValidateProperty","__parseValidateProperty","params","validator","e","validationError","message","throw","validateSync","create","createSync","split","parsedValidators","forEach","v","vName","shift"],"sources":["validation.ts"],"sourcesContent":["import _ from \"lodash\";\nimport ValidationError from \"./validationError\";\nimport { Validator, ValidateOptions, ParsedValidators } from \"./types\";\n\nconst entries = (validators: ParsedValidators): Array<[string, Array<string>]> => {\n return Object.entries(validators);\n};\n\nconst invalidRules = \"Validators must be specified as a string (eg. required,minLength:10,email).\";\n\ninterface CreatedValidators {\n async: Record<string, Validator>;\n sync: Record<string, Validator>;\n}\nconst createdValidators: CreatedValidators = {\n async: {},\n sync: {}\n};\n\n/**\n * Main class of Validation library.\n * Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.\n *\n * @class Validation\n * @example\n * import { validation } from '@webiny/validation';\n *\n * // `validation` is a preconfigured instance of Validation class.\n * // From here you can either add new validators or use it as-is.\n */\nclass Validation {\n /**\n * Contains a list of all set validators.\n * @private\n */\n __validators: {\n [key: string]: Validator;\n };\n\n constructor() {\n this.__validators = {};\n }\n\n /**\n * Add new validator.\n * @param name Validator name.\n * @param callable Validator function which throws a ValidationError if validation fails.\n * @returns {Validation}\n */\n setValidator(name: string, callable: Validator): this {\n this.__validators[name] = callable;\n return this;\n }\n\n /**\n * Get validator function by name.\n * @param name Validator name.\n * @returns {Validator} A validator function.\n */\n getValidator(name: string): Validator {\n if (!this.__validators[name]) {\n throw new ValidationError(\"Validator `\" + name + \"` does not exist!\", name);\n }\n return this.__validators[name];\n }\n\n /**\n * Asynchronously validates value.\n * @param value Value to validate.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @param [options] Validation options.\n * @returns {Promise<boolean | ValidationError>}\n */\n async validate(\n value: any,\n validators: string,\n options: ValidateOptions = {}\n ): Promise<boolean | ValidationError> {\n if (_.isString(validators) && _.isEmpty(validators)) {\n return true;\n }\n\n if (!_.isString(validators)) {\n throw new Error(invalidRules);\n }\n\n const parsedValidateProperty = this.__parseValidateProperty(validators);\n\n for (const [name, params] of entries(parsedValidateProperty)) {\n const validator = this.getValidator(name);\n try {\n await validator(value, params);\n } catch (e) {\n const validationError = new ValidationError(e.message, name, value);\n if (options.throw === false) {\n return validationError;\n }\n throw validationError;\n }\n }\n return true;\n }\n\n /**\n * Synchronously validates value.\n * @param value Value to validate.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @param [options] Validation options.\n * @returns {Promise<boolean | ValidationError>}\n */\n validateSync(\n value: any,\n validators: string,\n options: ValidateOptions = {}\n ): boolean | ValidationError {\n if (_.isString(validators) && _.isEmpty(validators)) {\n return true;\n }\n\n if (!_.isString(validators)) {\n throw new Error(invalidRules);\n }\n\n const parsedValidateProperty = this.__parseValidateProperty(validators);\n\n for (const [name, params] of entries(parsedValidateProperty)) {\n const validator = this.getValidator(name);\n try {\n validator(value, params);\n } catch (e) {\n const validationError = new ValidationError(e.message, name, value);\n if (options.throw === false) {\n return validationError;\n }\n throw validationError;\n }\n }\n return true;\n }\n\n create(validators: string) {\n if (createdValidators.async[validators]) {\n return createdValidators.async[validators];\n }\n\n createdValidators.async[validators] = value => this.validate(value, validators);\n return createdValidators.async[validators];\n }\n\n createSync(validators: string) {\n if (createdValidators.sync[validators]) {\n return createdValidators.sync[validators];\n }\n\n createdValidators.sync[validators] = value => this.validateSync(value, validators);\n return createdValidators.sync[validators];\n }\n\n /**\n * Parses a string of validators with parameters.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @returns {ParsedValidators}\n * @private\n */\n __parseValidateProperty(validators: string): ParsedValidators {\n const validate: Array<string> = validators.split(\",\");\n\n const parsedValidators: ParsedValidators = {};\n validate.forEach((v: string) => {\n const params = _.trim(v).split(\":\");\n const vName = params.shift();\n if (!vName) {\n return;\n }\n parsedValidators[vName] = params;\n });\n return parsedValidators;\n }\n}\n\nexport default Validation;\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["entries","validators","Object","invalidRules","createdValidators","async","sync","Validation","constructor","__validators","setValidator","name","callable","getValidator","ValidationError","validate","value","options","Error","parsedValidateProperty","__parseValidateProperty","params","validator","e","validationError","message","throw","validateSync","create","createSync","split","parsedValidators","forEach","v","vName","shift"],"sources":["validation.ts"],"sourcesContent":["import _ from \"lodash\";\nimport ValidationError from \"./validationError\";\nimport { Validator, ValidateOptions, ParsedValidators } from \"./types\";\n\nconst entries = (validators: ParsedValidators): Array<[string, Array<string>]> => {\n return Object.entries(validators);\n};\n\nconst invalidRules = \"Validators must be specified as a string (eg. required,minLength:10,email).\";\n\ninterface CreatedValidators {\n async: Record<string, Validator>;\n sync: Record<string, Validator>;\n}\nconst createdValidators: CreatedValidators = {\n async: {},\n sync: {}\n};\n\n/**\n * Main class of Validation library.\n * Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.\n *\n * @class Validation\n * @example\n * import { validation } from '@webiny/validation';\n *\n * // `validation` is a preconfigured instance of Validation class.\n * // From here you can either add new validators or use it as-is.\n */\nclass Validation {\n /**\n * Contains a list of all set validators.\n * @private\n */\n __validators: {\n [key: string]: Validator;\n };\n\n constructor() {\n this.__validators = {};\n }\n\n /**\n * Add new validator.\n * @param name Validator name.\n * @param callable Validator function which throws a ValidationError if validation fails.\n * @returns {Validation}\n */\n setValidator(name: string, callable: Validator): this {\n this.__validators[name] = callable;\n return this;\n }\n\n /**\n * Get validator function by name.\n * @param name Validator name.\n * @returns {Validator} A validator function.\n */\n getValidator(name: string): Validator {\n if (!this.__validators[name]) {\n throw new ValidationError(\"Validator `\" + name + \"` does not exist!\", name);\n }\n return this.__validators[name];\n }\n\n /**\n * Asynchronously validates value.\n * @param value Value to validate.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @param [options] Validation options.\n * @returns {Promise<boolean | ValidationError>}\n */\n async validate(\n value: any,\n validators: string,\n options: ValidateOptions = {}\n ): Promise<boolean | ValidationError> {\n if (_.isString(validators) && _.isEmpty(validators)) {\n return true;\n }\n\n if (!_.isString(validators)) {\n throw new Error(invalidRules);\n }\n\n const parsedValidateProperty = this.__parseValidateProperty(validators);\n\n for (const [name, params] of entries(parsedValidateProperty)) {\n const validator = this.getValidator(name);\n try {\n await validator(value, params);\n } catch (e) {\n const validationError = new ValidationError(e.message, name, value);\n if (options.throw === false) {\n return validationError;\n }\n throw validationError;\n }\n }\n return true;\n }\n\n /**\n * Synchronously validates value.\n * @param value Value to validate.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @param [options] Validation options.\n * @returns {Promise<boolean | ValidationError>}\n */\n validateSync(\n value: any,\n validators: string,\n options: ValidateOptions = {}\n ): boolean | ValidationError {\n if (_.isString(validators) && _.isEmpty(validators)) {\n return true;\n }\n\n if (!_.isString(validators)) {\n throw new Error(invalidRules);\n }\n\n const parsedValidateProperty = this.__parseValidateProperty(validators);\n\n for (const [name, params] of entries(parsedValidateProperty)) {\n const validator = this.getValidator(name);\n try {\n validator(value, params);\n } catch (e) {\n const validationError = new ValidationError(e.message, name, value);\n if (options.throw === false) {\n return validationError;\n }\n throw validationError;\n }\n }\n return true;\n }\n\n create(validators: string) {\n if (createdValidators.async[validators]) {\n return createdValidators.async[validators];\n }\n\n createdValidators.async[validators] = value => this.validate(value, validators);\n return createdValidators.async[validators];\n }\n\n createSync(validators: string) {\n if (createdValidators.sync[validators]) {\n return createdValidators.sync[validators];\n }\n\n createdValidators.sync[validators] = value => this.validateSync(value, validators);\n return createdValidators.sync[validators];\n }\n\n /**\n * Parses a string of validators with parameters.\n * @param validators A list of comma-separated validators (eg. required,number,gt:20).\n * @returns {ParsedValidators}\n * @private\n */\n __parseValidateProperty(validators: string): ParsedValidators {\n const validate: Array<string> = validators.split(\",\");\n\n const parsedValidators: ParsedValidators = {};\n validate.forEach((v: string) => {\n const params = _.trim(v).split(\":\");\n const vName = params.shift();\n if (!vName) {\n return;\n }\n parsedValidators[vName] = params;\n });\n return parsedValidators;\n }\n}\n\nexport default Validation;\n"],"mappings":";;;;;;;;;;;;;;;;;AACA;;AAGA,MAAMA,OAAO,GAAIC,UAAD,IAAkE;EAC9E,OAAOC,MAAM,CAACF,OAAP,CAAeC,UAAf,CAAP;AACH,CAFD;;AAIA,MAAME,YAAY,GAAG,6EAArB;AAMA,MAAMC,iBAAoC,GAAG;EACzCC,KAAK,EAAE,EADkC;EAEzCC,IAAI,EAAE;AAFmC,CAA7C;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,UAAN,CAAiB;EACb;AACJ;AACA;AACA;EAKIC,WAAW,GAAG;IAAA;IACV,KAAKC,YAAL,GAAoB,EAApB;EACH;EAED;AACJ;AACA;AACA;AACA;AACA;;;EACIC,YAAY,CAACC,IAAD,EAAeC,QAAf,EAA0C;IAClD,KAAKH,YAAL,CAAkBE,IAAlB,IAA0BC,QAA1B;IACA,OAAO,IAAP;EACH;EAED;AACJ;AACA;AACA;AACA;;;EACIC,YAAY,CAACF,IAAD,EAA0B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IAAqCe,KAAK,IAAI,KAAKW,YAAL,CAAkBX,KAAlB,EAAyBf,UAAzB,CAA9C;;IACA,OAAOG,iBAAiB,CAACE,IAAlB,CAAuBL,UAAvB,CAAP;EACH;EAED;AACJ;AACA;AACA;AACA;AACA;;;EACImB,uBAAuB,CAACnB,UAAD,EAAuC;IAC1D,MAAMc,QAAuB,GAAGd,UAAU,CAAC6B,KAAX,CAAiB,GAAjB,CAAhC;IAEA,MAAMC,gBAAkC,GAAG,EAA3C;IACAhB,QAAQ,CAACiB,OAAT,CAAkBC,CAAD,IAAe;MAC5B,MAAMZ,MAAM,GAAG,oBAAOY,CAAP,EAAUH,KAAV,CAAgB,GAAhB,CAAf;MACA,MAAMI,KAAK,GAAGb,MAAM,CAACc,KAAP,EAAd;;MACA,IAAI,CAACD,KAAL,EAAY;QACR;MACH;;MACDH,gBAAgB,CAACG,KAAD,CAAhB,GAA0Bb,MAA1B;IACH,CAPD;IAQA,OAAOU,gBAAP;EACH;;AAnJY;;eAsJFxB,U"}
|
package/validationError.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* This class is used by validators to throw an error when value validation fails.
|
|
11
14
|
*/
|
|
@@ -19,6 +22,8 @@ class ValidationError extends Error {
|
|
|
19
22
|
this.validator = validator;
|
|
20
23
|
this.value = value;
|
|
21
24
|
}
|
|
25
|
+
|
|
22
26
|
}
|
|
27
|
+
|
|
23
28
|
var _default = ValidationError;
|
|
24
29
|
exports.default = _default;
|
package/validationError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["ValidationError","Error","constructor","message","validator","value"],"sources":["validationError.ts"],"sourcesContent":["/**\n * This class is used by validators to throw an error when value validation fails.\n */\nclass ValidationError extends Error {\n public override readonly message: string;\n public readonly validator: string | null;\n public readonly value: any;\n\n constructor(message = \"\", validator: string | null = null, value: string | null = null) {\n super();\n this.message = message;\n this.validator = validator;\n this.value = value;\n }\n}\n\nexport default ValidationError;\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["ValidationError","Error","constructor","message","validator","value"],"sources":["validationError.ts"],"sourcesContent":["/**\n * This class is used by validators to throw an error when value validation fails.\n */\nclass ValidationError extends Error {\n public override readonly message: string;\n public readonly validator: string | null;\n public readonly value: any;\n\n constructor(message = \"\", validator: string | null = null, value: string | null = null) {\n super();\n this.message = message;\n this.validator = validator;\n this.value = value;\n }\n}\n\nexport default ValidationError;\n"],"mappings":";;;;;;;;;;;AAAA;AACA;AACA;AACA,MAAMA,eAAN,SAA8BC,KAA9B,CAAoC;EAKhCC,WAAW,CAACC,OAAO,GAAG,EAAX,EAAeC,SAAwB,GAAG,IAA1C,EAAgDC,KAAoB,GAAG,IAAvE,EAA6E;IACpF;IADoF;IAAA;IAAA;IAEpF,KAAKF,OAAL,GAAeA,OAAf;IACA,KAAKC,SAAL,GAAiBA,SAAjB;IACA,KAAKC,KAAL,GAAaA,KAAb;EACH;;AAV+B;;eAarBL,e"}
|
package/validators/creditCard.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* @name creditCard
|
|
11
14
|
* @description Credit card validator. This validator will check if the given value is a credit card number.
|
|
@@ -23,33 +26,44 @@ var _default = value => {
|
|
|
23
26
|
if (!value) {
|
|
24
27
|
return;
|
|
25
28
|
}
|
|
29
|
+
|
|
26
30
|
value = value + "";
|
|
31
|
+
|
|
27
32
|
if (value.length < 12) {
|
|
28
33
|
throw new _validationError.default("Credit card number too short.");
|
|
29
34
|
}
|
|
35
|
+
|
|
30
36
|
if (/[^0-9-\s]+/.test(value)) {
|
|
31
37
|
throw new _validationError.default("Credit card number invalid.");
|
|
32
38
|
}
|
|
39
|
+
|
|
33
40
|
let nCheck = 0;
|
|
34
41
|
let nDigit = 0;
|
|
35
42
|
let bEven = false;
|
|
36
43
|
value = value.replace(/ /g, "");
|
|
37
44
|
value = value.replace(/\D/g, "");
|
|
45
|
+
|
|
38
46
|
for (let n = value.length - 1; n >= 0; n--) {
|
|
39
47
|
const cDigit = value.charAt(n);
|
|
40
48
|
nDigit = parseInt(cDigit);
|
|
49
|
+
|
|
41
50
|
if (bEven) {
|
|
42
51
|
nDigit *= 2;
|
|
52
|
+
|
|
43
53
|
if (nDigit > 9) {
|
|
44
54
|
nDigit -= 9;
|
|
45
55
|
}
|
|
46
56
|
}
|
|
57
|
+
|
|
47
58
|
nCheck += nDigit;
|
|
48
59
|
bEven = !bEven;
|
|
49
60
|
}
|
|
61
|
+
|
|
50
62
|
if (nCheck % 10 === 0) {
|
|
51
63
|
return;
|
|
52
64
|
}
|
|
65
|
+
|
|
53
66
|
throw new _validationError.default("Credit card number invalid.");
|
|
54
67
|
};
|
|
68
|
+
|
|
55
69
|
exports.default = _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["value","length","ValidationError","test","nCheck","nDigit","bEven","replace","n","cDigit","charAt","parseInt"],"sources":["creditCard.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name creditCard\n * @description Credit card validator. This validator will check if the given value is a credit card number.\n * @param {any} value This is the value that will be validated.\n * @throws {ValidationError}\n * @example\n * import { validation } from '@webiny/validation';\n * validation.validate('notACreditCard', 'creditCard').then(() => {\n * // Valid\n * }).catch(e => {\n * // Invalid\n * });\n */\nexport default (value: any): void => {\n if (!value) {\n return;\n }\n value = value + \"\";\n\n if (value.length < 12) {\n throw new ValidationError(\"Credit card number too short.\");\n }\n\n if (/[^0-9-\\s]+/.test(value)) {\n throw new ValidationError(\"Credit card number invalid.\");\n }\n\n let nCheck = 0;\n let nDigit = 0;\n let bEven = false;\n\n value = value.replace(/ /g, \"\");\n value = value.replace(/\\D/g, \"\");\n\n for (let n = value.length - 1; n >= 0; n--) {\n const cDigit = value.charAt(n);\n nDigit = parseInt(cDigit);\n\n if (bEven) {\n nDigit *= 2;\n if (nDigit > 9) {\n nDigit -= 9;\n }\n }\n\n nCheck += nDigit;\n bEven = !bEven;\n }\n\n if (nCheck % 10 === 0) {\n return;\n }\n\n throw new ValidationError(\"Credit card number invalid.\");\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["value","length","ValidationError","test","nCheck","nDigit","bEven","replace","n","cDigit","charAt","parseInt"],"sources":["creditCard.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name creditCard\n * @description Credit card validator. This validator will check if the given value is a credit card number.\n * @param {any} value This is the value that will be validated.\n * @throws {ValidationError}\n * @example\n * import { validation } from '@webiny/validation';\n * validation.validate('notACreditCard', 'creditCard').then(() => {\n * // Valid\n * }).catch(e => {\n * // Invalid\n * });\n */\nexport default (value: any): void => {\n if (!value) {\n return;\n }\n value = value + \"\";\n\n if (value.length < 12) {\n throw new ValidationError(\"Credit card number too short.\");\n }\n\n if (/[^0-9-\\s]+/.test(value)) {\n throw new ValidationError(\"Credit card number invalid.\");\n }\n\n let nCheck = 0;\n let nDigit = 0;\n let bEven = false;\n\n value = value.replace(/ /g, \"\");\n value = value.replace(/\\D/g, \"\");\n\n for (let n = value.length - 1; n >= 0; n--) {\n const cDigit = value.charAt(n);\n nDigit = parseInt(cDigit);\n\n if (bEven) {\n nDigit *= 2;\n if (nDigit > 9) {\n nDigit -= 9;\n }\n }\n\n nCheck += nDigit;\n bEven = !bEven;\n }\n\n if (nCheck % 10 === 0) {\n return;\n }\n\n throw new ValidationError(\"Credit card number invalid.\");\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eACgBA,KAAD,IAAsB;EACjC,IAAI,CAACA,KAAL,EAAY;IACR;EACH;;EACDA,KAAK,GAAGA,KAAK,GAAG,EAAhB;;EAEA,IAAIA,KAAK,CAACC,MAAN,GAAe,EAAnB,EAAuB;IACnB,MAAM,IAAIC,wBAAJ,CAAoB,+BAApB,CAAN;EACH;;EAED,IAAI,aAAaC,IAAb,CAAkBH,KAAlB,CAAJ,EAA8B;IAC1B,MAAM,IAAIE,wBAAJ,CAAoB,6BAApB,CAAN;EACH;;EAED,IAAIE,MAAM,GAAG,CAAb;EACA,IAAIC,MAAM,GAAG,CAAb;EACA,IAAIC,KAAK,GAAG,KAAZ;EAEAN,KAAK,GAAGA,KAAK,CAACO,OAAN,CAAc,IAAd,EAAoB,EAApB,CAAR;EACAP,KAAK,GAAGA,KAAK,CAACO,OAAN,CAAc,KAAd,EAAqB,EAArB,CAAR;;EAEA,KAAK,IAAIC,CAAC,GAAGR,KAAK,CAACC,MAAN,GAAe,CAA5B,EAA+BO,CAAC,IAAI,CAApC,EAAuCA,CAAC,EAAxC,EAA4C;IACxC,MAAMC,MAAM,GAAGT,KAAK,CAACU,MAAN,CAAaF,CAAb,CAAf;IACAH,MAAM,GAAGM,QAAQ,CAACF,MAAD,CAAjB;;IAEA,IAAIH,KAAJ,EAAW;MACPD,MAAM,IAAI,CAAV;;MACA,IAAIA,MAAM,GAAG,CAAb,EAAgB;QACZA,MAAM,IAAI,CAAV;MACH;IACJ;;IAEDD,MAAM,IAAIC,MAAV;IACAC,KAAK,GAAG,CAACA,KAAT;EACH;;EAED,IAAIF,MAAM,GAAG,EAAT,KAAgB,CAApB,EAAuB;IACnB;EACH;;EAED,MAAM,IAAIF,wBAAJ,CAAoB,6BAApB,CAAN;AACH,C"}
|
package/validators/dateGte.js
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* Validates that given value is a greater or equal date to a gteValue
|
|
11
14
|
*/
|
|
12
15
|
var _default = (value, params) => {
|
|
13
16
|
if (!value || !params) {
|
|
14
17
|
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
18
|
+
} // we need to join because validation params are being split by :
|
|
19
|
+
|
|
20
|
+
|
|
17
21
|
const gteValue = params.join(":");
|
|
22
|
+
|
|
18
23
|
if (!gteValue) {
|
|
19
24
|
return;
|
|
20
25
|
}
|
|
26
|
+
|
|
21
27
|
const date = new Date(value);
|
|
22
28
|
const gteDate = new Date(gteValue);
|
|
29
|
+
|
|
23
30
|
if (date >= gteDate) {
|
|
24
31
|
return;
|
|
25
32
|
}
|
|
33
|
+
|
|
26
34
|
throw new _validationError.default(`Value needs to be greater than or equal to "${gteDate.toISOString()}".`);
|
|
27
35
|
};
|
|
36
|
+
|
|
28
37
|
exports.default = _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["value","params","gteValue","join","date","Date","gteDate","ValidationError","toISOString"],"sources":["dateGte.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * Validates that given value is a greater or equal date to a gteValue\n */\nexport default (value: string, params?: string[]) => {\n if (!value || !params) {\n return;\n }\n // we need to join because validation params are being split by :\n const gteValue = params.join(\":\");\n if (!gteValue) {\n return;\n }\n\n const date = new Date(value);\n const gteDate = new Date(gteValue);\n\n if (date >= gteDate) {\n return;\n }\n throw new ValidationError(\n `Value needs to be greater than or equal to \"${gteDate.toISOString()}\".`\n );\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["value","params","gteValue","join","date","Date","gteDate","ValidationError","toISOString"],"sources":["dateGte.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * Validates that given value is a greater or equal date to a gteValue\n */\nexport default (value: string, params?: string[]) => {\n if (!value || !params) {\n return;\n }\n // we need to join because validation params are being split by :\n const gteValue = params.join(\":\");\n if (!gteValue) {\n return;\n }\n\n const date = new Date(value);\n const gteDate = new Date(gteValue);\n\n if (date >= gteDate) {\n return;\n }\n throw new ValidationError(\n `Value needs to be greater than or equal to \"${gteDate.toISOString()}\".`\n );\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;AACA;AACA;eACe,CAACA,KAAD,EAAgBC,MAAhB,KAAsC;EACjD,IAAI,CAACD,KAAD,IAAU,CAACC,MAAf,EAAuB;IACnB;EACH,CAHgD,CAIjD;;;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAP,CAAY,GAAZ,CAAjB;;EACA,IAAI,CAACD,QAAL,EAAe;IACX;EACH;;EAED,MAAME,IAAI,GAAG,IAAIC,IAAJ,CAASL,KAAT,CAAb;EACA,MAAMM,OAAO,GAAG,IAAID,IAAJ,CAASH,QAAT,CAAhB;;EAEA,IAAIE,IAAI,IAAIE,OAAZ,EAAqB;IACjB;EACH;;EACD,MAAM,IAAIC,wBAAJ,CACD,+CAA8CD,OAAO,CAACE,WAAR,EAAsB,IADnE,CAAN;AAGH,C"}
|
package/validators/dateLte.js
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* Validates that given value is a lesser than or equal date to a lteValue
|
|
11
14
|
*/
|
|
12
15
|
var _default = (value, params) => {
|
|
13
16
|
if (!value || !params) {
|
|
14
17
|
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
18
|
+
} // we need to join because validation params are being split by :
|
|
19
|
+
|
|
20
|
+
|
|
17
21
|
const lteValue = params.join(":");
|
|
22
|
+
|
|
18
23
|
if (!lteValue) {
|
|
19
24
|
return;
|
|
20
25
|
}
|
|
26
|
+
|
|
21
27
|
const date = new Date(value);
|
|
22
28
|
const lteDate = new Date(lteValue);
|
|
29
|
+
|
|
23
30
|
if (date <= lteDate) {
|
|
24
31
|
return;
|
|
25
32
|
}
|
|
33
|
+
|
|
26
34
|
throw new _validationError.default(`Value needs to be lesser than or equal to "${lteDate.toISOString()}".`);
|
|
27
35
|
};
|
|
36
|
+
|
|
28
37
|
exports.default = _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["value","params","lteValue","join","date","Date","lteDate","ValidationError","toISOString"],"sources":["dateLte.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * Validates that given value is a lesser than or equal date to a lteValue\n */\nexport default (value: string, params?: string[]) => {\n if (!value || !params) {\n return;\n }\n // we need to join because validation params are being split by :\n const lteValue = params.join(\":\");\n if (!lteValue) {\n return;\n }\n\n const date = new Date(value);\n const lteDate = new Date(lteValue);\n\n if (date <= lteDate) {\n return;\n }\n throw new ValidationError(\n `Value needs to be lesser than or equal to \"${lteDate.toISOString()}\".`\n );\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["value","params","lteValue","join","date","Date","lteDate","ValidationError","toISOString"],"sources":["dateLte.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * Validates that given value is a lesser than or equal date to a lteValue\n */\nexport default (value: string, params?: string[]) => {\n if (!value || !params) {\n return;\n }\n // we need to join because validation params are being split by :\n const lteValue = params.join(\":\");\n if (!lteValue) {\n return;\n }\n\n const date = new Date(value);\n const lteDate = new Date(lteValue);\n\n if (date <= lteDate) {\n return;\n }\n throw new ValidationError(\n `Value needs to be lesser than or equal to \"${lteDate.toISOString()}\".`\n );\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;AACA;AACA;eACe,CAACA,KAAD,EAAgBC,MAAhB,KAAsC;EACjD,IAAI,CAACD,KAAD,IAAU,CAACC,MAAf,EAAuB;IACnB;EACH,CAHgD,CAIjD;;;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAP,CAAY,GAAZ,CAAjB;;EACA,IAAI,CAACD,QAAL,EAAe;IACX;EACH;;EAED,MAAME,IAAI,GAAG,IAAIC,IAAJ,CAASL,KAAT,CAAb;EACA,MAAMM,OAAO,GAAG,IAAID,IAAJ,CAASH,QAAT,CAAhB;;EAEA,IAAIE,IAAI,IAAIE,OAAZ,EAAqB;IACjB;EACH;;EACD,MAAM,IAAIC,wBAAJ,CACD,8CAA6CD,OAAO,CAACE,WAAR,EAAsB,IADlE,CAAN;AAGH,C"}
|
package/validators/email.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.default = void 0;
|
|
9
|
+
|
|
8
10
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* @name email
|
|
11
14
|
* @description Email validator. This validator checks if the given value is a valid email address.
|
|
@@ -23,13 +26,16 @@ var _default = value => {
|
|
|
23
26
|
if (!value) {
|
|
24
27
|
return;
|
|
25
28
|
}
|
|
26
|
-
value = value + "";
|
|
27
29
|
|
|
28
|
-
// eslint-disable-next-line
|
|
30
|
+
value = value + ""; // eslint-disable-next-line
|
|
31
|
+
|
|
29
32
|
const 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,}))$/;
|
|
33
|
+
|
|
30
34
|
if (!value || value.length && re.test(value)) {
|
|
31
35
|
return;
|
|
32
36
|
}
|
|
37
|
+
|
|
33
38
|
throw new _validationError.default("Value must be a valid e-mail address.");
|
|
34
39
|
};
|
|
40
|
+
|
|
35
41
|
exports.default = _default;
|