@nr1e/commons 0.0.2-alpha.15 → 0.0.2-alpha.17
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/errors/errors.d.ts +1 -0
- package/errors/errors.js +22 -1
- package/errors/errors.js.map +1 -1
- package/package.json +2 -5
- package/validator/validators.d.ts +89 -8
- package/validator/validators.js +181 -57
- package/validator/validators.js.map +1 -1
package/errors/errors.d.ts
CHANGED
package/errors/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NotImplementedError = exports.isNotImplementedError = exports.UnsupportedMediaTypeError = exports.isUnsupportedMediaTypeError = exports.ConflictError = exports.isConflictError = exports.InternalServerError = exports.isInternalServerError = exports.BadRequestError = exports.isBadRequestError = exports.ValidationError = exports.isValidationError = exports.ForbiddenError = exports.isForbiddenError = exports.NotFoundError = exports.isNotFoundError = exports.isHttpError = void 0;
|
|
3
|
+
exports.toError = exports.NotImplementedError = exports.isNotImplementedError = exports.UnsupportedMediaTypeError = exports.isUnsupportedMediaTypeError = exports.ConflictError = exports.isConflictError = exports.InternalServerError = exports.isInternalServerError = exports.BadRequestError = exports.isBadRequestError = exports.ValidationError = exports.isValidationError = exports.ForbiddenError = exports.isForbiddenError = exports.NotFoundError = exports.isNotFoundError = exports.isHttpError = void 0;
|
|
4
4
|
const http_1 = require("../http");
|
|
5
5
|
/**
|
|
6
6
|
* Checks if the given parameter is an HttpError.
|
|
@@ -214,4 +214,25 @@ class NotImplementedError extends Error {
|
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
exports.NotImplementedError = NotImplementedError;
|
|
217
|
+
function toError(code, message) {
|
|
218
|
+
switch (code) {
|
|
219
|
+
case http_1.HttpStatusCode.NOT_FOUND:
|
|
220
|
+
return new NotFoundError(message);
|
|
221
|
+
case http_1.HttpStatusCode.FORBIDDEN:
|
|
222
|
+
return new ForbiddenError(message);
|
|
223
|
+
case http_1.HttpStatusCode.BAD_REQUEST:
|
|
224
|
+
return new BadRequestError(message);
|
|
225
|
+
case http_1.HttpStatusCode.INTERNAL_SERVER_ERROR:
|
|
226
|
+
return new InternalServerError(message);
|
|
227
|
+
case http_1.HttpStatusCode.CONFLICT:
|
|
228
|
+
return new ConflictError(message);
|
|
229
|
+
case http_1.HttpStatusCode.UNSUPPORTED_MEDIA_TYPE:
|
|
230
|
+
return new UnsupportedMediaTypeError(message);
|
|
231
|
+
case http_1.HttpStatusCode.NOT_IMPLEMENTED:
|
|
232
|
+
return new NotImplementedError(message);
|
|
233
|
+
default:
|
|
234
|
+
return new Error(message);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
exports.toError = toError;
|
|
217
238
|
//# sourceMappingURL=errors.js.map
|
package/errors/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["errors.ts"],"names":[],"mappings":";;;AAAA,kCAAuC;AAavC;;;;GAIG;AACH,SAAgB,WAAW,CAAC,CAAiB;IAC3C,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;AACjE,CAAC;AAFD,kCAEC;AAED;;;;GAIG;AACH,SAAgB,eAAe,CAAC,CAAiB;IAC/C,OAAO,CAAC,CAAC,CACP,CAAC;QACD,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,IAAI,KAAK,eAAe,CAC3B,CAAC;AACJ,CAAC;AATD,0CASC;AAED;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;IAEtC,YAAY,OAAgB;QAC1B,OAAO,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,WAA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AC,CAAC;QACrC,KAAK,qBAAc,CAAC,WAAW;YAC7B,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QACtC,KAAK,qBAAc,CAAC,qBAAqB;YACvC,OAAO,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC;QAC1C,KAAK,qBAAc,CAAC,QAAQ;YAC1B,OAAO,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC;QACpC,KAAK,qBAAc,CAAC,sBAAsB;YACxC,OAAO,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAChD,KAAK,qBAAc,CAAC,eAAe;YACjC,OAAO,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC;QAC1C;YACE,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;KAC7B;AACH,CAAC;AAtBD,0BAsBC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nr1e/commons",
|
|
3
3
|
"description": "Provides common patterns for validation",
|
|
4
|
-
"version": "0.0.2-alpha.
|
|
4
|
+
"version": "0.0.2-alpha.17",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"author": "NR1E, Inc.",
|
|
@@ -36,15 +36,12 @@
|
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsc",
|
|
39
|
+
"postbuild": "prettier --check . && gts lint",
|
|
39
40
|
"watch": "tsc -w",
|
|
40
41
|
"test": "jest",
|
|
41
42
|
"lint": "gts lint",
|
|
42
43
|
"clean": "gts clean",
|
|
43
|
-
"compile": "tsc",
|
|
44
44
|
"fix": "gts fix",
|
|
45
|
-
"pretest": "pnpm run compile",
|
|
46
|
-
"posttest": "pnpm run lint",
|
|
47
|
-
"check": "prettier --check .",
|
|
48
45
|
"makepretty": "prettier --write ."
|
|
49
46
|
}
|
|
50
47
|
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests if a value is null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param o the value to check
|
|
5
|
+
*/
|
|
6
|
+
export declare function isNotNull(o?: unknown): o is NonNullable<unknown>;
|
|
1
7
|
/**
|
|
2
8
|
* Throws a ValidationError if the value is null or undefined.
|
|
3
9
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -6,6 +12,12 @@
|
|
|
6
12
|
* @param o the value to check
|
|
7
13
|
*/
|
|
8
14
|
export declare function notNull(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Tests if a value is empty, null, undefined or has a length of 0.
|
|
17
|
+
*
|
|
18
|
+
* @param o the value to check
|
|
19
|
+
*/
|
|
20
|
+
export declare function isNotEmpty(o?: unknown): o is NonNullable<unknown>;
|
|
9
21
|
/**
|
|
10
22
|
* Throws a ValidationError if the value is null, undefined or the length is 0.
|
|
11
23
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -14,6 +26,12 @@ export declare function notNull(name: string, o?: unknown): asserts o is NonNull
|
|
|
14
26
|
* @param o the value to check
|
|
15
27
|
*/
|
|
16
28
|
export declare function notEmpty(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* Tests if a value is null, undefined, has a length of 0 or contains only whitespace.
|
|
31
|
+
*
|
|
32
|
+
* @param o the value to check
|
|
33
|
+
*/
|
|
34
|
+
export declare function isNotBlank(o?: unknown): o is NonNullable<unknown>;
|
|
17
35
|
/**
|
|
18
36
|
* Throws a ValidationError if the value is null, undefined, has a length of 0 or contains only whitespace.
|
|
19
37
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -23,14 +41,29 @@ export declare function notEmpty(name: string, o?: unknown): asserts o is NonNul
|
|
|
23
41
|
*/
|
|
24
42
|
export declare function notBlank(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
25
43
|
/**
|
|
26
|
-
*
|
|
44
|
+
* Tests if a value does not match the regular expression provided.
|
|
45
|
+
* Undefined and null values are skipped and not tested.
|
|
46
|
+
*
|
|
47
|
+
* @param regex the regular expression to test with
|
|
48
|
+
* @param o the value to check
|
|
49
|
+
*/
|
|
50
|
+
export declare function isMatch(regex: RegExp, o?: string | null): o is string;
|
|
51
|
+
/**
|
|
52
|
+
* Throws a ValidationError if the value matches the regular expression provided.
|
|
27
53
|
* Undefined and null values are skipped and not validated.
|
|
28
54
|
*
|
|
29
55
|
* @param name the name of the variable
|
|
30
56
|
* @param regex the regular expression to validate with
|
|
31
57
|
* @param o the value to check
|
|
32
58
|
*/
|
|
33
|
-
export declare function
|
|
59
|
+
export declare function match(name: string, regex: RegExp, o?: string | null): void;
|
|
60
|
+
/**
|
|
61
|
+
* Tests if a value is a valid email address.
|
|
62
|
+
* Undefined and null values are skipped and not validated.
|
|
63
|
+
*
|
|
64
|
+
* @param o the value to check
|
|
65
|
+
*/
|
|
66
|
+
export declare function isEmail(o?: string | null): o is string;
|
|
34
67
|
/**
|
|
35
68
|
* Throws a ValidationError if the value provided is not an email.
|
|
36
69
|
* Undefined and null values are skipped and not validated.
|
|
@@ -38,7 +71,15 @@ export declare function matches(name: string, regex: RegExp, o?: string | null):
|
|
|
38
71
|
* @param name the name of the variable
|
|
39
72
|
* @param o the value to check
|
|
40
73
|
*/
|
|
41
|
-
export declare function
|
|
74
|
+
export declare function email(name: string, o?: string | null): void;
|
|
75
|
+
/**
|
|
76
|
+
* Tests if a value has a length that is less than the provided length.
|
|
77
|
+
* Undefined and null values are skipped and not validated.
|
|
78
|
+
*
|
|
79
|
+
* @param length the maximum length of the variable
|
|
80
|
+
* @param o the value to check
|
|
81
|
+
*/
|
|
82
|
+
export declare function isMaxLength(length: number, o?: string | unknown[] | null): boolean;
|
|
42
83
|
/**
|
|
43
84
|
* Throws a ValidationError if the value provided has a length that exceeds the provided length.
|
|
44
85
|
* Undefined and null values are skipped and not validated.
|
|
@@ -48,6 +89,14 @@ export declare function isEmail(name: string, o?: string | null): void;
|
|
|
48
89
|
* @param o the value to check
|
|
49
90
|
*/
|
|
50
91
|
export declare function maxLength(name: string, length: number, o?: string | unknown[] | null): void;
|
|
92
|
+
/**
|
|
93
|
+
* Tests if a value has a length that is greater than the provided length.
|
|
94
|
+
* Undefined and null values are skipped and not validated.
|
|
95
|
+
*
|
|
96
|
+
* @param length the minimum length of the variable
|
|
97
|
+
* @param o the value to check
|
|
98
|
+
*/
|
|
99
|
+
export declare function isMinLength(length: number, o?: string | unknown[] | null): boolean;
|
|
51
100
|
/**
|
|
52
101
|
* Throws a ValidationError if the value provided has a length that is less than the provided length.
|
|
53
102
|
* Undefined and null values are skipped and not validated.
|
|
@@ -57,6 +106,13 @@ export declare function maxLength(name: string, length: number, o?: string | unk
|
|
|
57
106
|
* @param o the value to check
|
|
58
107
|
*/
|
|
59
108
|
export declare function minLength(name: string, length: number, o?: string | unknown[] | null): void;
|
|
109
|
+
/**
|
|
110
|
+
* Tests if a value provided is a number.
|
|
111
|
+
* Undefined and null values are skipped and not validated.
|
|
112
|
+
*
|
|
113
|
+
* @param o the value to check
|
|
114
|
+
*/
|
|
115
|
+
export declare function isNumber(o?: string | null | number): boolean;
|
|
60
116
|
/**
|
|
61
117
|
* Throws a ValidationError if the value provided is not a number.
|
|
62
118
|
* Undefined and null values are skipped and not validated.
|
|
@@ -64,7 +120,15 @@ export declare function minLength(name: string, length: number, o?: string | unk
|
|
|
64
120
|
* @param name the name of the variable
|
|
65
121
|
* @param o the value to check
|
|
66
122
|
*/
|
|
67
|
-
export declare function
|
|
123
|
+
export declare function number(name: string, o?: string | null | number): void;
|
|
124
|
+
/**
|
|
125
|
+
* Tests if a value is less than the provided minimum value.
|
|
126
|
+
* Undefined and null values are skipped and not validated.
|
|
127
|
+
*
|
|
128
|
+
* @param minValue the minimum value allowed
|
|
129
|
+
* @param o the value to check
|
|
130
|
+
*/
|
|
131
|
+
export declare function isMinValue(minValue: number, o?: string | number | null): boolean;
|
|
68
132
|
/**
|
|
69
133
|
* Throws a ValidationError if the value is less than the provided minimum value.
|
|
70
134
|
* Undefined and null values are skipped and not validated.
|
|
@@ -74,6 +138,14 @@ export declare function isNumber(name: string, o?: string | null | number): void
|
|
|
74
138
|
* @param o the value to check
|
|
75
139
|
*/
|
|
76
140
|
export declare function minValue(name: string, minValue: number, o?: number | string | null): void;
|
|
141
|
+
/**
|
|
142
|
+
* Tests if a value is more than the provided maximum value.
|
|
143
|
+
* Undefined and null values are skipped and not validated.
|
|
144
|
+
*
|
|
145
|
+
* @param maxValue the maximum value allowed
|
|
146
|
+
* @param o the value to check
|
|
147
|
+
*/
|
|
148
|
+
export declare function isMaxValue(maxValue: number, o?: string | number | null): boolean;
|
|
77
149
|
/**
|
|
78
150
|
* Throws a ValidationError if the value is more than the provided maximum value.
|
|
79
151
|
* Undefined and null values are skipped and not validated.
|
|
@@ -83,6 +155,15 @@ export declare function minValue(name: string, minValue: number, o?: number | st
|
|
|
83
155
|
* @param o the value to check
|
|
84
156
|
*/
|
|
85
157
|
export declare function maxValue(name: string, maxValue: number, o?: number | string | null): void;
|
|
158
|
+
/**
|
|
159
|
+
* Tests if the value is between the provided minimum and maximum values inclusive.
|
|
160
|
+
* Undefined and null values are skipped and not validated.
|
|
161
|
+
*
|
|
162
|
+
* @param minValue the minimum value allowed
|
|
163
|
+
* @param maxValue the maximum value allowed
|
|
164
|
+
* @param o the value to check
|
|
165
|
+
*/
|
|
166
|
+
export declare function isBetweenValues(minValue: number, maxValue: number, o?: string | number | null): boolean;
|
|
86
167
|
/**
|
|
87
168
|
* Throws a ValidationError if the value is not between the provided minimum and maximum values inclusive.
|
|
88
169
|
* Undefined and null values are skipped and not validated.
|
|
@@ -94,14 +175,14 @@ export declare function maxValue(name: string, maxValue: number, o?: number | st
|
|
|
94
175
|
*/
|
|
95
176
|
export declare function betweenValues(name: string, minValue: number, maxValue: number, o?: string | number | null): void;
|
|
96
177
|
export interface StringValidationOptions {
|
|
97
|
-
readonly name: string;
|
|
98
178
|
readonly required: boolean;
|
|
99
179
|
readonly minLength?: number;
|
|
100
180
|
readonly maxLength?: number;
|
|
101
181
|
readonly regex?: RegExp;
|
|
102
182
|
readonly notBlank?: boolean;
|
|
103
183
|
readonly notEmpty?: boolean;
|
|
104
|
-
readonly
|
|
105
|
-
readonly
|
|
184
|
+
readonly email?: boolean;
|
|
185
|
+
readonly number?: boolean;
|
|
106
186
|
}
|
|
107
|
-
export declare function
|
|
187
|
+
export declare function isValidString(options: StringValidationOptions, value?: unknown): value is string;
|
|
188
|
+
export declare function validString(name: string, options: StringValidationOptions, value?: unknown): void;
|
package/validator/validators.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.validString = exports.isValidString = exports.betweenValues = exports.isBetweenValues = exports.maxValue = exports.isMaxValue = exports.minValue = exports.isMinValue = exports.number = exports.isNumber = exports.minLength = exports.isMinLength = exports.maxLength = exports.isMaxLength = exports.email = exports.isEmail = exports.match = exports.isMatch = exports.notBlank = exports.isNotBlank = exports.notEmpty = exports.isNotEmpty = exports.notNull = exports.isNotNull = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
|
+
/**
|
|
6
|
+
* Tests if a value is null or undefined.
|
|
7
|
+
*
|
|
8
|
+
* @param o the value to check
|
|
9
|
+
*/
|
|
10
|
+
function isNotNull(o) {
|
|
11
|
+
return o !== undefined && o !== null;
|
|
12
|
+
}
|
|
13
|
+
exports.isNotNull = isNotNull;
|
|
5
14
|
/**
|
|
6
15
|
* Throws a ValidationError if the value is null or undefined.
|
|
7
16
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -10,11 +19,20 @@ const errors_1 = require("../errors");
|
|
|
10
19
|
* @param o the value to check
|
|
11
20
|
*/
|
|
12
21
|
function notNull(name, o) {
|
|
13
|
-
if (o
|
|
22
|
+
if (!isNotNull(o)) {
|
|
14
23
|
throw new errors_1.ValidationError(`${name} may not be null or undefined`);
|
|
15
24
|
}
|
|
16
25
|
}
|
|
17
26
|
exports.notNull = notNull;
|
|
27
|
+
/**
|
|
28
|
+
* Tests if a value is empty, null, undefined or has a length of 0.
|
|
29
|
+
*
|
|
30
|
+
* @param o the value to check
|
|
31
|
+
*/
|
|
32
|
+
function isNotEmpty(o) {
|
|
33
|
+
return !(o === undefined || o === null || o.toString().length === 0);
|
|
34
|
+
}
|
|
35
|
+
exports.isNotEmpty = isNotEmpty;
|
|
18
36
|
/**
|
|
19
37
|
* Throws a ValidationError if the value is null, undefined or the length is 0.
|
|
20
38
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -23,11 +41,20 @@ exports.notNull = notNull;
|
|
|
23
41
|
* @param o the value to check
|
|
24
42
|
*/
|
|
25
43
|
function notEmpty(name, o) {
|
|
26
|
-
if (o
|
|
44
|
+
if (!isNotEmpty(o)) {
|
|
27
45
|
throw new errors_1.ValidationError(`${name} may not be empty`);
|
|
28
46
|
}
|
|
29
47
|
}
|
|
30
48
|
exports.notEmpty = notEmpty;
|
|
49
|
+
/**
|
|
50
|
+
* Tests if a value is null, undefined, has a length of 0 or contains only whitespace.
|
|
51
|
+
*
|
|
52
|
+
* @param o the value to check
|
|
53
|
+
*/
|
|
54
|
+
function isNotBlank(o) {
|
|
55
|
+
return !(o === undefined || o === null || o.toString().trim().length === 0);
|
|
56
|
+
}
|
|
57
|
+
exports.isNotBlank = isNotBlank;
|
|
31
58
|
/**
|
|
32
59
|
* Throws a ValidationError if the value is null, undefined, has a length of 0 or contains only whitespace.
|
|
33
60
|
* This function also asserts the value to be NonNullable if the check passes.
|
|
@@ -36,25 +63,50 @@ exports.notEmpty = notEmpty;
|
|
|
36
63
|
* @param o the value to check
|
|
37
64
|
*/
|
|
38
65
|
function notBlank(name, o) {
|
|
39
|
-
if (o
|
|
66
|
+
if (!isNotBlank(o)) {
|
|
40
67
|
throw new errors_1.ValidationError(`${name} may not be blank`);
|
|
41
68
|
}
|
|
42
69
|
}
|
|
43
70
|
exports.notBlank = notBlank;
|
|
44
71
|
/**
|
|
45
|
-
*
|
|
72
|
+
* Tests if a value does not match the regular expression provided.
|
|
73
|
+
* Undefined and null values are skipped and not tested.
|
|
74
|
+
*
|
|
75
|
+
* @param regex the regular expression to test with
|
|
76
|
+
* @param o the value to check
|
|
77
|
+
*/
|
|
78
|
+
function isMatch(regex, o) {
|
|
79
|
+
if (o === undefined || o === null) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return o.match(regex) !== null;
|
|
83
|
+
}
|
|
84
|
+
exports.isMatch = isMatch;
|
|
85
|
+
/**
|
|
86
|
+
* Throws a ValidationError if the value matches the regular expression provided.
|
|
46
87
|
* Undefined and null values are skipped and not validated.
|
|
47
88
|
*
|
|
48
89
|
* @param name the name of the variable
|
|
49
90
|
* @param regex the regular expression to validate with
|
|
50
91
|
* @param o the value to check
|
|
51
92
|
*/
|
|
52
|
-
function
|
|
53
|
-
if (
|
|
93
|
+
function match(name, regex, o) {
|
|
94
|
+
if (!isMatch(regex, o)) {
|
|
54
95
|
throw new errors_1.ValidationError(`${name} must match ${regex}`);
|
|
55
96
|
}
|
|
56
97
|
}
|
|
57
|
-
exports.
|
|
98
|
+
exports.match = match;
|
|
99
|
+
/**
|
|
100
|
+
* Tests if a value is a valid email address.
|
|
101
|
+
* Undefined and null values are skipped and not validated.
|
|
102
|
+
*
|
|
103
|
+
* @param o the value to check
|
|
104
|
+
*/
|
|
105
|
+
function isEmail(o) {
|
|
106
|
+
const expression = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
|
|
107
|
+
return !(o !== undefined && o !== null && !expression.test(o));
|
|
108
|
+
}
|
|
109
|
+
exports.isEmail = isEmail;
|
|
58
110
|
/**
|
|
59
111
|
* Throws a ValidationError if the value provided is not an email.
|
|
60
112
|
* Undefined and null values are skipped and not validated.
|
|
@@ -62,13 +114,23 @@ exports.matches = matches;
|
|
|
62
114
|
* @param name the name of the variable
|
|
63
115
|
* @param o the value to check
|
|
64
116
|
*/
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
if (o !== undefined && o !== null && !expression.test(o)) {
|
|
117
|
+
function email(name, o) {
|
|
118
|
+
if (!isEmail(o)) {
|
|
68
119
|
throw new errors_1.ValidationError(`${name} is not a valid email address`);
|
|
69
120
|
}
|
|
70
121
|
}
|
|
71
|
-
exports.
|
|
122
|
+
exports.email = email;
|
|
123
|
+
/**
|
|
124
|
+
* Tests if a value has a length that is less than the provided length.
|
|
125
|
+
* Undefined and null values are skipped and not validated.
|
|
126
|
+
*
|
|
127
|
+
* @param length the maximum length of the variable
|
|
128
|
+
* @param o the value to check
|
|
129
|
+
*/
|
|
130
|
+
function isMaxLength(length, o) {
|
|
131
|
+
return !(o !== undefined && o !== null && o.length > length);
|
|
132
|
+
}
|
|
133
|
+
exports.isMaxLength = isMaxLength;
|
|
72
134
|
/**
|
|
73
135
|
* Throws a ValidationError if the value provided has a length that exceeds the provided length.
|
|
74
136
|
* Undefined and null values are skipped and not validated.
|
|
@@ -78,11 +140,22 @@ exports.isEmail = isEmail;
|
|
|
78
140
|
* @param o the value to check
|
|
79
141
|
*/
|
|
80
142
|
function maxLength(name, length, o) {
|
|
81
|
-
if (
|
|
143
|
+
if (!isMaxLength(length, o)) {
|
|
82
144
|
throw new errors_1.ValidationError(`length of ${name} may not exceed ${length}`);
|
|
83
145
|
}
|
|
84
146
|
}
|
|
85
147
|
exports.maxLength = maxLength;
|
|
148
|
+
/**
|
|
149
|
+
* Tests if a value has a length that is greater than the provided length.
|
|
150
|
+
* Undefined and null values are skipped and not validated.
|
|
151
|
+
*
|
|
152
|
+
* @param length the minimum length of the variable
|
|
153
|
+
* @param o the value to check
|
|
154
|
+
*/
|
|
155
|
+
function isMinLength(length, o) {
|
|
156
|
+
return !(o !== undefined && o !== null && o.length < length);
|
|
157
|
+
}
|
|
158
|
+
exports.isMinLength = isMinLength;
|
|
86
159
|
/**
|
|
87
160
|
* Throws a ValidationError if the value provided has a length that is less than the provided length.
|
|
88
161
|
* Undefined and null values are skipped and not validated.
|
|
@@ -92,11 +165,21 @@ exports.maxLength = maxLength;
|
|
|
92
165
|
* @param o the value to check
|
|
93
166
|
*/
|
|
94
167
|
function minLength(name, length, o) {
|
|
95
|
-
if (
|
|
168
|
+
if (!isMinLength(length, o)) {
|
|
96
169
|
throw new errors_1.ValidationError(`length of ${name} may not be less than ${length}`);
|
|
97
170
|
}
|
|
98
171
|
}
|
|
99
172
|
exports.minLength = minLength;
|
|
173
|
+
/**
|
|
174
|
+
* Tests if a value provided is a number.
|
|
175
|
+
* Undefined and null values are skipped and not validated.
|
|
176
|
+
*
|
|
177
|
+
* @param o the value to check
|
|
178
|
+
*/
|
|
179
|
+
function isNumber(o) {
|
|
180
|
+
return o === undefined || o === null || !isNaN(+o);
|
|
181
|
+
}
|
|
182
|
+
exports.isNumber = isNumber;
|
|
100
183
|
/**
|
|
101
184
|
* Throws a ValidationError if the value provided is not a number.
|
|
102
185
|
* Undefined and null values are skipped and not validated.
|
|
@@ -104,12 +187,23 @@ exports.minLength = minLength;
|
|
|
104
187
|
* @param name the name of the variable
|
|
105
188
|
* @param o the value to check
|
|
106
189
|
*/
|
|
107
|
-
function
|
|
108
|
-
if (
|
|
190
|
+
function number(name, o) {
|
|
191
|
+
if (!isNumber(o)) {
|
|
109
192
|
throw new errors_1.ValidationError(`${name} is not a number`);
|
|
110
193
|
}
|
|
111
194
|
}
|
|
112
|
-
exports.
|
|
195
|
+
exports.number = number;
|
|
196
|
+
/**
|
|
197
|
+
* Tests if a value is less than the provided minimum value.
|
|
198
|
+
* Undefined and null values are skipped and not validated.
|
|
199
|
+
*
|
|
200
|
+
* @param minValue the minimum value allowed
|
|
201
|
+
* @param o the value to check
|
|
202
|
+
*/
|
|
203
|
+
function isMinValue(minValue, o) {
|
|
204
|
+
return o === undefined || o === null || +o >= minValue;
|
|
205
|
+
}
|
|
206
|
+
exports.isMinValue = isMinValue;
|
|
113
207
|
/**
|
|
114
208
|
* Throws a ValidationError if the value is less than the provided minimum value.
|
|
115
209
|
* Undefined and null values are skipped and not validated.
|
|
@@ -119,16 +213,22 @@ exports.isNumber = isNumber;
|
|
|
119
213
|
* @param o the value to check
|
|
120
214
|
*/
|
|
121
215
|
function minValue(name, minValue, o) {
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
isNumber(name, o);
|
|
125
|
-
}
|
|
126
|
-
if (+o < minValue) {
|
|
127
|
-
throw new errors_1.ValidationError(`${name} may not be less than ${minValue}`);
|
|
128
|
-
}
|
|
216
|
+
if (!isMinValue(minValue, o)) {
|
|
217
|
+
throw new errors_1.ValidationError(`${name} may not be less than ${minValue}`);
|
|
129
218
|
}
|
|
130
219
|
}
|
|
131
220
|
exports.minValue = minValue;
|
|
221
|
+
/**
|
|
222
|
+
* Tests if a value is more than the provided maximum value.
|
|
223
|
+
* Undefined and null values are skipped and not validated.
|
|
224
|
+
*
|
|
225
|
+
* @param maxValue the maximum value allowed
|
|
226
|
+
* @param o the value to check
|
|
227
|
+
*/
|
|
228
|
+
function isMaxValue(maxValue, o) {
|
|
229
|
+
return !(o !== undefined && o !== null && +o > maxValue);
|
|
230
|
+
}
|
|
231
|
+
exports.isMaxValue = isMaxValue;
|
|
132
232
|
/**
|
|
133
233
|
* Throws a ValidationError if the value is more than the provided maximum value.
|
|
134
234
|
* Undefined and null values are skipped and not validated.
|
|
@@ -138,16 +238,23 @@ exports.minValue = minValue;
|
|
|
138
238
|
* @param o the value to check
|
|
139
239
|
*/
|
|
140
240
|
function maxValue(name, maxValue, o) {
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
isNumber(name, o);
|
|
144
|
-
}
|
|
145
|
-
if (+o > maxValue) {
|
|
146
|
-
throw new errors_1.ValidationError(`${name} may not be greater than ${maxValue}`);
|
|
147
|
-
}
|
|
241
|
+
if (!isMaxValue(maxValue, o)) {
|
|
242
|
+
throw new errors_1.ValidationError(`${name} may not be greater than ${maxValue}`);
|
|
148
243
|
}
|
|
149
244
|
}
|
|
150
245
|
exports.maxValue = maxValue;
|
|
246
|
+
/**
|
|
247
|
+
* Tests if the value is between the provided minimum and maximum values inclusive.
|
|
248
|
+
* Undefined and null values are skipped and not validated.
|
|
249
|
+
*
|
|
250
|
+
* @param minValue the minimum value allowed
|
|
251
|
+
* @param maxValue the maximum value allowed
|
|
252
|
+
* @param o the value to check
|
|
253
|
+
*/
|
|
254
|
+
function isBetweenValues(minValue, maxValue, o) {
|
|
255
|
+
return !(o !== undefined && o !== null && (+o < minValue || +o > maxValue));
|
|
256
|
+
}
|
|
257
|
+
exports.isBetweenValues = isBetweenValues;
|
|
151
258
|
/**
|
|
152
259
|
* Throws a ValidationError if the value is not between the provided minimum and maximum values inclusive.
|
|
153
260
|
* Undefined and null values are skipped and not validated.
|
|
@@ -158,48 +265,65 @@ exports.maxValue = maxValue;
|
|
|
158
265
|
* @param o the value to check
|
|
159
266
|
*/
|
|
160
267
|
function betweenValues(name, minValue, maxValue, o) {
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
isNumber(name, o);
|
|
164
|
-
}
|
|
165
|
-
if (+o < minValue || +o > maxValue) {
|
|
166
|
-
throw new errors_1.ValidationError(`${name} must be between ${minValue} and ${maxValue}`);
|
|
167
|
-
}
|
|
268
|
+
if (!isBetweenValues(minValue, maxValue, o)) {
|
|
269
|
+
throw new errors_1.ValidationError(`${name} must be between ${minValue} and ${maxValue}`);
|
|
168
270
|
}
|
|
169
271
|
}
|
|
170
272
|
exports.betweenValues = betweenValues;
|
|
171
273
|
const isString = (value) => typeof value === 'string';
|
|
172
|
-
function
|
|
274
|
+
function isValidString(options, value) {
|
|
173
275
|
if (options.required) {
|
|
174
|
-
|
|
276
|
+
if (!isNotNull(value)) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
175
279
|
}
|
|
176
280
|
else if (value === undefined || value === null) {
|
|
177
|
-
return;
|
|
281
|
+
return true;
|
|
178
282
|
}
|
|
179
283
|
if (!isString(value)) {
|
|
180
|
-
|
|
284
|
+
return false;
|
|
181
285
|
}
|
|
182
|
-
if (options.minLength
|
|
183
|
-
|
|
286
|
+
if (options.minLength && !isMinLength(options.minLength, value)) {
|
|
287
|
+
return false;
|
|
184
288
|
}
|
|
185
|
-
if (options.maxLength
|
|
186
|
-
|
|
289
|
+
if (options.maxLength && !isMaxLength(options.maxLength, value)) {
|
|
290
|
+
return false;
|
|
187
291
|
}
|
|
188
|
-
if (options.regex
|
|
189
|
-
|
|
292
|
+
if (options.regex && !isMatch(options.regex, value)) {
|
|
293
|
+
return false;
|
|
190
294
|
}
|
|
191
|
-
if (options.notBlank
|
|
192
|
-
|
|
295
|
+
if (options.notBlank && !isNotBlank(value)) {
|
|
296
|
+
return false;
|
|
193
297
|
}
|
|
194
|
-
if (options.notEmpty
|
|
195
|
-
|
|
298
|
+
if (options.notEmpty && !isNotEmpty(value)) {
|
|
299
|
+
return false;
|
|
196
300
|
}
|
|
197
|
-
if (options.
|
|
198
|
-
|
|
301
|
+
if (options.email && !isEmail(value)) {
|
|
302
|
+
return false;
|
|
199
303
|
}
|
|
200
|
-
if (options.
|
|
201
|
-
|
|
304
|
+
if (options.number && !isNumber(value)) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
exports.isValidString = isValidString;
|
|
310
|
+
function validString(name, options, value) {
|
|
311
|
+
if (options.required) {
|
|
312
|
+
notNull(name, value);
|
|
313
|
+
}
|
|
314
|
+
else if (value === undefined || value === null) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (!isString(value)) {
|
|
318
|
+
throw new errors_1.ValidationError(`${name} must be a string`);
|
|
202
319
|
}
|
|
320
|
+
options.minLength && minLength(name, options.minLength, value);
|
|
321
|
+
options.maxLength && maxLength(name, options.maxLength, value);
|
|
322
|
+
options.regex && match(name, options.regex, value);
|
|
323
|
+
options.notBlank && notBlank(name, value);
|
|
324
|
+
options.notEmpty && notEmpty(name, value);
|
|
325
|
+
options.email && email(name, value);
|
|
326
|
+
options.number && number(name, value);
|
|
203
327
|
}
|
|
204
|
-
exports.
|
|
328
|
+
exports.validString = validString;
|
|
205
329
|
//# sourceMappingURL=validators.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":";;;AAAA,sCAA0C;AAE1C;;;;;;GAMG;AACH,SAAgB,OAAO,CACrB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":";;;AAAA,sCAA0C;AAE1C;;;;GAIG;AACH,SAAgB,SAAS,CAAC,CAAW;IACnC,OAAO,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,CAAC;AACvC,CAAC;AAFD,8BAEC;AAED;;;;;;GAMG;AACH,SAAgB,OAAO,CACrB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE;QACjB,MAAM,IAAI,wBAAe,CAAC,GAAG,IAAI,+BAA+B,CAAC,CAAC;KACnE;AACH,CAAC;AAPD,0BAOC;AAED;;;;GAIG;AACH,SAAgB,UAAU,CAAC,CAAW;IACpC,OAAO,CAAC,CAAC,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;AACvE,CAAC;AAFD,gCAEC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CACtB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,UAAU,CAAC,CAAC,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