@webiny/validation 5.35.0-beta.1 → 5.35.0-beta.2
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 +0 -27
- package/index.js.map +1 -1
- package/package.json +4 -4
- package/validation.js +7 -44
- package/validation.js.map +1 -1
- package/validationError.js +0 -5
- package/validationError.js.map +1 -1
- package/validators/creditCard.js +0 -14
- package/validators/creditCard.js.map +1 -1
- package/validators/dateGte.js +2 -11
- package/validators/dateGte.js.map +1 -1
- package/validators/dateLte.js +2 -11
- package/validators/dateLte.js.map +1 -1
- package/validators/email.js +2 -8
- package/validators/email.js.map +1 -1
- package/validators/eq.js +2 -7
- package/validators/eq.js.map +1 -1
- package/validators/gt.js +0 -6
- package/validators/gt.js.map +1 -1
- package/validators/gte.js +0 -6
- package/validators/gte.js.map +1 -1
- package/validators/in.js +0 -7
- package/validators/in.js.map +1 -1
- package/validators/integer.js +0 -7
- package/validators/integer.js.map +1 -1
- package/validators/json.js +0 -5
- package/validators/json.js.map +1 -1
- package/validators/lt.js +0 -7
- package/validators/lt.js.map +1 -1
- package/validators/lte.js +0 -7
- package/validators/lte.js.map +1 -1
- package/validators/maxLength.js +0 -13
- package/validators/maxLength.js.map +1 -1
- package/validators/minLength.js +0 -13
- package/validators/minLength.js.map +1 -1
- package/validators/number.js +0 -8
- package/validators/number.js.map +1 -1
- package/validators/numeric.js +0 -9
- package/validators/numeric.js.map +1 -1
- package/validators/password.js +0 -6
- package/validators/password.js.map +1 -1
- package/validators/phone.js +0 -7
- package/validators/phone.js.map +1 -1
- package/validators/required.js +0 -5
- package/validators/required.js.map +1 -1
- package/validators/slug.js +0 -7
- package/validators/slug.js.map +1 -1
- package/validators/time/index.js +0 -13
- package/validators/time/index.js.map +1 -1
- package/validators/timeGte.js +2 -12
- package/validators/timeGte.js.map +1 -1
- package/validators/timeLte.js +2 -12
- package/validators/timeLte.js.map +1 -1
- package/validators/url.js +8 -16
- package/validators/url.js.map +1 -1
package/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
@@ -18,57 +17,31 @@ Object.defineProperty(exports, "ValidationError", {
|
|
|
18
17
|
}
|
|
19
18
|
});
|
|
20
19
|
exports.validation = void 0;
|
|
21
|
-
|
|
22
20
|
var _validation = _interopRequireDefault(require("./validation"));
|
|
23
|
-
|
|
24
21
|
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
25
|
-
|
|
26
22
|
var _creditCard = _interopRequireDefault(require("./validators/creditCard"));
|
|
27
|
-
|
|
28
23
|
var _email = _interopRequireDefault(require("./validators/email"));
|
|
29
|
-
|
|
30
24
|
var _eq = _interopRequireDefault(require("./validators/eq"));
|
|
31
|
-
|
|
32
25
|
var _gt = _interopRequireDefault(require("./validators/gt"));
|
|
33
|
-
|
|
34
26
|
var _gte = _interopRequireDefault(require("./validators/gte"));
|
|
35
|
-
|
|
36
27
|
var _in = _interopRequireDefault(require("./validators/in"));
|
|
37
|
-
|
|
38
28
|
var _integer = _interopRequireDefault(require("./validators/integer"));
|
|
39
|
-
|
|
40
29
|
var _json = _interopRequireDefault(require("./validators/json"));
|
|
41
|
-
|
|
42
30
|
var _lt = _interopRequireDefault(require("./validators/lt"));
|
|
43
|
-
|
|
44
31
|
var _lte = _interopRequireDefault(require("./validators/lte"));
|
|
45
|
-
|
|
46
32
|
var _maxLength = _interopRequireDefault(require("./validators/maxLength"));
|
|
47
|
-
|
|
48
33
|
var _minLength = _interopRequireDefault(require("./validators/minLength"));
|
|
49
|
-
|
|
50
34
|
var _number = _interopRequireDefault(require("./validators/number"));
|
|
51
|
-
|
|
52
35
|
var _numeric = _interopRequireDefault(require("./validators/numeric"));
|
|
53
|
-
|
|
54
36
|
var _password = _interopRequireDefault(require("./validators/password"));
|
|
55
|
-
|
|
56
37
|
var _phone = _interopRequireDefault(require("./validators/phone"));
|
|
57
|
-
|
|
58
38
|
var _required = _interopRequireDefault(require("./validators/required"));
|
|
59
|
-
|
|
60
39
|
var _url = _interopRequireDefault(require("./validators/url"));
|
|
61
|
-
|
|
62
40
|
var _dateGte = _interopRequireDefault(require("./validators/dateGte"));
|
|
63
|
-
|
|
64
41
|
var _dateLte = _interopRequireDefault(require("./validators/dateLte"));
|
|
65
|
-
|
|
66
42
|
var _timeGte = _interopRequireDefault(require("./validators/timeGte"));
|
|
67
|
-
|
|
68
43
|
var _timeLte = _interopRequireDefault(require("./validators/timeLte"));
|
|
69
|
-
|
|
70
44
|
var _slug = _interopRequireDefault(require("./validators/slug"));
|
|
71
|
-
|
|
72
45
|
const validation = new _validation.default();
|
|
73
46
|
exports.validation = validation;
|
|
74
47
|
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,mBAAU,EAAE;AAAC;AACpCD,UAAU,CAACE,YAAY,CAAC,YAAY,EAAEC,mBAAU,CAAC;AACjDH,UAAU,CAACE,YAAY,CAAC,OAAO,EAAEE,cAAK,CAAC;AACvCJ,UAAU,CAACE,YAAY,CAAC,IAAI,EAAEG,WAAE,CAAC;AACjCL,UAAU,CAACE,YAAY,CAAC,IA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
|
|
3
|
-
"version": "5.35.0-beta.
|
|
3
|
+
"version": "5.35.0-beta.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -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": "^5.35.0-beta.
|
|
28
|
-
"@webiny/project-utils": "^5.35.0-beta.
|
|
27
|
+
"@webiny/cli": "^5.35.0-beta.2",
|
|
28
|
+
"@webiny/project-utils": "^5.35.0-beta.2",
|
|
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": "948cd1e05978e0ed25137ace7dd6c15ed0bf2cca"
|
|
45
45
|
}
|
package/validation.js
CHANGED
|
@@ -1,31 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
|
13
|
-
|
|
14
10
|
var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty"));
|
|
15
|
-
|
|
16
11
|
var _isString2 = _interopRequireDefault(require("lodash/isString"));
|
|
17
|
-
|
|
18
12
|
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
19
|
-
|
|
20
13
|
const entries = validators => {
|
|
21
14
|
return Object.entries(validators);
|
|
22
15
|
};
|
|
23
|
-
|
|
24
16
|
const invalidRules = "Validators must be specified as a string (eg. required,minLength:10,email).";
|
|
25
17
|
const createdValidators = {
|
|
26
18
|
async: {},
|
|
27
19
|
sync: {}
|
|
28
20
|
};
|
|
21
|
+
|
|
29
22
|
/**
|
|
30
23
|
* Main class of Validation library.
|
|
31
24
|
* Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
@@ -37,42 +30,40 @@ const createdValidators = {
|
|
|
37
30
|
* // `validation` is a preconfigured instance of Validation class.
|
|
38
31
|
* // From here you can either add new validators or use it as-is.
|
|
39
32
|
*/
|
|
40
|
-
|
|
41
33
|
class Validation {
|
|
42
34
|
/**
|
|
43
35
|
* Contains a list of all set validators.
|
|
44
36
|
* @private
|
|
45
37
|
*/
|
|
38
|
+
|
|
46
39
|
constructor() {
|
|
47
40
|
(0, _defineProperty2.default)(this, "__validators", void 0);
|
|
48
41
|
this.__validators = {};
|
|
49
42
|
}
|
|
43
|
+
|
|
50
44
|
/**
|
|
51
45
|
* Add new validator.
|
|
52
46
|
* @param name Validator name.
|
|
53
47
|
* @param callable Validator function which throws a ValidationError if validation fails.
|
|
54
48
|
* @returns {Validation}
|
|
55
49
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
50
|
setValidator(name, callable) {
|
|
59
51
|
this.__validators[name] = callable;
|
|
60
52
|
return this;
|
|
61
53
|
}
|
|
54
|
+
|
|
62
55
|
/**
|
|
63
56
|
* Get validator function by name.
|
|
64
57
|
* @param name Validator name.
|
|
65
58
|
* @returns {Validator} A validator function.
|
|
66
59
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
60
|
getValidator(name) {
|
|
70
61
|
if (!this.__validators[name]) {
|
|
71
62
|
throw new _validationError.default("Validator `" + name + "` does not exist!", name);
|
|
72
63
|
}
|
|
73
|
-
|
|
74
64
|
return this.__validators[name];
|
|
75
65
|
}
|
|
66
|
+
|
|
76
67
|
/**
|
|
77
68
|
* Asynchronously validates value.
|
|
78
69
|
* @param value Value to validate.
|
|
@@ -80,37 +71,29 @@ class Validation {
|
|
|
80
71
|
* @param [options] Validation options.
|
|
81
72
|
* @returns {Promise<boolean | ValidationError>}
|
|
82
73
|
*/
|
|
83
|
-
|
|
84
|
-
|
|
85
74
|
async validate(value, validators, options = {}) {
|
|
86
75
|
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
87
76
|
return true;
|
|
88
77
|
}
|
|
89
|
-
|
|
90
78
|
if (!(0, _isString2.default)(validators)) {
|
|
91
79
|
throw new Error(invalidRules);
|
|
92
80
|
}
|
|
93
|
-
|
|
94
81
|
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
95
|
-
|
|
96
82
|
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
97
83
|
const validator = this.getValidator(name);
|
|
98
|
-
|
|
99
84
|
try {
|
|
100
85
|
await validator(value, params);
|
|
101
86
|
} catch (e) {
|
|
102
87
|
const validationError = new _validationError.default(e.message, name, value);
|
|
103
|
-
|
|
104
88
|
if (options.throw === false) {
|
|
105
89
|
return validationError;
|
|
106
90
|
}
|
|
107
|
-
|
|
108
91
|
throw validationError;
|
|
109
92
|
}
|
|
110
93
|
}
|
|
111
|
-
|
|
112
94
|
return true;
|
|
113
95
|
}
|
|
96
|
+
|
|
114
97
|
/**
|
|
115
98
|
* Synchronously validates value.
|
|
116
99
|
* @param value Value to validate.
|
|
@@ -118,82 +101,62 @@ class Validation {
|
|
|
118
101
|
* @param [options] Validation options.
|
|
119
102
|
* @returns {Promise<boolean | ValidationError>}
|
|
120
103
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
104
|
validateSync(value, validators, options = {}) {
|
|
124
105
|
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
125
106
|
return true;
|
|
126
107
|
}
|
|
127
|
-
|
|
128
108
|
if (!(0, _isString2.default)(validators)) {
|
|
129
109
|
throw new Error(invalidRules);
|
|
130
110
|
}
|
|
131
|
-
|
|
132
111
|
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
133
|
-
|
|
134
112
|
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
135
113
|
const validator = this.getValidator(name);
|
|
136
|
-
|
|
137
114
|
try {
|
|
138
115
|
validator(value, params);
|
|
139
116
|
} catch (e) {
|
|
140
117
|
const validationError = new _validationError.default(e.message, name, value);
|
|
141
|
-
|
|
142
118
|
if (options.throw === false) {
|
|
143
119
|
return validationError;
|
|
144
120
|
}
|
|
145
|
-
|
|
146
121
|
throw validationError;
|
|
147
122
|
}
|
|
148
123
|
}
|
|
149
|
-
|
|
150
124
|
return true;
|
|
151
125
|
}
|
|
152
|
-
|
|
153
126
|
create(validators) {
|
|
154
127
|
if (createdValidators.async[validators]) {
|
|
155
128
|
return createdValidators.async[validators];
|
|
156
129
|
}
|
|
157
|
-
|
|
158
130
|
createdValidators.async[validators] = value => this.validate(value, validators);
|
|
159
|
-
|
|
160
131
|
return createdValidators.async[validators];
|
|
161
132
|
}
|
|
162
|
-
|
|
163
133
|
createSync(validators) {
|
|
164
134
|
if (createdValidators.sync[validators]) {
|
|
165
135
|
return createdValidators.sync[validators];
|
|
166
136
|
}
|
|
167
|
-
|
|
168
137
|
createdValidators.sync[validators] = value => this.validateSync(value, validators);
|
|
169
|
-
|
|
170
138
|
return createdValidators.sync[validators];
|
|
171
139
|
}
|
|
140
|
+
|
|
172
141
|
/**
|
|
173
142
|
* Parses a string of validators with parameters.
|
|
174
143
|
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
175
144
|
* @returns {ParsedValidators}
|
|
176
145
|
* @private
|
|
177
146
|
*/
|
|
178
|
-
|
|
179
|
-
|
|
180
147
|
__parseValidateProperty(validators) {
|
|
181
148
|
const validate = validators.split(",");
|
|
182
149
|
const parsedValidators = {};
|
|
183
150
|
validate.forEach(v => {
|
|
184
151
|
const params = (0, _trim2.default)(v).split(":");
|
|
185
152
|
const vName = params.shift();
|
|
186
|
-
|
|
187
153
|
if (!vName) {
|
|
188
154
|
return;
|
|
189
155
|
}
|
|
190
|
-
|
|
191
156
|
parsedValidators[vName] = params;
|
|
192
157
|
});
|
|
193
158
|
return parsedValidators;
|
|
194
159
|
}
|
|
195
|
-
|
|
196
160
|
}
|
|
197
|
-
|
|
198
161
|
var _default = Validation;
|
|
199
162
|
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,UAA4B,IAAqC;EAC9E,OAAOC,MAAM,CAACF,OAAO,CAACC,UAAU,CAAC;AACrC,CAAC;AAED,MAAME,YAAY,GAAG,6EAA6E;AAMlG,MAAMC,iBAAoC,GAAG;EACzCC,KAAK,EAAE,CAAC,CAAC;EACTC,IAAI,EAAE,CAAC;AACX,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,UAAU,CAAC;EACb;AACJ;AACA;AACA;;EAKIC,WAAW,GAAG;IAAA;IACV,IAAI,CAACC,YAAY,GAAG,CAAC,CAAC;EAC1B;;EAEA;AACJ;AACA;AACA;AACA;AACA;EACIC,YAAY,CAACC,IAAY,EAAEC,QAAmB,EAAQ;IAClD,IAAI,CAACH,YAAY,CAACE,IAAI,CAAC,GAAGC,QAAQ;IAClC,OAAO,IAAI;EACf;;EAEA;AACJ;AACA;AACA;AACA;EACIC,YAAY,CAACF,IAAY,EAAa;IAClC,IAAI,CAAC,IAAI,CAACF,YAAY,CAACE,IAAI,CAAC,EAAE;MAC1B,MAAM,IAAIG,wBAAe,CAAC,aAAa,GAAGH,IAAI,GAAG,mBAAmB,EAAEA,IAAI,CAAC;IAC/E;IACA,OAAO,IAAI,CAACF,YAAY,CAACE,IAAI,CAAC;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,KAAK,CAAC,GAAG,CAAC;IAErD,MAAMC,gBAAkC,GAAG,CAAC,CAAC;IAC7ChB,QAAQ,CAACiB,OAAO,CAAEC,CAAS,IAAK;MAC5B,MAAMZ,MAAM,GAAG,oBAAOY,CAAC,CAAC,CAACH,KAAK,CAAC,GAAG,CAAC;MACnC,MAAMI,KAAK,GAAGb,MAAM,CAACc,KAAK,EAAE;MAC5B,IAAI,CAACD,KAAK,EAAE;QACR;MACJ;MACAH,gBAAgB,CAACG,KAAK,CAAC,GAAGb,MAAM;IACpC,CAAC,CAAC;IACF,OAAOU,gBAAgB;EAC3B;AACJ;AAAC,eAEcxB,UAAU;AAAA"}
|
package/validationError.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* This class is used by validators to throw an error when value validation fails.
|
|
14
11
|
*/
|
|
@@ -22,8 +19,6 @@ class ValidationError extends Error {
|
|
|
22
19
|
this.validator = validator;
|
|
23
20
|
this.value = value;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
var _default = ValidationError;
|
|
29
24
|
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,eAAe,SAASC,KAAK,CAAC;EAKhCC,WAAW,CAACC,OAAO,GAAG,EAAE,EAAEC,SAAwB,GAAG,IAAI,EAAEC,KAAoB,GAAG,IAAI,EAAE;IACpF,KAAK,EAAE;IAAC;IAAA;IAAA;IACR,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,SAAS,GAAGA,SAAS;IAC1B,IAAI,CAACC,KAAK,GAAGA,KAAK;EACtB;AACJ;AAAC,eAEcL,eAAe;AAAA"}
|
package/validators/creditCard.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* @name creditCard
|
|
14
11
|
* @description Credit card validator. This validator will check if the given value is a credit card number.
|
|
@@ -26,44 +23,33 @@ var _default = value => {
|
|
|
26
23
|
if (!value) {
|
|
27
24
|
return;
|
|
28
25
|
}
|
|
29
|
-
|
|
30
26
|
value = value + "";
|
|
31
|
-
|
|
32
27
|
if (value.length < 12) {
|
|
33
28
|
throw new _validationError.default("Credit card number too short.");
|
|
34
29
|
}
|
|
35
|
-
|
|
36
30
|
if (/[^0-9-\s]+/.test(value)) {
|
|
37
31
|
throw new _validationError.default("Credit card number invalid.");
|
|
38
32
|
}
|
|
39
|
-
|
|
40
33
|
let nCheck = 0;
|
|
41
34
|
let nDigit = 0;
|
|
42
35
|
let bEven = false;
|
|
43
36
|
value = value.replace(/ /g, "");
|
|
44
37
|
value = value.replace(/\D/g, "");
|
|
45
|
-
|
|
46
38
|
for (let n = value.length - 1; n >= 0; n--) {
|
|
47
39
|
const cDigit = value.charAt(n);
|
|
48
40
|
nDigit = parseInt(cDigit);
|
|
49
|
-
|
|
50
41
|
if (bEven) {
|
|
51
42
|
nDigit *= 2;
|
|
52
|
-
|
|
53
43
|
if (nDigit > 9) {
|
|
54
44
|
nDigit -= 9;
|
|
55
45
|
}
|
|
56
46
|
}
|
|
57
|
-
|
|
58
47
|
nCheck += nDigit;
|
|
59
48
|
bEven = !bEven;
|
|
60
49
|
}
|
|
61
|
-
|
|
62
50
|
if (nCheck % 10 === 0) {
|
|
63
51
|
return;
|
|
64
52
|
}
|
|
65
|
-
|
|
66
53
|
throw new _validationError.default("Credit card number invalid.");
|
|
67
54
|
};
|
|
68
|
-
|
|
69
55
|
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;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|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* Validates that given value is a greater or equal date to a gteValue
|
|
14
11
|
*/
|
|
15
12
|
var _default = (value, params) => {
|
|
16
13
|
if (!value || !params) {
|
|
17
14
|
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
}
|
|
16
|
+
// we need to join because validation params are being split by :
|
|
21
17
|
const gteValue = params.join(":");
|
|
22
|
-
|
|
23
18
|
if (!gteValue) {
|
|
24
19
|
return;
|
|
25
20
|
}
|
|
26
|
-
|
|
27
21
|
const date = new Date(value);
|
|
28
22
|
const gteDate = new Date(gteValue);
|
|
29
|
-
|
|
30
23
|
if (date >= gteDate) {
|
|
31
24
|
return;
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
throw new _validationError.default(`Value needs to be greater than or equal to "${gteDate.toISOString()}".`);
|
|
35
27
|
};
|
|
36
|
-
|
|
37
28
|
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;AAFA,eAGe,CAACA,KAAa,EAAEC,MAAiB,KAAK;EACjD,IAAI,CAACD,KAAK,IAAI,CAACC,MAAM,EAAE;IACnB;EACJ;EACA;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAI,CAAC,GAAG,CAAC;EACjC,IAAI,CAACD,QAAQ,EAAE;IACX;EACJ;EAEA,MAAME,IAAI,GAAG,IAAIC,IAAI,CAACL,KAAK,CAAC;EAC5B,MAAMM,OAAO,GAAG,IAAID,IAAI,CAACH,QAAQ,CAAC;EAElC,IAAIE,IAAI,IAAIE,OAAO,EAAE;IACjB;EACJ;EACA,MAAM,IAAIC,wBAAe,CACpB,+CAA8CD,OAAO,CAACE,WAAW,EAAG,IAAG,CAC3E;AACL,CAAC;AAAA"}
|
package/validators/dateLte.js
CHANGED
|
@@ -1,37 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* Validates that given value is a lesser than or equal date to a lteValue
|
|
14
11
|
*/
|
|
15
12
|
var _default = (value, params) => {
|
|
16
13
|
if (!value || !params) {
|
|
17
14
|
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
}
|
|
16
|
+
// we need to join because validation params are being split by :
|
|
21
17
|
const lteValue = params.join(":");
|
|
22
|
-
|
|
23
18
|
if (!lteValue) {
|
|
24
19
|
return;
|
|
25
20
|
}
|
|
26
|
-
|
|
27
21
|
const date = new Date(value);
|
|
28
22
|
const lteDate = new Date(lteValue);
|
|
29
|
-
|
|
30
23
|
if (date <= lteDate) {
|
|
31
24
|
return;
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
throw new _validationError.default(`Value needs to be lesser than or equal to "${lteDate.toISOString()}".`);
|
|
35
27
|
};
|
|
36
|
-
|
|
37
28
|
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;AAFA,eAGe,CAACA,KAAa,EAAEC,MAAiB,KAAK;EACjD,IAAI,CAACD,KAAK,IAAI,CAACC,MAAM,EAAE;IACnB;EACJ;EACA;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAI,CAAC,GAAG,CAAC;EACjC,IAAI,CAACD,QAAQ,EAAE;IACX;EACJ;EAEA,MAAME,IAAI,GAAG,IAAIC,IAAI,CAACL,KAAK,CAAC;EAC5B,MAAMM,OAAO,GAAG,IAAID,IAAI,CAACH,QAAQ,CAAC;EAElC,IAAIE,IAAI,IAAIE,OAAO,EAAE;IACjB;EACJ;EACA,MAAM,IAAIC,wBAAe,CACpB,8CAA6CD,OAAO,CAACE,WAAW,EAAG,IAAG,CAC1E;AACL,CAAC;AAAA"}
|
package/validators/email.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* @name email
|
|
14
11
|
* @description Email validator. This validator checks if the given value is a valid email address.
|
|
@@ -26,16 +23,13 @@ var _default = value => {
|
|
|
26
23
|
if (!value) {
|
|
27
24
|
return;
|
|
28
25
|
}
|
|
26
|
+
value = value + "";
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
// eslint-disable-next-line
|
|
32
29
|
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
|
-
|
|
34
30
|
if (!value || value.length && re.test(value)) {
|
|
35
31
|
return;
|
|
36
32
|
}
|
|
37
|
-
|
|
38
33
|
throw new _validationError.default("Value must be a valid e-mail address.");
|
|
39
34
|
};
|
|
40
|
-
|
|
41
35
|
exports.default = _default;
|
package/validators/email.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["value","re","length","test","ValidationError"],"sources":["email.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name email\n * @description Email validator. This validator checks if the given value is a valid email address.\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('email@gmail.com', 'email').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 // eslint-disable-next-line\n const re =\n /^(([^<>()[\\]\\\\.,;:\\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,}))$/;\n if (!value || (value.length && re.test(value))) {\n return;\n }\n throw new ValidationError(\"Value must be a valid e-mail address.\");\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["value","re","length","test","ValidationError"],"sources":["email.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name email\n * @description Email validator. This validator checks if the given value is a valid email address.\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('email@gmail.com', 'email').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 // eslint-disable-next-line\n const re =\n /^(([^<>()[\\]\\\\.,;:\\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,}))$/;\n if (!value || (value.length && re.test(value))) {\n return;\n }\n throw new ValidationError(\"Value must be a valid e-mail address.\");\n};\n"],"mappings":";;;;;;;AAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAZA,eAagBA,KAAU,IAAW;EACjC,IAAI,CAACA,KAAK,EAAE;IACR;EACJ;EACAA,KAAK,GAAGA,KAAK,GAAG,EAAE;;EAElB;EACA,MAAMC,EAAE,GACJ,sJAAsJ;EAC1J,IAAI,CAACD,KAAK,IAAKA,KAAK,CAACE,MAAM,IAAID,EAAE,CAACE,IAAI,CAACH,KAAK,CAAE,EAAE;IAC5C;EACJ;EACA,MAAM,IAAII,wBAAe,CAAC,uCAAuC,CAAC;AACtE,CAAC;AAAA"}
|