@webiny/validation 0.0.0-ee-vpcs.549378cf03

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.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +409 -0
  3. package/index.d.ts +4 -0
  4. package/index.js +96 -0
  5. package/index.js.map +1 -0
  6. package/package.json +45 -0
  7. package/types.d.ts +30 -0
  8. package/types.js +5 -0
  9. package/types.js.map +1 -0
  10. package/validation.d.ts +62 -0
  11. package/validation.js +199 -0
  12. package/validation.js.map +1 -0
  13. package/validationError.d.ts +10 -0
  14. package/validationError.js +29 -0
  15. package/validationError.js.map +1 -0
  16. package/validators/creditCard.d.ts +15 -0
  17. package/validators/creditCard.js +69 -0
  18. package/validators/creditCard.js.map +1 -0
  19. package/validators/dateGte.d.ts +5 -0
  20. package/validators/dateGte.js +37 -0
  21. package/validators/dateGte.js.map +1 -0
  22. package/validators/dateLte.d.ts +5 -0
  23. package/validators/dateLte.js +37 -0
  24. package/validators/dateLte.js.map +1 -0
  25. package/validators/email.d.ts +15 -0
  26. package/validators/email.js +41 -0
  27. package/validators/email.js.map +1 -0
  28. package/validators/eq.d.ts +16 -0
  29. package/validators/eq.js +41 -0
  30. package/validators/eq.js.map +1 -0
  31. package/validators/gt.d.ts +16 -0
  32. package/validators/gt.js +38 -0
  33. package/validators/gt.js.map +1 -0
  34. package/validators/gte.d.ts +16 -0
  35. package/validators/gte.js +38 -0
  36. package/validators/gte.js.map +1 -0
  37. package/validators/in.d.ts +2 -0
  38. package/validators/in.js +27 -0
  39. package/validators/in.js.map +1 -0
  40. package/validators/integer.d.ts +2 -0
  41. package/validators/integer.js +26 -0
  42. package/validators/integer.js.map +1 -0
  43. package/validators/json.d.ts +2 -0
  44. package/validators/json.js +24 -0
  45. package/validators/json.js.map +1 -0
  46. package/validators/lt.d.ts +2 -0
  47. package/validators/lt.js +26 -0
  48. package/validators/lt.js.map +1 -0
  49. package/validators/lte.d.ts +2 -0
  50. package/validators/lte.js +26 -0
  51. package/validators/lte.js.map +1 -0
  52. package/validators/maxLength.d.ts +2 -0
  53. package/validators/maxLength.js +44 -0
  54. package/validators/maxLength.js.map +1 -0
  55. package/validators/minLength.d.ts +2 -0
  56. package/validators/minLength.js +44 -0
  57. package/validators/minLength.js.map +1 -0
  58. package/validators/number.d.ts +8 -0
  59. package/validators/number.js +34 -0
  60. package/validators/number.js.map +1 -0
  61. package/validators/numeric.d.ts +8 -0
  62. package/validators/numeric.js +42 -0
  63. package/validators/numeric.js.map +1 -0
  64. package/validators/password.d.ts +2 -0
  65. package/validators/password.js +26 -0
  66. package/validators/password.js.map +1 -0
  67. package/validators/phone.d.ts +2 -0
  68. package/validators/phone.js +26 -0
  69. package/validators/phone.js.map +1 -0
  70. package/validators/required.d.ts +2 -0
  71. package/validators/required.js +22 -0
  72. package/validators/required.js.map +1 -0
  73. package/validators/slug.d.ts +2 -0
  74. package/validators/slug.js +26 -0
  75. package/validators/slug.js.map +1 -0
  76. package/validators/time/index.d.ts +6 -0
  77. package/validators/time/index.js +79 -0
  78. package/validators/time/index.js.map +1 -0
  79. package/validators/timeGte.d.ts +5 -0
  80. package/validators/timeGte.js +38 -0
  81. package/validators/timeGte.js.map +1 -0
  82. package/validators/timeLte.d.ts +5 -0
  83. package/validators/timeLte.js +38 -0
  84. package/validators/timeLte.js.map +1 -0
  85. package/validators/url.d.ts +2 -0
  86. package/validators/url.js +59 -0
  87. package/validators/url.js.map +1 -0
