@webiny/validation 6.3.0 → 6.4.0-beta.0
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 +30 -30
- package/index.js.map +1 -1
- package/package.json +4 -4
- package/types.js +0 -3
- package/validation.js +62 -136
- package/validation.js.map +1 -1
- package/validationError.js +8 -10
- package/validationError.js.map +1 -1
- package/validators/creditCard.js +23 -46
- package/validators/creditCard.js.map +1 -1
- package/validators/dateGte.js +10 -20
- package/validators/dateGte.js.map +1 -1
- package/validators/dateLte.js +10 -20
- package/validators/dateLte.js.map +1 -1
- package/validators/email.js +8 -25
- package/validators/email.js.map +1 -1
- package/validators/eq.js +7 -28
- package/validators/eq.js.map +1 -1
- package/validators/gt.js +7 -25
- package/validators/gt.js.map +1 -1
- package/validators/gte.js +7 -25
- package/validators/gte.js.map +1 -1
- package/validators/in.js +7 -12
- package/validators/in.js.map +1 -1
- package/validators/integer.js +6 -9
- package/validators/integer.js.map +1 -1
- package/validators/json.js +9 -10
- package/validators/json.js.map +1 -1
- package/validators/lt.js +7 -10
- package/validators/lt.js.map +1 -1
- package/validators/lte.js +7 -10
- package/validators/lte.js.map +1 -1
- package/validators/maxLength.js +10 -18
- package/validators/maxLength.js.map +1 -1
- package/validators/minLength.js +10 -18
- package/validators/minLength.js.map +1 -1
- package/validators/number.js +7 -17
- package/validators/number.js.map +1 -1
- package/validators/numeric.js +8 -23
- package/validators/numeric.js.map +1 -1
- package/validators/password.js +8 -11
- package/validators/password.js.map +1 -1
- package/validators/phone.js +7 -10
- package/validators/phone.js.map +1 -1
- package/validators/required.js +6 -7
- package/validators/required.js.map +1 -1
- package/validators/slug.js +7 -10
- package/validators/slug.js.map +1 -1
- package/validators/time/index.js +28 -55
- package/validators/time/index.js.map +1 -1
- package/validators/timeGte.js +9 -19
- package/validators/timeGte.js.map +1 -1
- package/validators/timeLte.js +9 -19
- package/validators/timeLte.js.map +1 -1
- package/validators/url.js +18 -29
- package/validators/url.js.map +1 -1
- package/types.js.map +0 -1
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import validation from "./validation.js";
|
|
2
|
+
import slug from "./validators/slug.js";
|
|
3
3
|
import creditCard from "./validators/creditCard.js";
|
|
4
4
|
import email from "./validators/email.js";
|
|
5
5
|
import eq from "./validators/eq.js";
|
|
6
6
|
import gt from "./validators/gt.js";
|
|
7
7
|
import gte from "./validators/gte.js";
|
|
8
|
-
import
|
|
8
|
+
import validators_in from "./validators/in.js";
|
|
9
9
|
import integer from "./validators/integer.js";
|
|
10
10
|
import json from "./validators/json.js";
|
|
11
11
|
import lt from "./validators/lt.js";
|
|
@@ -14,7 +14,7 @@ import maxLength from "./validators/maxLength.js";
|
|
|
14
14
|
import minLength from "./validators/minLength.js";
|
|
15
15
|
import number from "./validators/number.js";
|
|
16
16
|
import numeric from "./validators/numeric.js";
|
|
17
|
-
import
|
|
17
|
+
import validators_password from "./validators/password.js";
|
|
18
18
|
import phone from "./validators/phone.js";
|
|
19
19
|
import required from "./validators/required.js";
|
|
20
20
|
import url from "./validators/url.js";
|
|
@@ -22,31 +22,31 @@ import dateGte from "./validators/dateGte.js";
|
|
|
22
22
|
import dateLte from "./validators/dateLte.js";
|
|
23
23
|
import timeGte from "./validators/timeGte.js";
|
|
24
24
|
import timeLte from "./validators/timeLte.js";
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export {
|
|
25
|
+
const src_validation = new validation();
|
|
26
|
+
src_validation.setValidator("creditCard", creditCard);
|
|
27
|
+
src_validation.setValidator("email", email);
|
|
28
|
+
src_validation.setValidator("eq", eq);
|
|
29
|
+
src_validation.setValidator("gt", gt);
|
|
30
|
+
src_validation.setValidator("gte", gte);
|
|
31
|
+
src_validation.setValidator("in", validators_in);
|
|
32
|
+
src_validation.setValidator("integer", integer);
|
|
33
|
+
src_validation.setValidator("json", json);
|
|
34
|
+
src_validation.setValidator("lt", lt);
|
|
35
|
+
src_validation.setValidator("lte", lte);
|
|
36
|
+
src_validation.setValidator("maxLength", maxLength);
|
|
37
|
+
src_validation.setValidator("minLength", minLength);
|
|
38
|
+
src_validation.setValidator("number", number);
|
|
39
|
+
src_validation.setValidator("numeric", numeric);
|
|
40
|
+
src_validation.setValidator("password", validators_password);
|
|
41
|
+
src_validation.setValidator("phone", phone);
|
|
42
|
+
src_validation.setValidator("required", required);
|
|
43
|
+
src_validation.setValidator("url", url);
|
|
44
|
+
src_validation.setValidator("dateGte", dateGte);
|
|
45
|
+
src_validation.setValidator("dateLte", dateLte);
|
|
46
|
+
src_validation.setValidator("timeGte", timeGte);
|
|
47
|
+
src_validation.setValidator("timeLte", timeLte);
|
|
48
|
+
src_validation.setValidator("slug", slug);
|
|
49
|
+
export { default as ValidationError } from "./validationError.js";
|
|
50
|
+
export { src_validation as validation, validation as Validation };
|
|
51
51
|
|
|
52
52
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import Validation from \"./validation.js\";\nimport ValidationError from \"./validationError.js\";\nimport creditCard from \"./validators/creditCard.js\";\nimport email from \"./validators/email.js\";\nimport eq from \"./validators/eq.js\";\nimport gt from \"./validators/gt.js\";\nimport gte from \"./validators/gte.js\";\nimport isIn from \"./validators/in.js\";\nimport integer from \"./validators/integer.js\";\nimport json from \"./validators/json.js\";\nimport lt from \"./validators/lt.js\";\nimport lte from \"./validators/lte.js\";\nimport maxLength from \"./validators/maxLength.js\";\nimport minLength from \"./validators/minLength.js\";\nimport number from \"./validators/number.js\";\nimport numeric from \"./validators/numeric.js\";\nimport password from \"./validators/password.js\";\nimport phone from \"./validators/phone.js\";\nimport required from \"./validators/required.js\";\nimport url from \"./validators/url.js\";\nimport dateGte from \"./validators/dateGte.js\";\nimport dateLte from \"./validators/dateLte.js\";\nimport timeGte from \"./validators/timeGte.js\";\nimport timeLte from \"./validators/timeLte.js\";\nimport slug from \"./validators/slug.js\";\n\nexport const validation = new Validation();\n\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, ValidationError };\n"],"names":["validation","Validation","creditCard","email","eq","gt","gte","isIn","integer","json","lt","lte","maxLength","minLength","number","numeric","password","phone","required","url","dateGte","dateLte","timeGte","timeLte","slug"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BO,MAAMA,iBAAa,IAAIC;AAE9BD,eAAW,YAAY,CAAC,cAAcE;AACtCF,eAAW,YAAY,CAAC,SAASG;AACjCH,eAAW,YAAY,CAAC,MAAMI;AAC9BJ,eAAW,YAAY,CAAC,MAAMK;AAC9BL,eAAW,YAAY,CAAC,OAAOM;AAC/BN,eAAW,YAAY,CAAC,MAAMO;AAC9BP,eAAW,YAAY,CAAC,WAAWQ;AACnCR,eAAW,YAAY,CAAC,QAAQS;AAChCT,eAAW,YAAY,CAAC,MAAMU;AAC9BV,eAAW,YAAY,CAAC,OAAOW;AAC/BX,eAAW,YAAY,CAAC,aAAaY;AACrCZ,eAAW,YAAY,CAAC,aAAaa;AACrCb,eAAW,YAAY,CAAC,UAAUc;AAClCd,eAAW,YAAY,CAAC,WAAWe;AACnCf,eAAW,YAAY,CAAC,YAAYgB;AACpChB,eAAW,YAAY,CAAC,SAASiB;AACjCjB,eAAW,YAAY,CAAC,YAAYkB;AACpClB,eAAW,YAAY,CAAC,OAAOmB;AAC/BnB,eAAW,YAAY,CAAC,WAAWoB;AACnCpB,eAAW,YAAY,CAAC,WAAWqB;AACnCrB,eAAW,YAAY,CAAC,WAAWsB;AACnCtB,eAAW,YAAY,CAAC,WAAWuB;AACnCvB,eAAW,YAAY,CAAC,QAAQwB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/validation",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.0-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -22,14 +22,14 @@
|
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/lodash": "4.17.24",
|
|
25
|
-
"@webiny/build-tools": "6.
|
|
25
|
+
"@webiny/build-tools": "6.4.0-beta.0",
|
|
26
26
|
"rimraf": "6.1.3",
|
|
27
27
|
"typescript": "6.0.3",
|
|
28
|
-
"vitest": "4.1.
|
|
28
|
+
"vitest": "4.1.6"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public",
|
|
32
32
|
"directory": "dist"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a545d7529828af07d08d49c3da1bcb967483b9ce"
|
|
35
35
|
}
|
package/types.js
CHANGED
package/validation.js
CHANGED
|
@@ -1,155 +1,81 @@
|
|
|
1
1
|
import isString from "lodash/isString.js";
|
|
2
2
|
import isEmpty from "lodash/isEmpty.js";
|
|
3
3
|
import trim from "lodash/trim.js";
|
|
4
|
-
import
|
|
5
|
-
const entries = validators
|
|
6
|
-
return Object.entries(validators);
|
|
7
|
-
};
|
|
4
|
+
import validationError_0 from "./validationError.js";
|
|
5
|
+
const entries = (validators)=>Object.entries(validators);
|
|
8
6
|
const invalidRules = "Validators must be specified as a string (eg. required,minLength:10,email).";
|
|
9
7
|
const createdValidators = {
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
async: {},
|
|
9
|
+
sync: {}
|
|
12
10
|
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Main class of Validation library.
|
|
16
|
-
* Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
17
|
-
*
|
|
18
|
-
* @class Validation
|
|
19
|
-
* @example
|
|
20
|
-
* import { validation } from '@webiny/validation';
|
|
21
|
-
*
|
|
22
|
-
* // `validation` is a preconfigured instance of Validation class.
|
|
23
|
-
* // From here you can either add new validators or use it as-is.
|
|
24
|
-
*/
|
|
25
11
|
class Validation {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* @private
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
constructor() {
|
|
32
|
-
this.__validators = {};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Add new validator.
|
|
37
|
-
* @param name Validator name.
|
|
38
|
-
* @param callable Validator function which throws a ValidationError if validation fails.
|
|
39
|
-
* @returns {Validation}
|
|
40
|
-
*/
|
|
41
|
-
setValidator(name, callable) {
|
|
42
|
-
this.__validators[name] = callable;
|
|
43
|
-
this.__validators[name].validatorName = name;
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get validator function by name.
|
|
49
|
-
* @param name Validator name.
|
|
50
|
-
* @returns {Validator} A validator function.
|
|
51
|
-
*/
|
|
52
|
-
getValidator(name) {
|
|
53
|
-
if (!this.__validators[name]) {
|
|
54
|
-
throw new ValidationError("Validator `" + name + "` does not exist!", name);
|
|
12
|
+
constructor(){
|
|
13
|
+
this.__validators = {};
|
|
55
14
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* Asynchronously validates value.
|
|
61
|
-
* @param value Value to validate.
|
|
62
|
-
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
63
|
-
* @param [options] Validation options.
|
|
64
|
-
* @returns {Promise<boolean | ValidationError>}
|
|
65
|
-
*/
|
|
66
|
-
async validate(value, validators, options = {}) {
|
|
67
|
-
if (isString(validators) && isEmpty(validators)) {
|
|
68
|
-
return true;
|
|
15
|
+
setValidator(name, callable) {
|
|
16
|
+
this.__validators[name] = callable;
|
|
17
|
+
this.__validators[name].validatorName = name;
|
|
18
|
+
return this;
|
|
69
19
|
}
|
|
70
|
-
|
|
71
|
-
|
|
20
|
+
getValidator(name) {
|
|
21
|
+
if (!this.__validators[name]) throw new validationError_0("Validator `" + name + "` does not exist!", name);
|
|
22
|
+
return this.__validators[name];
|
|
72
23
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
24
|
+
async validate(value, validators, options = {}) {
|
|
25
|
+
if (isString(validators) && isEmpty(validators)) return true;
|
|
26
|
+
if (!isString(validators)) throw new Error(invalidRules);
|
|
27
|
+
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
28
|
+
for (const [name, params] of entries(parsedValidateProperty)){
|
|
29
|
+
const validator = this.getValidator(name);
|
|
30
|
+
try {
|
|
31
|
+
await validator(value, params);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
const validationError = new validationError_0(e.message, name, value);
|
|
34
|
+
if (false === options.throw) return validationError;
|
|
35
|
+
throw validationError;
|
|
36
|
+
}
|
|
82
37
|
}
|
|
83
|
-
|
|
84
|
-
}
|
|
38
|
+
return true;
|
|
85
39
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
if (!isString(validators)) {
|
|
101
|
-
throw new Error(invalidRules);
|
|
102
|
-
}
|
|
103
|
-
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
104
|
-
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
105
|
-
const validator = this.getValidator(name);
|
|
106
|
-
try {
|
|
107
|
-
validator(value, params);
|
|
108
|
-
} catch (e) {
|
|
109
|
-
const validationError = new ValidationError(e.message, name, value);
|
|
110
|
-
if (options.throw === false) {
|
|
111
|
-
return validationError;
|
|
40
|
+
validateSync(value, validators, options = {}) {
|
|
41
|
+
if (isString(validators) && isEmpty(validators)) return true;
|
|
42
|
+
if (!isString(validators)) throw new Error(invalidRules);
|
|
43
|
+
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
44
|
+
for (const [name, params] of entries(parsedValidateProperty)){
|
|
45
|
+
const validator = this.getValidator(name);
|
|
46
|
+
try {
|
|
47
|
+
validator(value, params);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
const validationError = new validationError_0(e.message, name, value);
|
|
50
|
+
if (false === options.throw) return validationError;
|
|
51
|
+
throw validationError;
|
|
52
|
+
}
|
|
112
53
|
}
|
|
113
|
-
|
|
114
|
-
}
|
|
54
|
+
return true;
|
|
115
55
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return createdValidators.async[validators];
|
|
56
|
+
create(validators) {
|
|
57
|
+
if (createdValidators.async[validators]) return createdValidators.async[validators];
|
|
58
|
+
createdValidators.async[validators] = (value)=>this.validate(value, validators);
|
|
59
|
+
return createdValidators.async[validators];
|
|
121
60
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
61
|
+
createSync(validators) {
|
|
62
|
+
if (createdValidators.sync[validators]) return createdValidators.sync[validators];
|
|
63
|
+
createdValidators.sync[validators] = (value)=>this.validateSync(value, validators);
|
|
64
|
+
return createdValidators.sync[validators];
|
|
65
|
+
}
|
|
66
|
+
__parseValidateProperty(validators) {
|
|
67
|
+
const validate = validators.split(",");
|
|
68
|
+
const parsedValidators = {};
|
|
69
|
+
validate.forEach((v)=>{
|
|
70
|
+
const params = trim(v).split(":");
|
|
71
|
+
const vName = params.shift();
|
|
72
|
+
if (!vName) return;
|
|
73
|
+
parsedValidators[vName] = params;
|
|
74
|
+
});
|
|
75
|
+
return parsedValidators;
|
|
128
76
|
}
|
|
129
|
-
createdValidators.sync[validators] = value => this.validateSync(value, validators);
|
|
130
|
-
return createdValidators.sync[validators];
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Parses a string of validators with parameters.
|
|
135
|
-
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
136
|
-
* @returns {ParsedValidators}
|
|
137
|
-
* @private
|
|
138
|
-
*/
|
|
139
|
-
__parseValidateProperty(validators) {
|
|
140
|
-
const validate = validators.split(",");
|
|
141
|
-
const parsedValidators = {};
|
|
142
|
-
validate.forEach(v => {
|
|
143
|
-
const params = trim(v).split(":");
|
|
144
|
-
const vName = params.shift();
|
|
145
|
-
if (!vName) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
parsedValidators[vName] = params;
|
|
149
|
-
});
|
|
150
|
-
return parsedValidators;
|
|
151
|
-
}
|
|
152
77
|
}
|
|
153
|
-
|
|
78
|
+
const validation = Validation;
|
|
79
|
+
export default validation;
|
|
154
80
|
|
|
155
81
|
//# sourceMappingURL=validation.js.map
|
package/validation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"validation.js","sources":["../src/validation.ts"],"sourcesContent":["import isString from \"lodash/isString.js\";\nimport isEmpty from \"lodash/isEmpty.js\";\nimport trim from \"lodash/trim.js\";\nimport ValidationError from \"./validationError.js\";\nimport type { ParsedValidators, ValidateOptions, Validator } from \"./types.js\";\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 this.__validators[name].validatorName = name;\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"],"names":["entries","validators","Object","invalidRules","createdValidators","Validation","name","callable","ValidationError","value","options","isString","isEmpty","Error","parsedValidateProperty","params","validator","e","validationError","validate","parsedValidators","v","trim","vName"],"mappings":"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"}
|
package/validationError.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This class is used by validators to throw an error when value validation fails.
|
|
3
|
-
*/
|
|
4
1
|
class ValidationError extends Error {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
constructor(message = "", validator = null, value = null){
|
|
3
|
+
super();
|
|
4
|
+
this.message = message;
|
|
5
|
+
this.validator = validator;
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
11
8
|
}
|
|
12
|
-
|
|
9
|
+
const validationError = ValidationError;
|
|
10
|
+
export default validationError;
|
|
13
11
|
|
|
14
12
|
//# sourceMappingURL=validationError.js.map
|
package/validationError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"validationError.js","sources":["../src/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"],"names":["ValidationError","Error","message","validator","value"],"mappings":"AAGA,MAAMA,wBAAwBC;IAK1B,YAAYC,UAAU,EAAE,EAAEC,YAA2B,IAAI,EAAEC,QAAuB,IAAI,CAAE;QACpF,KAAK;QACL,IAAI,CAAC,OAAO,GAAGF;QACf,IAAI,CAAC,SAAS,GAAGC;QACjB,IAAI,CAAC,KAAK,GAAGC;IACjB;AACJ;AAEA,wBAAeJ"}
|
package/validators/creditCard.js
CHANGED
|
@@ -1,50 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (value.length < 12) {
|
|
22
|
-
throw new ValidationError("Credit card number too short.");
|
|
23
|
-
}
|
|
24
|
-
if (/[^0-9-\s]+/.test(value)) {
|
|
25
|
-
throw new ValidationError("Credit card number invalid.");
|
|
26
|
-
}
|
|
27
|
-
let nCheck = 0;
|
|
28
|
-
let nDigit = 0;
|
|
29
|
-
let bEven = false;
|
|
30
|
-
value = value.replace(/ /g, "");
|
|
31
|
-
value = value.replace(/\D/g, "");
|
|
32
|
-
for (let n = value.length - 1; n >= 0; n--) {
|
|
33
|
-
const cDigit = value.charAt(n);
|
|
34
|
-
nDigit = parseInt(cDigit);
|
|
35
|
-
if (bEven) {
|
|
36
|
-
nDigit *= 2;
|
|
37
|
-
if (nDigit > 9) {
|
|
38
|
-
nDigit -= 9;
|
|
39
|
-
}
|
|
1
|
+
import validationError from "../validationError.js";
|
|
2
|
+
const creditCard = (value)=>{
|
|
3
|
+
if (!value) return;
|
|
4
|
+
value += "";
|
|
5
|
+
if (value.length < 12) throw new validationError("Credit card number too short.");
|
|
6
|
+
if (/[^0-9-\s]+/.test(value)) throw new validationError("Credit card number invalid.");
|
|
7
|
+
let nCheck = 0;
|
|
8
|
+
let nDigit = 0;
|
|
9
|
+
let bEven = false;
|
|
10
|
+
value = value.replace(/ /g, "");
|
|
11
|
+
value = value.replace(/\D/g, "");
|
|
12
|
+
for(let n = value.length - 1; n >= 0; n--){
|
|
13
|
+
const cDigit = value.charAt(n);
|
|
14
|
+
nDigit = parseInt(cDigit);
|
|
15
|
+
if (bEven) {
|
|
16
|
+
nDigit *= 2;
|
|
17
|
+
if (nDigit > 9) nDigit -= 9;
|
|
18
|
+
}
|
|
19
|
+
nCheck += nDigit;
|
|
20
|
+
bEven = !bEven;
|
|
40
21
|
}
|
|
41
|
-
nCheck
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
if (nCheck % 10 === 0) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
throw new ValidationError("Credit card number invalid.");
|
|
22
|
+
if (nCheck % 10 === 0) return;
|
|
23
|
+
throw new validationError("Credit card number invalid.");
|
|
48
24
|
};
|
|
25
|
+
export default creditCard;
|
|
49
26
|
|
|
50
27
|
//# sourceMappingURL=creditCard.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"validators/creditCard.js","sources":["../../src/validators/creditCard.ts"],"sourcesContent":["import ValidationError from \"~/validationError.js\";\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"],"names":["value","ValidationError","nCheck","nDigit","bEven","n","cDigit","parseInt"],"mappings":";AAeA,mBAAgB,CAAAA;IACZ,IAAI,CAACA,OACD;IAEJA,SAAgB;IAEhB,IAAIA,MAAM,MAAM,GAAG,IACf,MAAM,IAAIC,gBAAgB;IAG9B,IAAI,aAAa,IAAI,CAACD,QAClB,MAAM,IAAIC,gBAAgB;IAG9B,IAAIC,SAAS;IACb,IAAIC,SAAS;IACb,IAAIC,QAAQ;IAEZJ,QAAQA,MAAM,OAAO,CAAC,MAAM;IAC5BA,QAAQA,MAAM,OAAO,CAAC,OAAO;IAE7B,IAAK,IAAIK,IAAIL,MAAM,MAAM,GAAG,GAAGK,KAAK,GAAGA,IAAK;QACxC,MAAMC,SAASN,MAAM,MAAM,CAACK;QAC5BF,SAASI,SAASD;QAElB,IAAIF,OAAO;YACPD,UAAU;YACV,IAAIA,SAAS,GACTA,UAAU;QAElB;QAEAD,UAAUC;QACVC,QAAQ,CAACA;IACb;IAEA,IAAIF,SAAS,OAAO,GAChB;IAGJ,MAAM,IAAID,gBAAgB;AAC9B"}
|
package/validators/dateGte.js
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return;
|
|
9
|
-
|
|
10
|
-
// we need to join because validation params are being split by :
|
|
11
|
-
const gteValue = params.join(":");
|
|
12
|
-
if (!gteValue) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
const date = new Date(value);
|
|
16
|
-
const gteDate = new Date(gteValue);
|
|
17
|
-
if (date >= gteDate) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
throw new ValidationError(`Value needs to be greater than or equal to "${gteDate.toISOString()}".`);
|
|
1
|
+
import validationError from "../validationError.js";
|
|
2
|
+
const dateGte = (value, params)=>{
|
|
3
|
+
if (!value || !params) return;
|
|
4
|
+
const gteValue = params.join(":");
|
|
5
|
+
if (!gteValue) return;
|
|
6
|
+
const date = new Date(value);
|
|
7
|
+
const gteDate = new Date(gteValue);
|
|
8
|
+
if (date >= gteDate) return;
|
|
9
|
+
throw new validationError(`Value needs to be greater than or equal to "${gteDate.toISOString()}".`);
|
|
21
10
|
};
|
|
11
|
+
export default dateGte;
|
|
22
12
|
|
|
23
13
|
//# sourceMappingURL=dateGte.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"validators/dateGte.js","sources":["../../src/validators/dateGte.ts"],"sourcesContent":["import ValidationError from \"~/validationError.js\";\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"],"names":["value","params","gteValue","date","Date","gteDate","ValidationError"],"mappings":";AAKA,gBAAgB,CAAAA,OAAeC;IAC3B,IAAI,CAACD,SAAS,CAACC,QACX;IAGJ,MAAMC,WAAWD,OAAO,IAAI,CAAC;IAC7B,IAAI,CAACC,UACD;IAGJ,MAAMC,OAAO,IAAIC,KAAKJ;IACtB,MAAMK,UAAU,IAAID,KAAKF;IAEzB,IAAIC,QAAQE,SACR;IAEJ,MAAM,IAAIC,gBACN,CAAC,4CAA4C,EAAED,QAAQ,WAAW,GAAG,EAAE,CAAC;AAEhF"}
|