@webiny/validation 0.0.0-mt-1
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 +93 -0
- package/package.json +43 -0
- package/types.d.ts +27 -0
- package/types.js +5 -0
- package/validation.d.ts +62 -0
- package/validation.js +194 -0
- package/validationError.d.ts +10 -0
- package/validationError.js +29 -0
- package/validators/creditCard.d.ts +15 -0
- package/validators/creditCard.js +69 -0
- package/validators/dateGte.d.ts +5 -0
- package/validators/dateGte.js +37 -0
- package/validators/dateLte.d.ts +5 -0
- package/validators/dateLte.js +37 -0
- package/validators/email.d.ts +15 -0
- package/validators/email.js +41 -0
- package/validators/eq.d.ts +16 -0
- package/validators/eq.js +41 -0
- package/validators/gt.d.ts +16 -0
- package/validators/gt.js +38 -0
- package/validators/gte.d.ts +16 -0
- package/validators/gte.js +38 -0
- package/validators/in.d.ts +2 -0
- package/validators/in.js +27 -0
- package/validators/integer.d.ts +2 -0
- package/validators/integer.js +26 -0
- package/validators/json.d.ts +2 -0
- package/validators/json.js +24 -0
- package/validators/lt.d.ts +2 -0
- package/validators/lt.js +26 -0
- package/validators/lte.d.ts +2 -0
- package/validators/lte.js +26 -0
- package/validators/maxLength.d.ts +2 -0
- package/validators/maxLength.js +44 -0
- package/validators/minLength.d.ts +2 -0
- package/validators/minLength.js +44 -0
- package/validators/number.d.ts +8 -0
- package/validators/number.js +34 -0
- package/validators/numeric.d.ts +8 -0
- package/validators/numeric.js +37 -0
- package/validators/password.d.ts +2 -0
- package/validators/password.js +26 -0
- package/validators/phone.d.ts +2 -0
- package/validators/phone.js +26 -0
- package/validators/required.d.ts +2 -0
- package/validators/required.js +22 -0
- package/validators/time/index.d.ts +6 -0
- package/validators/time/index.js +79 -0
- package/validators/timeGte.d.ts +5 -0
- package/validators/timeGte.js +38 -0
- package/validators/timeLte.d.ts +5 -0
- package/validators/timeLte.js +38 -0
- package/validators/url.d.ts +2 -0
- package/validators/url.js +59 -0
package/validation.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
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;
|
|
@@ -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,29 @@
|
|
|
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;
|
|
@@ -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,69 @@
|
|
|
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;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
* Validates that given value is a greater or equal date to a gteValue
|
|
14
|
+
*/
|
|
15
|
+
var _default = (value, params) => {
|
|
16
|
+
if (!value) {
|
|
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,37 @@
|
|
|
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
|
+
* Validates that given value is a lesser than or equal date to a lteValue
|
|
14
|
+
*/
|
|
15
|
+
var _default = (value, params) => {
|
|
16
|
+
if (!value) {
|
|
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,15 @@
|
|
|
1
|
+
declare const _default: (value: any) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @name email
|
|
4
|
+
* @description Email validator. This validator checks if the given value is a valid email address.
|
|
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('email@gmail.com', 'email').then(() => {
|
|
10
|
+
* // Valid
|
|
11
|
+
* }).catch(e => {
|
|
12
|
+
* // Invalid
|
|
13
|
+
* });
|
|
14
|
+
*/
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,41 @@
|
|
|
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 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,16 @@
|
|
|
1
|
+
declare const _default: (value: any, params: Array<string>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @name eq
|
|
4
|
+
* @description Equality validator. This validator checks if the given values are equal.
|
|
5
|
+
* @param {any} value This is the value that will be validated.
|
|
6
|
+
* @param {Array<string>} params This is the value to validate against. It is passed as a validator parameter: `eq:valueToCompareWith`
|
|
7
|
+
* @throws {ValidationError}
|
|
8
|
+
* @example
|
|
9
|
+
* import { validation } from '@webiny/validation';
|
|
10
|
+
* validation.validate('email@gmail.com', 'eq:another@gmail.com').then(() => {
|
|
11
|
+
* // Valid
|
|
12
|
+
* }).catch(e => {
|
|
13
|
+
* // Invalid
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export default _default;
|
package/validators/eq.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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 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) {
|
|
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,16 @@
|
|
|
1
|
+
declare const _default: (value: any, params: Array<string>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @name gt
|
|
4
|
+
* @description "Greater than" validator. This validator checks if the given values is greater than the `min` value.
|
|
5
|
+
* @param {any} value This is the value that will be validated.
|
|
6
|
+
* @param {Array<string>} params This is the value to validate against. It is passed as a validator parameter: `gt:valueToCompareAgainst`
|
|
7
|
+
* @throws {ValidationError}
|
|
8
|
+
* @example
|
|
9
|
+
* import { validation } from '@webiny/validation';
|
|
10
|
+
* validation.validate(10, 'gt:100').then(() => {
|
|
11
|
+
* // Valid
|
|
12
|
+
* }).catch(e => {
|
|
13
|
+
* // Invalid
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export default _default;
|
package/validators/gt.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 gt
|
|
14
|
+
* @description "Greater than" validator. This validator checks if the given values is greater than the `min` value.
|
|
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: `gt:valueToCompareAgainst`
|
|
17
|
+
* @throws {ValidationError}
|
|
18
|
+
* @example
|
|
19
|
+
* import { validation } from '@webiny/validation';
|
|
20
|
+
* validation.validate(10, 'gt:100').then(() => {
|
|
21
|
+
* // Valid
|
|
22
|
+
* }).catch(e => {
|
|
23
|
+
* // Invalid
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
var _default = (value, params) => {
|
|
27
|
+
if (!value) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (parseFloat(value) > parseFloat(params[0])) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw new _validationError.default("Value needs to be greater than " + params[0] + ".");
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.default = _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare const _default: (value: any, params: Array<string>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @name gte
|
|
4
|
+
* @description "Greater than or equals" validator. This validator checks if the given values is greater than or equal to the `min` value.
|
|
5
|
+
* @param {any} value This is the value that will be validated.
|
|
6
|
+
* @param {any} min This is the value to validate against. It is passed as a validator parameter: `gte:valueToCompareAgainst`
|
|
7
|
+
* @throws {ValidationError}
|
|
8
|
+
* @example
|
|
9
|
+
* import { validation } from '@webiny/validation';
|
|
10
|
+
* validation.validate(10, 'gte:100').then(() => {
|
|
11
|
+
* // Valid
|
|
12
|
+
* }).catch(e => {
|
|
13
|
+
* // Invalid
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export default _default;
|
|
@@ -0,0 +1,38 @@
|
|
|
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 gte
|
|
14
|
+
* @description "Greater than or equals" validator. This validator checks if the given values is greater than or equal to the `min` value.
|
|
15
|
+
* @param {any} value This is the value that will be validated.
|
|
16
|
+
* @param {any} min This is the value to validate against. It is passed as a validator parameter: `gte:valueToCompareAgainst`
|
|
17
|
+
* @throws {ValidationError}
|
|
18
|
+
* @example
|
|
19
|
+
* import { validation } from '@webiny/validation';
|
|
20
|
+
* validation.validate(10, 'gte:100').then(() => {
|
|
21
|
+
* // Valid
|
|
22
|
+
* }).catch(e => {
|
|
23
|
+
* // Invalid
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
var _default = (value, params) => {
|
|
27
|
+
if (!value) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (parseFloat(value) >= parseFloat(params[0])) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw new _validationError.default("Value needs to be greater than or equal to " + params[0] + ".");
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.default = _default;
|
package/validators/in.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
// In array validator. This validator checks if the given value is allowed to.
|
|
13
|
+
var _default = (value, params) => {
|
|
14
|
+
if (!value) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
value = value + "";
|
|
19
|
+
|
|
20
|
+
if (params.includes(value)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
throw new _validationError.default("Value must be one of the following: " + params.join(", ") + ".");
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.default = _default;
|