@@ -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): Validator;
53
+ createSync(validators: string): Validator;
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,199 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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
+
187
+ if (!vName) {
188
+ return;
189
+ }
190
+
191
+ parsedValidators[vName] = params;
192
+ });
193
+ return parsedValidators;
194
+ }
195
+
196
+ }
197
+
198
+ var _default = Validation;
199
+ 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":";;;;;;;;;;;;;;;;;AACA;;AAGA,MAAMA,OAAO,GAAIC,UAAD,IAAkE;EAC9E,OAAOC,MAAM,CAACF,OAAP,CAAeC,UAAf,CAAP;AACH,CAFD;;AAIA,MAAME,YAAY,GAAG,6EAArB;AAMA,MAAMC,iBAAoC,GAAG;EACzCC,KAAK,EAAE,EADkC;EAEzCC,IAAI,EAAE;AAFmC,CAA7C;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,UAAN,CAAiB;EACb;AACJ;AACA;AACA;EAKIC,WAAW,GAAG;IAAA;IACV,KAAKC,YAAL,GAAoB,EAApB;EACH;EAED;AACJ;AACA;AACA;AACA;AACA;;;EACIC,YAAY,CAACC,IAAD,EAAeC,QAAf,EAA0C;IAClD,KAAKH,YAAL,CAAkBE,IAAlB,IAA0BC,QAA1B;IACA,OAAO,IAAP;EACH;EAED;AACJ;AACA;AACA;AACA;;;EACIC,YAAY,CAACF,IAAD,EAA0B;IAClC,IAAI,CAAC,KAAKF,YAAL,CAAkBE,IAAlB,CAAL,EAA8B;MAC1B,MAAM,IAAIG,wBAAJ,CAAoB,gBAAgBH,IAAhB,GAAuB,mBAA3C,EAAgEA,IAAhE,CAAN;IACH;;IACD,OAAO,KAAKF,YAAL,CAAkBE,IAAlB,CAAP;EACH;EAED;AACJ;AACA;AACA;AACA;AACA;AACA;;;EACkB,MAARI,QAAQ,CACVC,KADU,EAEVf,UAFU,EAGVgB,OAAwB,GAAG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AhB,QAAQ,CAACiB,OAAT,CAAkBC,CAAD,IAAe;MAC5B,MAAMZ,MAAM,GAAG,oBAAOY,CAAP,EAAUH,KAAV,CAAgB,GAAhB,CAAf;MACA,MAAMI,KAAK,GAAGb,MAAM,CAACc,KAAP,EAAd;;MACA,IAAI,CAACD,KAAL,EAAY;QACR;MACH;;MACDH,gBAAgB,CAACG,KAAD,CAAhB,GAA0Bb,MAA1B;IACH,CAPD;IAQA,OAAOU,gBAAP;EACH;;AAnJY;;eAsJFxB,U"}
@@ -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
+ readonly message: string;
6
+ readonly validator: string | null;
7
+ readonly value: any;
8
+ constructor(message?: string, validator?: string | null, value?: string | null);
9
+ }
10
+ export default ValidationError;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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;
@@ -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,eAAN,SAA8BC,KAA9B,CAAoC;EAKhCC,WAAW,CAACC,OAAO,GAAG,EAAX,EAAeC,SAAwB,GAAG,IAA1C,EAAgDC,KAAoB,GAAG,IAAvE,EAA6E;IACpF;IADoF;IAAA;IAAA;IAEpF,KAAKF,OAAL,GAAeA,OAAf;IACA,KAAKC,SAAL,GAAiBA,SAAjB;IACA,KAAKC,KAAL,GAAaA,KAAb;EACH;;AAV+B;;eAarBL,e"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @name creditCard
3
+ * @description Credit card validator. This validator will check if the given value is a credit card number.
4
+ * @param {any} value This is the value that will be validated.
5
+ * @throws {ValidationError}
6
+ * @example
7
+ * import { validation } from '@webiny/validation';
8
+ * validation.validate('notACreditCard', 'creditCard').then(() => {
9
+ * // Valid
10
+ * }).catch(e => {
11
+ * // Invalid
12
+ * });
13
+ */
14
+ declare const _default: (value: any) => void;
15
+ export default _default;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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;
@@ -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;eACgBA,KAAD,IAAsB;EACjC,IAAI,CAACA,KAAL,EAAY;IACR;EACH;;EACDA,KAAK,GAAGA,KAAK,GAAG,EAAhB;;EAEA,IAAIA,KAAK,CAACC,MAAN,GAAe,EAAnB,EAAuB;IACnB,MAAM,IAAIC,wBAAJ,CAAoB,+BAApB,CAAN;EACH;;EAED,IAAI,aAAaC,IAAb,CAAkBH,KAAlB,CAAJ,EAA8B;IAC1B,MAAM,IAAIE,wBAAJ,CAAoB,6BAApB,CAAN;EACH;;EAED,IAAIE,MAAM,GAAG,CAAb;EACA,IAAIC,MAAM,GAAG,CAAb;EACA,IAAIC,KAAK,GAAG,KAAZ;EAEAN,KAAK,GAAGA,KAAK,CAACO,OAAN,CAAc,IAAd,EAAoB,EAApB,CAAR;EACAP,KAAK,GAAGA,KAAK,CAACO,OAAN,CAAc,KAAd,EAAqB,EAArB,CAAR;;EAEA,KAAK,IAAIC,CAAC,GAAGR,KAAK,CAACC,MAAN,GAAe,CAA5B,EAA+BO,CAAC,IAAI,CAApC,EAAuCA,CAAC,EAAxC,EAA4C;IACxC,MAAMC,MAAM,GAAGT,KAAK,CAACU,MAAN,CAAaF,CAAb,CAAf;IACAH,MAAM,GAAGM,QAAQ,CAACF,MAAD,CAAjB;;IAEA,IAAIH,KAAJ,EAAW;MACPD,MAAM,IAAI,CAAV;;MACA,IAAIA,MAAM,GAAG,CAAb,EAAgB;QACZA,MAAM,IAAI,CAAV;MACH;IACJ;;IAEDD,MAAM,IAAIC,MAAV;IACAC,KAAK,GAAG,CAACA,KAAT;EACH;;EAED,IAAIF,MAAM,GAAG,EAAT,KAAgB,CAApB,EAAuB;IACnB;EACH;;EAED,MAAM,IAAIF,wBAAJ,CAAoB,6BAApB,CAAN;AACH,C"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Validates that given value is a greater or equal date to a gteValue
3
+ */
4
+ declare const _default: (value: string, params?: string[]) => void;
5
+ export default _default;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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
+ * Validates that given value is a greater or equal date to a gteValue
14
+ */
15
+ var _default = (value, params) => {
16
+ if (!value || !params) {
17
+ return;
18
+ } // we need to join because validation params are being split by :
19
+
20
+
21
+ const gteValue = params.join(":");
22
+
23
+ if (!gteValue) {
24
+ return;
25
+ }
26
+
27
+ const date = new Date(value);
28
+ const gteDate = new Date(gteValue);
29
+
30
+ if (date >= gteDate) {
31
+ return;
32
+ }
33
+
34
+ throw new _validationError.default(`Value needs to be greater than or equal to "${gteDate.toISOString()}".`);
35
+ };
36
+
37
+ 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;eACe,CAACA,KAAD,EAAgBC,MAAhB,KAAsC;EACjD,IAAI,CAACD,KAAD,IAAU,CAACC,MAAf,EAAuB;IACnB;EACH,CAHgD,CAIjD;;;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAP,CAAY,GAAZ,CAAjB;;EACA,IAAI,CAACD,QAAL,EAAe;IACX;EACH;;EAED,MAAME,IAAI,GAAG,IAAIC,IAAJ,CAASL,KAAT,CAAb;EACA,MAAMM,OAAO,GAAG,IAAID,IAAJ,CAASH,QAAT,CAAhB;;EAEA,IAAIE,IAAI,IAAIE,OAAZ,EAAqB;IACjB;EACH;;EACD,MAAM,IAAIC,wBAAJ,CACD,+CAA8CD,OAAO,CAACE,WAAR,EAAsB,IADnE,CAAN;AAGH,C"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Validates that given value is a lesser than or equal date to a lteValue
3
+ */
4
+ declare const _default: (value: string, params?: string[]) => void;
5
+ export default _default;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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
+ * Validates that given value is a lesser than or equal date to a lteValue
14
+ */
15
+ var _default = (value, params) => {
16
+ if (!value || !params) {
17
+ return;
18
+ } // we need to join because validation params are being split by :
19
+
20
+
21
+ const lteValue = params.join(":");
22
+
23
+ if (!lteValue) {
24
+ return;
25
+ }
26
+
27
+ const date = new Date(value);
28
+ const lteDate = new Date(lteValue);
29
+
30
+ if (date <= lteDate) {
31
+ return;
32
+ }
33
+
34
+ throw new _validationError.default(`Value needs to be lesser than or equal to "${lteDate.toISOString()}".`);
35
+ };
36
+
37
+ 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;eACe,CAACA,KAAD,EAAgBC,MAAhB,KAAsC;EACjD,IAAI,CAACD,KAAD,IAAU,CAACC,MAAf,EAAuB;IACnB;EACH,CAHgD,CAIjD;;;EACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,IAAP,CAAY,GAAZ,CAAjB;;EACA,IAAI,CAACD,QAAL,EAAe;IACX;EACH;;EAED,MAAME,IAAI,GAAG,IAAIC,IAAJ,CAASL,KAAT,CAAb;EACA,MAAMM,OAAO,GAAG,IAAID,IAAJ,CAASH,QAAT,CAAhB;;EAEA,IAAIE,IAAI,IAAIE,OAAZ,EAAqB;IACjB;EACH;;EACD,MAAM,IAAIC,wBAAJ,CACD,8CAA6CD,OAAO,CAACE,WAAR,EAAsB,IADlE,CAAN;AAGH,C"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @name email
3
+ * @description Email validator. This validator checks if the given value is a valid email address.
4
+ * @param {any} value This is the value that will be validated.
5
+ * @throws {ValidationError}
6
+ * @example
7
+ * import { validation } from '@webiny/validation';
8
+ * validation.validate('email@gmail.com', 'email').then(() => {
9
+ * // Valid
10
+ * }).catch(e => {
11
+ * // Invalid
12
+ * });
13
+ */
14
+ declare const _default: (value: any) => void;
15
+ export default _default;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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 email
14
+ * @description Email validator. This validator checks if the given value is a valid email address.
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('email@gmail.com', 'email').then(() => {
20
+ * // Valid
21
+ * }).catch(e => {
22
+ * // Invalid
23
+ * });
24
+ */
25
+ var _default = value => {
26
+ if (!value) {
27
+ return;
28
+ }
29
+
30
+ value = value + ""; // eslint-disable-next-line
31
+
32
+ const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
33
+
34
+ if (!value || value.length && re.test(value)) {
35
+ return;
36
+ }
37
+
38
+ throw new _validationError.default("Value must be a valid e-mail address.");
39
+ };
40
+
41
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["value","re","length","test","ValidationError"],"sources":["email.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name email\n * @description Email validator. This validator checks if the given value is a valid email address.\n * @param {any} value This is the value that will be validated.\n * @throws {ValidationError}\n * @example\n * import { validation } from '@webiny/validation';\n * validation.validate('email@gmail.com', 'email').then(() => {\n * // Valid\n * }).catch(e => {\n * // Invalid\n * });\n */\nexport default (value: any): void => {\n if (!value) {\n return;\n }\n value = value + \"\";\n\n // eslint-disable-next-line\n const re =\n /^(([^<>()[\\]\\\\.,;:\\s@\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/;\n if (!value || (value.length && re.test(value))) {\n return;\n }\n throw new ValidationError(\"Value must be a valid e-mail address.\");\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eACgBA,KAAD,IAAsB;EACjC,IAAI,CAACA,KAAL,EAAY;IACR;EACH;;EACDA,KAAK,GAAGA,KAAK,GAAG,EAAhB,CAJiC,CAMjC;;EACA,MAAMC,EAAE,GACJ,sJADJ;;EAEA,IAAI,CAACD,KAAD,IAAWA,KAAK,CAACE,MAAN,IAAgBD,EAAE,CAACE,IAAH,CAAQH,KAAR,CAA/B,EAAgD;IAC5C;EACH;;EACD,MAAM,IAAII,wBAAJ,CAAoB,uCAApB,CAAN;AACH,C"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @name eq
3
+ * @description Equality validator. This validator checks if the given values are equal.
4
+ * @param {any} value This is the value that will be validated.
5
+ * @param {Array<string>} params This is the value to validate against. It is passed as a validator parameter: `eq:valueToCompareWith`
6
+ * @throws {ValidationError}
7
+ * @example
8
+ * import { validation } from '@webiny/validation';
9
+ * validation.validate('email@gmail.com', 'eq:another@gmail.com').then(() => {
10
+ * // Valid
11
+ * }).catch(e => {
12
+ * // Invalid
13
+ * });
14
+ */
15
+ declare const _default: (value: any, params?: string[]) => void;
16
+ export default _default;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
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 eq
14
+ * @description Equality validator. This validator checks if the given values are equal.
15
+ * @param {any} value This is the value that will be validated.
16
+ * @param {Array<string>} params This is the value to validate against. It is passed as a validator parameter: `eq:valueToCompareWith`
17
+ * @throws {ValidationError}
18
+ * @example
19
+ * import { validation } from '@webiny/validation';
20
+ * validation.validate('email@gmail.com', 'eq:another@gmail.com').then(() => {
21
+ * // Valid
22
+ * }).catch(e => {
23
+ * // Invalid
24
+ * });
25
+ */
26
+ var _default = (value, params) => {
27
+ if (!value || !params) {
28
+ return;
29
+ }
30
+
31
+ value = value + ""; // Intentionally put '==' instead of '===' because passed parameter for this validator is always sent inside a string (eg. "eq:test").
32
+ // eslint-disable-next-line
33
+
34
+ if (value == params[0]) {
35
+ return;
36
+ }
37
+
38
+ throw new _validationError.default("Value must be equal to " + params[0] + ".");
39
+ };
40
+
41
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["value","params","ValidationError"],"sources":["eq.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\n/**\n * @name eq\n * @description Equality validator. This validator checks if the given values are equal.\n * @param {any} value This is the value that will be validated.\n * @param {Array<string>} params This is the value to validate against. It is passed as a validator parameter: `eq:valueToCompareWith`\n * @throws {ValidationError}\n * @example\n * import { validation } from '@webiny/validation';\n * validation.validate('email@gmail.com', 'eq:another@gmail.com').then(() => {\n * // Valid\n * }).catch(e => {\n * // Invalid\n * });\n */\nexport default (value: any, params?: string[]) => {\n if (!value || !params) {\n return;\n }\n value = value + \"\";\n\n // Intentionally put '==' instead of '===' because passed parameter for this validator is always sent inside a string (eg. \"eq:test\").\n // eslint-disable-next-line\n if (value == params[0]) {\n return;\n }\n throw new ValidationError(\"Value must be equal to \" + params[0] + \".\");\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eACe,CAACA,KAAD,EAAaC,MAAb,KAAmC;EAC9C,IAAI,CAACD,KAAD,IAAU,CAACC,MAAf,EAAuB;IACnB;EACH;;EACDD,KAAK,GAAGA,KAAK,GAAG,EAAhB,CAJ8C,CAM9C;EACA;;EACA,IAAIA,KAAK,IAAIC,MAAM,CAAC,CAAD,CAAnB,EAAwB;IACpB;EACH;;EACD,MAAM,IAAIC,wBAAJ,CAAoB,4BAA4BD,MAAM,CAAC,CAAD,CAAlC,GAAwC,GAA5D,CAAN;AACH,C"}