@webiny/validation 0.0.0-mt-3 → 0.0.0-unstable.2af142b57e
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 +4 -28
- package/index.js.map +1 -0
- package/package.json +13 -11
- package/types.d.ts +8 -5
- package/types.js.map +1 -0
- package/validation.d.ts +2 -2
- package/validation.js +11 -43
- package/validation.js.map +1 -0
- package/validationError.d.ts +4 -4
- package/validationError.js +1 -6
- package/validationError.js.map +1 -0
- package/validators/creditCard.d.ts +1 -1
- package/validators/creditCard.js +2 -16
- package/validators/creditCard.js.map +1 -0
- package/validators/dateGte.d.ts +1 -1
- package/validators/dateGte.js +5 -14
- package/validators/dateGte.js.map +1 -0
- package/validators/dateLte.d.ts +1 -1
- package/validators/dateLte.js +5 -14
- package/validators/dateLte.js.map +1 -0
- package/validators/email.d.ts +1 -1
- package/validators/email.js +4 -10
- package/validators/email.js.map +1 -0
- package/validators/eq.d.ts +1 -1
- package/validators/eq.js +5 -10
- package/validators/eq.js.map +1 -0
- package/validators/gt.d.ts +1 -1
- package/validators/gt.js +3 -9
- package/validators/gt.js.map +1 -0
- package/validators/gte.d.ts +1 -1
- package/validators/gte.js +3 -9
- package/validators/gte.js.map +1 -0
- package/validators/in.d.ts +1 -1
- package/validators/in.js +3 -10
- package/validators/in.js.map +1 -0
- package/validators/integer.js +2 -9
- package/validators/integer.js.map +1 -0
- package/validators/json.js +2 -7
- package/validators/json.js.map +1 -0
- package/validators/lt.d.ts +1 -1
- package/validators/lt.js +3 -10
- package/validators/lt.js.map +1 -0
- package/validators/lte.d.ts +1 -1
- package/validators/lte.js +3 -10
- package/validators/lte.js.map +1 -0
- package/validators/maxLength.d.ts +1 -1
- package/validators/maxLength.js +3 -16
- package/validators/maxLength.js.map +1 -0
- package/validators/minLength.d.ts +1 -1
- package/validators/minLength.js +3 -16
- package/validators/minLength.js.map +1 -0
- package/validators/number.d.ts +1 -1
- package/validators/number.js +2 -10
- package/validators/number.js.map +1 -0
- package/validators/numeric.d.ts +1 -1
- package/validators/numeric.js +6 -10
- package/validators/numeric.js.map +1 -0
- package/validators/password.d.ts +1 -1
- package/validators/password.js +4 -10
- package/validators/password.js.map +1 -0
- package/validators/phone.js +2 -9
- package/validators/phone.js.map +1 -0
- package/validators/required.js +2 -7
- package/validators/required.js.map +1 -0
- package/validators/slug.d.ts +2 -0
- package/validators/slug.js +19 -0
- package/validators/slug.js.map +1 -0
- package/validators/time/index.js +1 -14
- package/validators/time/index.js.map +1 -0
- package/validators/timeGte.d.ts +1 -1
- package/validators/timeGte.js +5 -15
- package/validators/timeGte.js.map +1 -0
- package/validators/timeLte.d.ts +1 -1
- package/validators/timeLte.js +5 -15
- package/validators/timeLte.js.map +1 -0
- package/validators/url.d.ts +1 -1
- package/validators/url.js +12 -20
- package/validators/url.js.map +1 -0
package/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
@@ -18,55 +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
|
-
|
|
44
|
+
var _slug = _interopRequireDefault(require("./validators/slug"));
|
|
70
45
|
const validation = new _validation.default();
|
|
71
46
|
exports.validation = validation;
|
|
72
47
|
validation.setValidator("creditCard", _creditCard.default);
|
|
@@ -90,4 +65,5 @@ validation.setValidator("url", _url.default);
|
|
|
90
65
|
validation.setValidator("dateGte", _dateGte.default);
|
|
91
66
|
validation.setValidator("dateLte", _dateLte.default);
|
|
92
67
|
validation.setValidator("timeGte", _timeGte.default);
|
|
93
|
-
validation.setValidator("timeLte", _timeLte.default);
|
|
68
|
+
validation.setValidator("timeLte", _timeLte.default);
|
|
69
|
+
validation.setValidator("slug", _slug.default);
|
package/index.js.map
ADDED
|
@@ -0,0 +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":";;;;;;;;;;;;;;;;;;;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,IAAI,EAAEI,WAAE,CAAC;AACjCN,UAAU,CAACE,YAAY,CAAC,KAAK,EAAEK,YAAG,CAAC;AACnCP,UAAU,CAACE,YAAY,CAAC,IAAI,EAAEM,WAAI,CAAC;AACnCR,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEO,gBAAO,CAAC;AAC3CT,UAAU,CAACE,YAAY,CAAC,MAAM,EAAEQ,aAAI,CAAC;AACrCV,UAAU,CAACE,YAAY,CAAC,IAAI,EAAES,WAAE,CAAC;AACjCX,UAAU,CAACE,YAAY,CAAC,KAAK,EAAEU,YAAG,CAAC;AACnCZ,UAAU,CAACE,YAAY,CAAC,WAAW,EAAEW,kBAAS,CAAC;AAC/Cb,UAAU,CAACE,YAAY,CAAC,WAAW,EAAEY,kBAAS,CAAC;AAC/Cd,UAAU,CAACE,YAAY,CAAC,QAAQ,EAAEa,eAAM,CAAC;AACzCf,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEc,gBAAO,CAAC;AAC3ChB,UAAU,CAACE,YAAY,CAAC,UAAU,EAAEe,iBAAQ,CAAC;AAC7CjB,UAAU,CAACE,YAAY,CAAC,OAAO,EAAEgB,cAAK,CAAC;AACvClB,UAAU,CAACE,YAAY,CAAC,UAAU,EAAEiB,iBAAQ,CAAC;AAC7CnB,UAAU,CAACE,YAAY,CAAC,KAAK,EAAEkB,YAAG,CAAC;AACnCpB,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEmB,gBAAO,CAAC;AAC3CrB,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEoB,gBAAO,CAAC;AAC3CtB,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEqB,gBAAO,CAAC;AAC3CvB,UAAU,CAACE,YAAY,CAAC,SAAS,EAAEsB,gBAAO,CAAC;AAC3CxB,UAAU,CAACE,YAAY,CAAC,MAAM,EAAEuB,aAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/validation",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-unstable.2af142b57e",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,22 +14,24 @@
|
|
|
14
14
|
],
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@babel/runtime": "7.
|
|
17
|
+
"@babel/runtime": "7.20.13",
|
|
18
18
|
"isnumeric": "0.3.3",
|
|
19
19
|
"lodash": "4.17.21"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@babel/cli": "^7.
|
|
23
|
-
"@babel/core": "^7.
|
|
24
|
-
"@babel/preset-env": "^7.
|
|
25
|
-
"@babel/preset-typescript": "^7.
|
|
26
|
-
"@
|
|
27
|
-
"@webiny/
|
|
22
|
+
"@babel/cli": "^7.19.3",
|
|
23
|
+
"@babel/core": "^7.19.3",
|
|
24
|
+
"@babel/preset-env": "^7.19.4",
|
|
25
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
26
|
+
"@types/lodash": "^4.14.190",
|
|
27
|
+
"@webiny/cli": "^0.0.0-unstable.2af142b57e",
|
|
28
|
+
"@webiny/project-utils": "^0.0.0-unstable.2af142b57e",
|
|
28
29
|
"babel-plugin-lodash": "^3.3.4",
|
|
29
|
-
"jest": "^
|
|
30
|
+
"jest": "^28.1.0",
|
|
30
31
|
"merge": "^1.2.1",
|
|
31
32
|
"rimraf": "^3.0.2",
|
|
32
|
-
"
|
|
33
|
+
"ttypescript": "^1.5.13",
|
|
34
|
+
"typescript": "4.7.4"
|
|
33
35
|
},
|
|
34
36
|
"publishConfig": {
|
|
35
37
|
"access": "public",
|
|
@@ -39,5 +41,5 @@
|
|
|
39
41
|
"build": "yarn webiny run build",
|
|
40
42
|
"watch": "yarn webiny run watch"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "2af142b57e7cdc433f5098b3b6d43ae6caa5d54e"
|
|
43
45
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import ValidationError from "./validationError";
|
|
2
|
+
declare type ValidatorResult = boolean | ValidationError | void | Promise<boolean | ValidationError | void>;
|
|
1
3
|
/**
|
|
2
4
|
* @typedef Validator
|
|
3
5
|
* @name Validator
|
|
@@ -6,22 +8,23 @@
|
|
|
6
8
|
* @param {Array<string>} parameters (Optional) This represents an array validator parameters.
|
|
7
9
|
* @throws {ValidationError}
|
|
8
10
|
*/
|
|
9
|
-
export declare type Validator = (value: any, params
|
|
11
|
+
export declare type Validator = (value: any, params?: string[]) => ValidatorResult;
|
|
10
12
|
/**
|
|
11
13
|
* @typedef ValidateOptions
|
|
12
14
|
* @name ValidateOptions
|
|
13
15
|
* @description This is an object containing validation options.
|
|
14
16
|
* @property {boolean} throw Should validation throw on failure? Default: true.
|
|
15
17
|
*/
|
|
16
|
-
export
|
|
18
|
+
export interface ValidateOptions {
|
|
17
19
|
throw?: boolean;
|
|
18
|
-
}
|
|
20
|
+
}
|
|
19
21
|
/**
|
|
20
22
|
* @private
|
|
21
23
|
* @typedef ParsedValidators
|
|
22
24
|
* @name ParsedValidators
|
|
23
25
|
* @description An object containing validators with parameters: `{ [string]: Array<string> }`.
|
|
24
26
|
*/
|
|
25
|
-
export
|
|
27
|
+
export interface ParsedValidators {
|
|
26
28
|
[key: string]: Array<string>;
|
|
27
|
-
}
|
|
29
|
+
}
|
|
30
|
+
export {};
|
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\ntype ValidatorResult = boolean | ValidationError | void | Promise<boolean | ValidationError | void>;\n/**\n * @typedef Validator\n * @name Validator\n * @description This type defines the validator function.\n * @param {any} value This is the value being validated.\n * @param {Array<string>} parameters (Optional) This represents an array validator parameters.\n * @throws {ValidationError}\n */\nexport type Validator = (value: any, params?: string[]) => ValidatorResult;\n\n/**\n * @typedef ValidateOptions\n * @name ValidateOptions\n * @description This is an object containing validation options.\n * @property {boolean} throw Should validation throw on failure? Default: true.\n */\nexport interface ValidateOptions {\n throw?: boolean;\n}\n\n/**\n * @private\n * @typedef ParsedValidators\n * @name ParsedValidators\n * @description An object containing validators with parameters: `{ [string]: Array<string> }`.\n */\nexport interface ParsedValidators {\n [key: string]: Array<string>;\n}\n"],"mappings":""}
|
package/validation.d.ts
CHANGED
|
@@ -49,8 +49,8 @@ declare class Validation {
|
|
|
49
49
|
* @returns {Promise<boolean | ValidationError>}
|
|
50
50
|
*/
|
|
51
51
|
validateSync(value: any, validators: string, options?: ValidateOptions): boolean | ValidationError;
|
|
52
|
-
create(validators: string):
|
|
53
|
-
createSync(validators: string):
|
|
52
|
+
create(validators: string): Validator;
|
|
53
|
+
createSync(validators: string): Validator;
|
|
54
54
|
/**
|
|
55
55
|
* Parses a string of validators with parameters.
|
|
56
56
|
* @param validators A list of comma-separated validators (eg. required,number,gt:20).
|
package/validation.js
CHANGED
|
@@ -1,31 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
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,77 +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();
|
|
153
|
+
if (!vName) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
186
156
|
parsedValidators[vName] = params;
|
|
187
157
|
});
|
|
188
158
|
return parsedValidators;
|
|
189
159
|
}
|
|
190
|
-
|
|
191
160
|
}
|
|
192
|
-
|
|
193
161
|
var _default = Validation;
|
|
194
162
|
exports.default = _default;
|
|
@@ -0,0 +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":"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"}
|
package/validationError.d.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* This class is used by validators to throw an error when value validation fails.
|
|
3
3
|
*/
|
|
4
4
|
declare class ValidationError extends Error {
|
|
5
|
-
message: string;
|
|
6
|
-
validator: string;
|
|
7
|
-
value: any;
|
|
8
|
-
constructor(message?: string, validator?: string, value?:
|
|
5
|
+
readonly message: string;
|
|
6
|
+
readonly validator: string | null;
|
|
7
|
+
readonly value: any;
|
|
8
|
+
constructor(message?: string, validator?: string | null, value?: string | null);
|
|
9
9
|
}
|
|
10
10
|
export default ValidationError;
|
package/validationError.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
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;
|
|
@@ -0,0 +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":";;;;;;;;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"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
declare const _default: (value: any) => void;
|
|
2
1
|
/**
|
|
3
2
|
* @name creditCard
|
|
4
3
|
* @description Credit card validator. This validator will check if the given value is a credit card number.
|
|
@@ -12,4 +11,5 @@ declare const _default: (value: any) => void;
|
|
|
12
11
|
* // Invalid
|
|
13
12
|
* });
|
|
14
13
|
*/
|
|
14
|
+
declare const _default: (value: any) => void;
|
|
15
15
|
export default _default;
|
package/validators/creditCard.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
|
-
var _validationError = _interopRequireDefault(require("./../validationError"));
|
|
11
|
-
|
|
8
|
+
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
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;
|
|
@@ -0,0 +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":";;;;;;;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,IAAIA,KAAK,CAACC,MAAM,GAAG,EAAE,EAAE;IACnB,MAAM,IAAIC,wBAAe,CAAC,+BAA+B,CAAC;EAC9D;EAEA,IAAI,YAAY,CAACC,IAAI,CAACH,KAAK,CAAC,EAAE;IAC1B,MAAM,IAAIE,wBAAe,CAAC,6BAA6B,CAAC;EAC5D;EAEA,IAAIE,MAAM,GAAG,CAAC;EACd,IAAIC,MAAM,GAAG,CAAC;EACd,IAAIC,KAAK,GAAG,KAAK;EAEjBN,KAAK,GAAGA,KAAK,CAACO,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;EAC/BP,KAAK,GAAGA,KAAK,CAACO,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;EAEhC,KAAK,IAAIC,CAAC,GAAGR,KAAK,CAACC,MAAM,GAAG,CAAC,EAAEO,CAAC,IAAI,CAAC,EAAEA,CAAC,EAAE,EAAE;IACxC,MAAMC,MAAM,GAAGT,KAAK,CAACU,MAAM,CAACF,CAAC,CAAC;IAC9BH,MAAM,GAAGM,QAAQ,CAACF,MAAM,CAAC;IAEzB,IAAIH,KAAK,EAAE;MACPD,MAAM,IAAI,CAAC;MACX,IAAIA,MAAM,GAAG,CAAC,EAAE;QACZA,MAAM,IAAI,CAAC;MACf;IACJ;IAEAD,MAAM,IAAIC,MAAM;IAChBC,KAAK,GAAG,CAACA,KAAK;EAClB;EAEA,IAAIF,MAAM,GAAG,EAAE,KAAK,CAAC,EAAE;IACnB;EACJ;EAEA,MAAM,IAAIF,wBAAe,CAAC,6BAA6B,CAAC;AAC5D,CAAC;AAAA"}
|
package/validators/dateGte.d.ts
CHANGED
package/validators/dateGte.js
CHANGED
|
@@ -1,37 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
|
-
var _validationError = _interopRequireDefault(require("./../validationError"));
|
|
11
|
-
|
|
8
|
+
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
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
|
-
if (!value) {
|
|
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;
|
|
@@ -0,0 +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":";;;;;;;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.d.ts
CHANGED
package/validators/dateLte.js
CHANGED
|
@@ -1,37 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
|
-
var _validationError = _interopRequireDefault(require("./../validationError"));
|
|
11
|
-
|
|
8
|
+
var _validationError = _interopRequireDefault(require("../validationError"));
|
|
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
|
-
if (!value) {
|
|
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;
|
|
@@ -0,0 +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":";;;;;;;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"}
|