@webiny/validation 5.15.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/LICENSE +21 -0
- package/README.md +409 -0
- package/index.d.ts +4 -0
- package/index.js +94 -0
- package/index.js.map +1 -0
- package/package.json +43 -0
- package/types.d.ts +27 -0
- package/types.js +6 -0
- package/types.js.map +1 -0
- package/validation.d.ts +62 -0
- package/validation.js +195 -0
- package/validation.js.map +1 -0
- package/validationError.d.ts +10 -0
- package/validationError.js +30 -0
- package/validationError.js.map +1 -0
- package/validators/creditCard.d.ts +15 -0
- package/validators/creditCard.js +70 -0
- package/validators/creditCard.js.map +1 -0
- package/validators/dateGte.d.ts +5 -0
- package/validators/dateGte.js +38 -0
- package/validators/dateGte.js.map +1 -0
- package/validators/dateLte.d.ts +5 -0
- package/validators/dateLte.js +38 -0
- package/validators/dateLte.js.map +1 -0
- package/validators/email.d.ts +15 -0
- package/validators/email.js +42 -0
- package/validators/email.js.map +1 -0
- package/validators/eq.d.ts +16 -0
- package/validators/eq.js +42 -0
- package/validators/eq.js.map +1 -0
- package/validators/gt.d.ts +16 -0
- package/validators/gt.js +39 -0
- package/validators/gt.js.map +1 -0
- package/validators/gte.d.ts +16 -0
- package/validators/gte.js +39 -0
- package/validators/gte.js.map +1 -0
- package/validators/in.d.ts +2 -0
- package/validators/in.js +28 -0
- package/validators/in.js.map +1 -0
- package/validators/integer.d.ts +2 -0
- package/validators/integer.js +27 -0
- package/validators/integer.js.map +1 -0
- package/validators/json.d.ts +2 -0
- package/validators/json.js +25 -0
- package/validators/json.js.map +1 -0
- package/validators/lt.d.ts +2 -0
- package/validators/lt.js +27 -0
- package/validators/lt.js.map +1 -0
- package/validators/lte.d.ts +2 -0
- package/validators/lte.js +27 -0
- package/validators/lte.js.map +1 -0
- package/validators/maxLength.d.ts +2 -0
- package/validators/maxLength.js +45 -0
- package/validators/maxLength.js.map +1 -0
- package/validators/minLength.d.ts +2 -0
- package/validators/minLength.js +45 -0
- package/validators/minLength.js.map +1 -0
- package/validators/number.d.ts +8 -0
- package/validators/number.js +35 -0
- package/validators/number.js.map +1 -0
- package/validators/numeric.d.ts +8 -0
- package/validators/numeric.js +38 -0
- package/validators/numeric.js.map +1 -0
- package/validators/password.d.ts +2 -0
- package/validators/password.js +27 -0
- package/validators/password.js.map +1 -0
- package/validators/phone.d.ts +2 -0
- package/validators/phone.js +27 -0
- package/validators/phone.js.map +1 -0
- package/validators/required.d.ts +2 -0
- package/validators/required.js +23 -0
- package/validators/required.js.map +1 -0
- package/validators/time/index.d.ts +6 -0
- package/validators/time/index.js +80 -0
- package/validators/time/index.js.map +1 -0
- package/validators/timeGte.d.ts +5 -0
- package/validators/timeGte.js +39 -0
- package/validators/timeGte.js.map +1 -0
- package/validators/timeLte.d.ts +5 -0
- package/validators/timeLte.js +39 -0
- package/validators/timeLte.js.map +1 -0
- package/validators/url.d.ts +2 -0
- package/validators/url.js +60 -0
- package/validators/url.js.map +1 -0
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"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"],"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;;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,WAA9B;AACAL,UAAU,CAACE,YAAX,CAAwB,IAAxB,EAA8BI,WAA9B;AACAN,UAAU,CAACE,YAAX,CAAwB,KAAxB,EAA+BK,YAA/B;AACAP,UAAU,CAACE,YAAX,CAAwB,IAAxB,EAA8BM,WAA9B;AACAR,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCO,gBAAnC;AACAT,UAAU,CAACE,YAAX,CAAwB,MAAxB,EAAgCQ,aAAhC;AACAV,UAAU,CAACE,YAAX,CAAwB,IAAxB,EAA8BS,WAA9B;AACAX,UAAU,CAACE,YAAX,CAAwB,KAAxB,EAA+BU,YAA/B;AACAZ,UAAU,CAACE,YAAX,CAAwB,WAAxB,EAAqCW,kBAArC;AACAb,UAAU,CAACE,YAAX,CAAwB,WAAxB,EAAqCY,kBAArC;AACAd,UAAU,CAACE,YAAX,CAAwB,QAAxB,EAAkCa,eAAlC;AACAf,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCc,gBAAnC;AACAhB,UAAU,CAACE,YAAX,CAAwB,UAAxB,EAAoCe,iBAApC;AACAjB,UAAU,CAACE,YAAX,CAAwB,OAAxB,EAAiCgB,cAAjC;AACAlB,UAAU,CAACE,YAAX,CAAwB,UAAxB,EAAoCiB,iBAApC;AACAnB,UAAU,CAACE,YAAX,CAAwB,KAAxB,EAA+BkB,YAA/B;AACApB,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCmB,gBAAnC;AACArB,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCoB,gBAAnC;AACAtB,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCqB,gBAAnC;AACAvB,UAAU,CAACE,YAAX,CAAwB,SAAxB,EAAmCsB,gBAAnC","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\";\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);\n\nexport { validation, Validation, ValidationError };\n"],"file":"index.js"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/validation",
|
|
3
|
+
"version": "5.15.0-beta.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/webiny/webiny-js.git"
|
|
8
|
+
},
|
|
9
|
+
"description": "A simple data validation library, packed with frequently used validators.",
|
|
10
|
+
"contributors": [
|
|
11
|
+
"Pavel Denisjuk <pavel@webiny.com>",
|
|
12
|
+
"Sven Al Hamad <sven@webiny.com>",
|
|
13
|
+
"Adrian Smijulj <adrian@webiny.com>"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@babel/runtime": "7.15.3",
|
|
18
|
+
"isnumeric": "0.3.3",
|
|
19
|
+
"lodash": "4.17.21"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@babel/cli": "^7.5.5",
|
|
23
|
+
"@babel/core": "^7.5.5",
|
|
24
|
+
"@babel/preset-env": "^7.5.5",
|
|
25
|
+
"@babel/preset-typescript": "^7.8.3",
|
|
26
|
+
"@webiny/cli": "^5.15.0-beta.0",
|
|
27
|
+
"@webiny/project-utils": "^5.15.0-beta.0",
|
|
28
|
+
"babel-plugin-lodash": "^3.3.4",
|
|
29
|
+
"jest": "^26.6.3",
|
|
30
|
+
"merge": "^1.2.1",
|
|
31
|
+
"rimraf": "^3.0.2",
|
|
32
|
+
"typescript": "^4.1.3"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"directory": "dist"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "yarn webiny run build",
|
|
40
|
+
"watch": "yarn webiny run watch"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "baf2dbb8cae1b623b59d20fedc0cd0e11a310ca1"
|
|
43
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef Validator
|
|
3
|
+
* @name Validator
|
|
4
|
+
* @description This type defines the validator function.
|
|
5
|
+
* @param {any} value This is the value being validated.
|
|
6
|
+
* @param {Array<string>} parameters (Optional) This represents an array validator parameters.
|
|
7
|
+
* @throws {ValidationError}
|
|
8
|
+
*/
|
|
9
|
+
export declare type Validator = (value: any, params: Array<string>) => void;
|
|
10
|
+
/**
|
|
11
|
+
* @typedef ValidateOptions
|
|
12
|
+
* @name ValidateOptions
|
|
13
|
+
* @description This is an object containing validation options.
|
|
14
|
+
* @property {boolean} throw Should validation throw on failure? Default: true.
|
|
15
|
+
*/
|
|
16
|
+
export declare type ValidateOptions = {
|
|
17
|
+
throw?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @private
|
|
21
|
+
* @typedef ParsedValidators
|
|
22
|
+
* @name ParsedValidators
|
|
23
|
+
* @description An object containing validators with parameters: `{ [string]: Array<string> }`.
|
|
24
|
+
*/
|
|
25
|
+
export declare type ParsedValidators = {
|
|
26
|
+
[key: string]: Array<string>;
|
|
27
|
+
};
|
package/types.js
ADDED
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[],"file":"types.js"}
|
package/validation.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import ValidationError from "./validationError";
|
|
2
|
+
import { Validator, ValidateOptions, ParsedValidators } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Main class of Validation library.
|
|
5
|
+
* Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
6
|
+
*
|
|
7
|
+
* @class Validation
|
|
8
|
+
* @example
|
|
9
|
+
* import { validation } from '@webiny/validation';
|
|
10
|
+
*
|
|
11
|
+
* // `validation` is a preconfigured instance of Validation class.
|
|
12
|
+
* // From here you can either add new validators or use it as-is.
|
|
13
|
+
*/
|
|
14
|
+
declare class Validation {
|
|
15
|
+
/**
|
|
16
|
+
* Contains a list of all set validators.
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
__validators: {
|
|
20
|
+
[key: string]: Validator;
|
|
21
|
+
};
|
|
22
|
+
constructor();
|
|
23
|
+
/**
|
|
24
|
+
* Add new validator.
|
|
25
|
+
* @param name Validator name.
|
|
26
|
+
* @param callable Validator function which throws a ValidationError if validation fails.
|
|
27
|
+
* @returns {Validation}
|
|
28
|
+
*/
|
|
29
|
+
setValidator(name: string, callable: Validator): this;
|
|
30
|
+
/**
|
|
31
|
+
* Get validator function by name.
|
|
32
|
+
* @param name Validator name.
|
|
33
|
+
* @returns {Validator} A validator function.
|
|
34
|
+
*/
|
|
35
|
+
getValidator(name: string): Validator;
|
|
36
|
+
/**
|
|
37
|
+
* Asynchronously validates value.
|
|
38
|
+
* @param value Value to validate.
|
|
39
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
40
|
+
* @param [options] Validation options.
|
|
41
|
+
* @returns {Promise<boolean | ValidationError>}
|
|
42
|
+
*/
|
|
43
|
+
validate(value: any, validators: string, options?: ValidateOptions): Promise<boolean | ValidationError>;
|
|
44
|
+
/**
|
|
45
|
+
* Synchronously validates value.
|
|
46
|
+
* @param value Value to validate.
|
|
47
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
48
|
+
* @param [options] Validation options.
|
|
49
|
+
* @returns {Promise<boolean | ValidationError>}
|
|
50
|
+
*/
|
|
51
|
+
validateSync(value: any, validators: string, options?: ValidateOptions): boolean | ValidationError;
|
|
52
|
+
create(validators: string): any;
|
|
53
|
+
createSync(validators: string): any;
|
|
54
|
+
/**
|
|
55
|
+
* Parses a string of validators with parameters.
|
|
56
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
57
|
+
* @returns {ParsedValidators}
|
|
58
|
+
* @private
|
|
59
|
+
*/
|
|
60
|
+
__parseValidateProperty(validators: string): ParsedValidators;
|
|
61
|
+
}
|
|
62
|
+
export default Validation;
|
package/validation.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
|
13
|
+
|
|
14
|
+
var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty"));
|
|
15
|
+
|
|
16
|
+
var _isString2 = _interopRequireDefault(require("lodash/isString"));
|
|
17
|
+
|
|
18
|
+
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
19
|
+
|
|
20
|
+
const entries = validators => {
|
|
21
|
+
return Object.entries(validators);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const invalidRules = "Validators must be specified as a string (eg. required,minLength:10,email).";
|
|
25
|
+
const createdValidators = {
|
|
26
|
+
async: {},
|
|
27
|
+
sync: {}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Main class of Validation library.
|
|
31
|
+
* Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
32
|
+
*
|
|
33
|
+
* @class Validation
|
|
34
|
+
* @example
|
|
35
|
+
* import { validation } from '@webiny/validation';
|
|
36
|
+
*
|
|
37
|
+
* // `validation` is a preconfigured instance of Validation class.
|
|
38
|
+
* // From here you can either add new validators or use it as-is.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
class Validation {
|
|
42
|
+
/**
|
|
43
|
+
* Contains a list of all set validators.
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
constructor() {
|
|
47
|
+
(0, _defineProperty2.default)(this, "__validators", void 0);
|
|
48
|
+
this.__validators = {};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add new validator.
|
|
52
|
+
* @param name Validator name.
|
|
53
|
+
* @param callable Validator function which throws a ValidationError if validation fails.
|
|
54
|
+
* @returns {Validation}
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
setValidator(name, callable) {
|
|
59
|
+
this.__validators[name] = callable;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get validator function by name.
|
|
64
|
+
* @param name Validator name.
|
|
65
|
+
* @returns {Validator} A validator function.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
getValidator(name) {
|
|
70
|
+
if (!this.__validators[name]) {
|
|
71
|
+
throw new _validationError.default("Validator `" + name + "` does not exist!", name);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return this.__validators[name];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Asynchronously validates value.
|
|
78
|
+
* @param value Value to validate.
|
|
79
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
80
|
+
* @param [options] Validation options.
|
|
81
|
+
* @returns {Promise<boolean | ValidationError>}
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async validate(value, validators, options = {}) {
|
|
86
|
+
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!(0, _isString2.default)(validators)) {
|
|
91
|
+
throw new Error(invalidRules);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
95
|
+
|
|
96
|
+
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
97
|
+
const validator = this.getValidator(name);
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
await validator(value, params);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
const validationError = new _validationError.default(e.message, name, value);
|
|
103
|
+
|
|
104
|
+
if (options.throw === false) {
|
|
105
|
+
return validationError;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
throw validationError;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Synchronously validates value.
|
|
116
|
+
* @param value Value to validate.
|
|
117
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
118
|
+
* @param [options] Validation options.
|
|
119
|
+
* @returns {Promise<boolean | ValidationError>}
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
validateSync(value, validators, options = {}) {
|
|
124
|
+
if ((0, _isString2.default)(validators) && (0, _isEmpty2.default)(validators)) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (!(0, _isString2.default)(validators)) {
|
|
129
|
+
throw new Error(invalidRules);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const parsedValidateProperty = this.__parseValidateProperty(validators);
|
|
133
|
+
|
|
134
|
+
for (const [name, params] of entries(parsedValidateProperty)) {
|
|
135
|
+
const validator = this.getValidator(name);
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
validator(value, params);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
const validationError = new _validationError.default(e.message, name, value);
|
|
141
|
+
|
|
142
|
+
if (options.throw === false) {
|
|
143
|
+
return validationError;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
throw validationError;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
create(validators) {
|
|
154
|
+
if (createdValidators.async[validators]) {
|
|
155
|
+
return createdValidators.async[validators];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
createdValidators.async[validators] = value => this.validate(value, validators);
|
|
159
|
+
|
|
160
|
+
return createdValidators.async[validators];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
createSync(validators) {
|
|
164
|
+
if (createdValidators.sync[validators]) {
|
|
165
|
+
return createdValidators.sync[validators];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
createdValidators.sync[validators] = value => this.validateSync(value, validators);
|
|
169
|
+
|
|
170
|
+
return createdValidators.sync[validators];
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Parses a string of validators with parameters.
|
|
174
|
+
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
|
175
|
+
* @returns {ParsedValidators}
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
__parseValidateProperty(validators) {
|
|
181
|
+
const validate = validators.split(",");
|
|
182
|
+
const parsedValidators = {};
|
|
183
|
+
validate.forEach(v => {
|
|
184
|
+
const params = (0, _trim2.default)(v).split(":");
|
|
185
|
+
const vName = params.shift();
|
|
186
|
+
parsedValidators[vName] = params;
|
|
187
|
+
});
|
|
188
|
+
return parsedValidators;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
var _default = Validation;
|
|
194
|
+
exports.default = _default;
|
|
195
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/validation.ts"],"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"],"mappings":";;;;;;;;;;;;;;;;;AACA;;AAGA,MAAMA,OAAO,GAAIC,UAAD,IAAkE;AAC9E,SAAOC,MAAM,CAACF,OAAP,CAAeC,UAAf,CAAP;AACH,CAFD;;AAIA,MAAME,YAAY,GAAG,6EAArB;AAEA,MAAMC,iBAAiB,GAAG;AACtBC,EAAAA,KAAK,EAAE,EADe;AAEtBC,EAAAA,IAAI,EAAE;AAFgB,CAA1B;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,UAAN,CAAiB;AACb;AACJ;AACA;AACA;AAGIC,EAAAA,WAAW,GAAG;AAAA;AACV,SAAKC,YAAL,GAAoB,EAApB;AACH;AAED;A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C,KAAlB,CAAwBJ,UAAxB,CAAP;AACH;;AAED4B,EAAAA,UAAU,CAAC5B,UAAD,EAAqB;AAC3B,QAAIG,iBAAiB,CAACE,IAAlB,CAAuBL,UAAvB,CAAJ,EAAwC;AACpC,aAAOG,iBAAiB,CAACE,IAAlB,CAAuBL,UAAvB,CAAP;AACH;;AAEDG,IAAAA,iBAAiB,CAACE,IAAlB,CAAuBL,UAAvB,IAAqCe,KAAK,IAAI,KAAKW,YAAL,CAAkBX,KAAlB,EAAyBf,UAAzB,CAA9C;;AACA,WAAOG,iBAAiB,CAACE,IAAlB,CAAuBL,UAAvB,CAAP;AACH;AAED;AACJ;AACA;AACA;AACA;AACA;;;AACImB,EAAAA,uBAAuB,CAACnB,UAAD,EAAuC;AAC1D,UAAMc,QAAuB,GAAGd,UAAU,CAAC6B,KAAX,CAAiB,GAAjB,CAAhC;AAEA,UAAMC,gBAAkC,GAAG,EAA3C;AACAhB,IAAAA,QAAQ,CAACiB,OAAT,CAAkBC,CAAD,IAAe;AAC5B,YAAMZ,MAAM,GAAG,oBAAOY,CAAP,EAAUH,KAAV,CAAgB,GAAhB,CAAf;AACA,YAAMI,KAAK,GAAGb,MAAM,CAACc,KAAP,EAAd;AACAJ,MAAAA,gBAAgB,CAACG,KAAD,CAAhB,GAA0Bb,MAA1B;AACH,KAJD;AAKA,WAAOU,gBAAP;AACH;;AA9IY;;eAiJFxB,U","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\nconst 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: { [key: string]: Validator };\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 parsedValidators[vName] = params;\n });\n return parsedValidators;\n }\n}\n\nexport default Validation;\n"],"file":"validation.js"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This class is used by validators to throw an error when value validation fails.
|
|
3
|
+
*/
|
|
4
|
+
declare class ValidationError extends Error {
|
|
5
|
+
message: string;
|
|
6
|
+
validator: string;
|
|
7
|
+
value: any;
|
|
8
|
+
constructor(message?: string, validator?: string, value?: any);
|
|
9
|
+
}
|
|
10
|
+
export default ValidationError;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This class is used by validators to throw an error when value validation fails.
|
|
14
|
+
*/
|
|
15
|
+
class ValidationError extends Error {
|
|
16
|
+
constructor(message = "", validator = null, value = null) {
|
|
17
|
+
super();
|
|
18
|
+
(0, _defineProperty2.default)(this, "message", void 0);
|
|
19
|
+
(0, _defineProperty2.default)(this, "validator", void 0);
|
|
20
|
+
(0, _defineProperty2.default)(this, "value", void 0);
|
|
21
|
+
this.message = message;
|
|
22
|
+
this.validator = validator;
|
|
23
|
+
this.value = value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var _default = ValidationError;
|
|
29
|
+
exports.default = _default;
|
|
30
|
+
//# sourceMappingURL=validationError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/validationError.ts"],"names":["ValidationError","Error","constructor","message","validator","value"],"mappings":";;;;;;;;;;;AAAA;AACA;AACA;AACA,MAAMA,eAAN,SAA8BC,KAA9B,CAAoC;AAKhCC,EAAAA,WAAW,CAACC,OAAO,GAAG,EAAX,EAAeC,SAAiB,GAAG,IAAnC,EAAyCC,KAAK,GAAG,IAAjD,EAAuD;AAC9D;AAD8D;AAAA;AAAA;AAE9D,SAAKF,OAAL,GAAeA,OAAf;AACA,SAAKC,SAAL,GAAiBA,SAAjB;AACA,SAAKC,KAAL,GAAaA,KAAb;AACH;;AAV+B;;eAarBL,e","sourcesContent":["/**\n * This class is used by validators to throw an error when value validation fails.\n */\nclass ValidationError extends Error {\n message: string;\n validator: string;\n value: any;\n\n constructor(message = \"\", validator: string = null, value = null) {\n super();\n this.message = message;\n this.validator = validator;\n this.value = value;\n }\n}\n\nexport default ValidationError;\n"],"file":"validationError.js"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const _default: (value: any) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @name creditCard
|
|
4
|
+
* @description Credit card validator. This validator will check if the given value is a credit card number.
|
|
5
|
+
* @param {any} value This is the value that will be validated.
|
|
6
|
+
* @throws {ValidationError}
|
|
7
|
+
* @example
|
|
8
|
+
* import { validation } from '@webiny/validation';
|
|
9
|
+
* validation.validate('notACreditCard', 'creditCard').then(() => {
|
|
10
|
+
* // Valid
|
|
11
|
+
* }).catch(e => {
|
|
12
|
+
* // Invalid
|
|
13
|
+
* });
|
|
14
|
+
*/
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _validationError = _interopRequireDefault(require("./../validationError"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @name creditCard
|
|
14
|
+
* @description Credit card validator. This validator will check if the given value is a credit card number.
|
|
15
|
+
* @param {any} value This is the value that will be validated.
|
|
16
|
+
* @throws {ValidationError}
|
|
17
|
+
* @example
|
|
18
|
+
* import { validation } from '@webiny/validation';
|
|
19
|
+
* validation.validate('notACreditCard', 'creditCard').then(() => {
|
|
20
|
+
* // Valid
|
|
21
|
+
* }).catch(e => {
|
|
22
|
+
* // Invalid
|
|
23
|
+
* });
|
|
24
|
+
*/
|
|
25
|
+
var _default = value => {
|
|
26
|
+
if (!value) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
value = value + "";
|
|
31
|
+
|
|
32
|
+
if (value.length < 12) {
|
|
33
|
+
throw new _validationError.default("Credit card number too short.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (/[^0-9-\s]+/.test(value)) {
|
|
37
|
+
throw new _validationError.default("Credit card number invalid.");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let nCheck = 0;
|
|
41
|
+
let nDigit = 0;
|
|
42
|
+
let bEven = false;
|
|
43
|
+
value = value.replace(/ /g, "");
|
|
44
|
+
value = value.replace(/\D/g, "");
|
|
45
|
+
|
|
46
|
+
for (let n = value.length - 1; n >= 0; n--) {
|
|
47
|
+
const cDigit = value.charAt(n);
|
|
48
|
+
nDigit = parseInt(cDigit);
|
|
49
|
+
|
|
50
|
+
if (bEven) {
|
|
51
|
+
nDigit *= 2;
|
|
52
|
+
|
|
53
|
+
if (nDigit > 9) {
|
|
54
|
+
nDigit -= 9;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
nCheck += nDigit;
|
|
59
|
+
bEven = !bEven;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (nCheck % 10 === 0) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new _validationError.default("Credit card number invalid.");
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
exports.default = _default;
|
|
70
|
+
//# sourceMappingURL=creditCard.js.map
|