@nr1e/commons 0.0.2-alpha.2
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/.prettierrc.js +3 -0
- package/LICENSE +26 -0
- package/README.md +14 -0
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/index.js.map +1 -0
- package/jest.config.js +8 -0
- package/package.json +48 -0
- package/validator/index.d.ts +2 -0
- package/validator/index.js +19 -0
- package/validator/index.js.map +1 -0
- package/validator/validation-error.d.ts +19 -0
- package/validator/validation-error.js +28 -0
- package/validator/validation-error.js.map +1 -0
- package/validator/validators.d.ts +107 -0
- package/validator/validators.js +205 -0
- package/validator/validators.js.map +1 -0
package/.prettierrc.js
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright 2023 NR1E, Inc.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
7
|
+
list of conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
|
11
|
+
and/or other materials provided with the distribution.
|
|
12
|
+
|
|
13
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
14
|
+
may be used to endorse or promote products derived from this software without
|
|
15
|
+
specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
18
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
21
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
22
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
23
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
24
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
25
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Commons JS
|
|
2
|
+
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![TypeScript Style Guide][gts-image]][gts-url]
|
|
5
|
+
[![GitHub Actions][github-image]][github-url]
|
|
6
|
+
|
|
7
|
+
This project provides reusable components commonly needed in typescript projects.
|
|
8
|
+
|
|
9
|
+
[github-url]: https://github.com/nr1etech/commons-js/actions
|
|
10
|
+
[github-image]: https://github.com/nr1etech/commons-js/workflows/ci/badge.svg
|
|
11
|
+
[npm-url]: https://npmjs.org/package/@nr1e/commons-js
|
|
12
|
+
[npm-image]: https://img.shields.io/npm/v/@nre1/commons-js.svg
|
|
13
|
+
[gts-image]: https://img.shields.io/badge/code%20style-google-blueviolet.svg
|
|
14
|
+
[gts-url]: https://github.com/google/gts
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as validator from './validator';
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,2CAAyC"}
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nr1e/commons",
|
|
3
|
+
"description": "Provides common patterns for validation",
|
|
4
|
+
"version": "0.0.2-alpha.2",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"author": "NR1E, Inc.",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"license": "BSD-3-Clause",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/nr1etech/commons-js.git"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"/**/*.d.ts",
|
|
18
|
+
"/**/*.js",
|
|
19
|
+
"/**/*.js.map",
|
|
20
|
+
"!/**/*.test.*"
|
|
21
|
+
],
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/jest": "^29.5.10",
|
|
24
|
+
"@types/node": "20.8.2",
|
|
25
|
+
"gts": "^5.2.0",
|
|
26
|
+
"jest": "^29.7.0",
|
|
27
|
+
"ts-jest": "^29.1.1",
|
|
28
|
+
"typescript": "~5.1.6"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./index.js",
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
"./validator": "./validator/index.js"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"watch": "tsc -w",
|
|
38
|
+
"test": "jest",
|
|
39
|
+
"lint": "gts lint",
|
|
40
|
+
"clean": "gts clean",
|
|
41
|
+
"compile": "tsc",
|
|
42
|
+
"fix": "gts fix",
|
|
43
|
+
"pretest": "pnpm run compile",
|
|
44
|
+
"posttest": "pnpm run lint",
|
|
45
|
+
"check": "prettier --check .",
|
|
46
|
+
"makepretty": "prettier --write ."
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./validators"), exports);
|
|
18
|
+
__exportStar(require("./validation-error"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,qDAAmC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type PossibleValidationError = {
|
|
2
|
+
stack?: string;
|
|
3
|
+
message?: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
statusCode?: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the given variable is a ValidationError.
|
|
9
|
+
*
|
|
10
|
+
* @param e the variable to check
|
|
11
|
+
*/
|
|
12
|
+
export declare function isValidationError(e?: PossibleValidationError): e is ValidationError;
|
|
13
|
+
/**
|
|
14
|
+
* Thrown when a validation error occurs.
|
|
15
|
+
*/
|
|
16
|
+
export declare class ValidationError extends Error {
|
|
17
|
+
statusCode: number;
|
|
18
|
+
constructor(message?: string, statusCode?: number);
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationError = exports.isValidationError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the given variable is a ValidationError.
|
|
6
|
+
*
|
|
7
|
+
* @param e the variable to check
|
|
8
|
+
*/
|
|
9
|
+
function isValidationError(e) {
|
|
10
|
+
return !!(e &&
|
|
11
|
+
e.stack &&
|
|
12
|
+
e.message &&
|
|
13
|
+
e.statusCode &&
|
|
14
|
+
e.name === 'ValidationError');
|
|
15
|
+
}
|
|
16
|
+
exports.isValidationError = isValidationError;
|
|
17
|
+
/**
|
|
18
|
+
* Thrown when a validation error occurs.
|
|
19
|
+
*/
|
|
20
|
+
class ValidationError extends Error {
|
|
21
|
+
constructor(message, statusCode) {
|
|
22
|
+
super(message !== null && message !== void 0 ? message : 'Validation error');
|
|
23
|
+
this.name = 'ValidationError';
|
|
24
|
+
this.statusCode = statusCode !== null && statusCode !== void 0 ? statusCode : 400;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.ValidationError = ValidationError;
|
|
28
|
+
//# sourceMappingURL=validation-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-error.js","sourceRoot":"","sources":["validation-error.ts"],"names":[],"mappings":";;;AAOA;;;;GAIG;AACH,SAAgB,iBAAiB,CAC/B,CAA2B;IAE3B,OAAO,CAAC,CAAC,CACP,CAAC;QACD,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,IAAI,KAAK,iBAAiB,CAC7B,CAAC;AACJ,CAAC;AAVD,8CAUC;AAED;;GAEG;AACH,MAAa,eAAgB,SAAQ,KAAK;IAGxC,YAAY,OAAgB,EAAE,UAAmB;QAC/C,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,kBAAkB,CAAC,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,UAAU,GAAG,UAAU,aAAV,UAAU,cAAV,UAAU,GAAI,GAAG,CAAC;IACtC,CAAC;CACF;AARD,0CAQC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws a ValidationError if the value is null or undefined.
|
|
3
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
4
|
+
*
|
|
5
|
+
* @param name the name of the variable
|
|
6
|
+
* @param o the value to check
|
|
7
|
+
*/
|
|
8
|
+
export declare function notNull(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Throws a ValidationError if the value is null, undefined or the length is 0.
|
|
11
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
12
|
+
*
|
|
13
|
+
* @param name the name of the variable
|
|
14
|
+
* @param o the value to check
|
|
15
|
+
*/
|
|
16
|
+
export declare function notEmpty(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* Throws a ValidationError if the value is null, undefined, has a length of 0 or contains only whitespace.
|
|
19
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
20
|
+
*
|
|
21
|
+
* @param name the name of the variable
|
|
22
|
+
* @param o the value to check
|
|
23
|
+
*/
|
|
24
|
+
export declare function notBlank(name: string, o?: unknown): asserts o is NonNullable<unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Throws a ValidationError if the value does not match the regular expression provided.
|
|
27
|
+
* Undefined and null values are skipped and not validated.
|
|
28
|
+
*
|
|
29
|
+
* @param name the name of the variable
|
|
30
|
+
* @param regex the regular expression to validate with
|
|
31
|
+
* @param o the value to check
|
|
32
|
+
*/
|
|
33
|
+
export declare function matches(name: string, regex: RegExp, o?: string | null): void;
|
|
34
|
+
/**
|
|
35
|
+
* Throws a ValidationError if the value provided is not an email.
|
|
36
|
+
* Undefined and null values are skipped and not validated.
|
|
37
|
+
*
|
|
38
|
+
* @param name the name of the variable
|
|
39
|
+
* @param o the value to check
|
|
40
|
+
*/
|
|
41
|
+
export declare function isEmail(name: string, o?: string | null): void;
|
|
42
|
+
/**
|
|
43
|
+
* Throws a ValidationError if the value provided has a length that exceeds the provided length.
|
|
44
|
+
* Undefined and null values are skipped and not validated.
|
|
45
|
+
*
|
|
46
|
+
* @param name the name of the variable
|
|
47
|
+
* @param length the maximum length of the variable
|
|
48
|
+
* @param o the value to check
|
|
49
|
+
*/
|
|
50
|
+
export declare function maxLength(name: string, length: number, o?: string | unknown[] | null): void;
|
|
51
|
+
/**
|
|
52
|
+
* Throws a ValidationError if the value provided has a length that is less than the provided length.
|
|
53
|
+
* Undefined and null values are skipped and not validated.
|
|
54
|
+
*
|
|
55
|
+
* @param name the name of the variable
|
|
56
|
+
* @param length the minimum length of the variable
|
|
57
|
+
* @param o the value to check
|
|
58
|
+
*/
|
|
59
|
+
export declare function minLength(name: string, length: number, o?: string | unknown[] | null): void;
|
|
60
|
+
/**
|
|
61
|
+
* Throws a ValidationError if the value provided is not a number.
|
|
62
|
+
* Undefined and null values are skipped and not validated.
|
|
63
|
+
*
|
|
64
|
+
* @param name the name of the variable
|
|
65
|
+
* @param o the value to check
|
|
66
|
+
*/
|
|
67
|
+
export declare function isNumber(name: string, o?: string | null | number): void;
|
|
68
|
+
/**
|
|
69
|
+
* Throws a ValidationError if the value is less than the provided minimum value.
|
|
70
|
+
* Undefined and null values are skipped and not validated.
|
|
71
|
+
*
|
|
72
|
+
* @param name the name of the variable
|
|
73
|
+
* @param minValue the minimum value allowed
|
|
74
|
+
* @param o the value to check
|
|
75
|
+
*/
|
|
76
|
+
export declare function minValue(name: string, minValue: number, o?: number | string | null): void;
|
|
77
|
+
/**
|
|
78
|
+
* Throws a ValidationError if the value is more than the provided maximum value.
|
|
79
|
+
* Undefined and null values are skipped and not validated.
|
|
80
|
+
*
|
|
81
|
+
* @param name the name of the variable
|
|
82
|
+
* @param maxValue the maximum value allowed
|
|
83
|
+
* @param o the value to check
|
|
84
|
+
*/
|
|
85
|
+
export declare function maxValue(name: string, maxValue: number, o?: number | string | null): void;
|
|
86
|
+
/**
|
|
87
|
+
* Throws a ValidationError if the value is not between the provided minimum and maximum values inclusive.
|
|
88
|
+
* Undefined and null values are skipped and not validated.
|
|
89
|
+
*
|
|
90
|
+
* @param name the name of the variable
|
|
91
|
+
* @param minValue the minimum value allowed
|
|
92
|
+
* @param maxValue the maximum value allowed
|
|
93
|
+
* @param o the value to check
|
|
94
|
+
*/
|
|
95
|
+
export declare function betweenValues(name: string, minValue: number, maxValue: number, o?: string | number | null): void;
|
|
96
|
+
export interface StringValidationOptions {
|
|
97
|
+
readonly name: string;
|
|
98
|
+
readonly required: boolean;
|
|
99
|
+
readonly minLength?: number;
|
|
100
|
+
readonly maxLength?: number;
|
|
101
|
+
readonly regex?: RegExp;
|
|
102
|
+
readonly notBlank?: boolean;
|
|
103
|
+
readonly notEmpty?: boolean;
|
|
104
|
+
readonly isEmail?: boolean;
|
|
105
|
+
readonly isNumber?: boolean;
|
|
106
|
+
}
|
|
107
|
+
export declare function validateString(options: StringValidationOptions, value?: unknown): void;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateString = exports.betweenValues = exports.maxValue = exports.minValue = exports.isNumber = exports.minLength = exports.maxLength = exports.isEmail = exports.matches = exports.notBlank = exports.notEmpty = exports.notNull = void 0;
|
|
4
|
+
const validation_error_1 = require("./validation-error");
|
|
5
|
+
/**
|
|
6
|
+
* Throws a ValidationError if the value is null or undefined.
|
|
7
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
8
|
+
*
|
|
9
|
+
* @param name the name of the variable
|
|
10
|
+
* @param o the value to check
|
|
11
|
+
*/
|
|
12
|
+
function notNull(name, o) {
|
|
13
|
+
if (o === undefined || o === null) {
|
|
14
|
+
throw new validation_error_1.ValidationError(`${name} may not be null or undefined`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.notNull = notNull;
|
|
18
|
+
/**
|
|
19
|
+
* Throws a ValidationError if the value is null, undefined or the length is 0.
|
|
20
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
21
|
+
*
|
|
22
|
+
* @param name the name of the variable
|
|
23
|
+
* @param o the value to check
|
|
24
|
+
*/
|
|
25
|
+
function notEmpty(name, o) {
|
|
26
|
+
if (o === undefined || o === null || o.toString().length === 0) {
|
|
27
|
+
throw new validation_error_1.ValidationError(`${name} may not be empty`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.notEmpty = notEmpty;
|
|
31
|
+
/**
|
|
32
|
+
* Throws a ValidationError if the value is null, undefined, has a length of 0 or contains only whitespace.
|
|
33
|
+
* This function also asserts the value to be NonNullable if the check passes.
|
|
34
|
+
*
|
|
35
|
+
* @param name the name of the variable
|
|
36
|
+
* @param o the value to check
|
|
37
|
+
*/
|
|
38
|
+
function notBlank(name, o) {
|
|
39
|
+
if (o === undefined || o === null || o.toString().trim().length === 0) {
|
|
40
|
+
throw new validation_error_1.ValidationError(`${name} may not be blank`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.notBlank = notBlank;
|
|
44
|
+
/**
|
|
45
|
+
* Throws a ValidationError if the value does not match the regular expression provided.
|
|
46
|
+
* Undefined and null values are skipped and not validated.
|
|
47
|
+
*
|
|
48
|
+
* @param name the name of the variable
|
|
49
|
+
* @param regex the regular expression to validate with
|
|
50
|
+
* @param o the value to check
|
|
51
|
+
*/
|
|
52
|
+
function matches(name, regex, o) {
|
|
53
|
+
if (o !== undefined && o !== null && !o.match(regex)) {
|
|
54
|
+
throw new validation_error_1.ValidationError(`${name} must match ${regex}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.matches = matches;
|
|
58
|
+
/**
|
|
59
|
+
* Throws a ValidationError if the value provided is not an email.
|
|
60
|
+
* Undefined and null values are skipped and not validated.
|
|
61
|
+
*
|
|
62
|
+
* @param name the name of the variable
|
|
63
|
+
* @param o the value to check
|
|
64
|
+
*/
|
|
65
|
+
function isEmail(name, o) {
|
|
66
|
+
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])?)*$/;
|
|
67
|
+
if (o !== undefined && o !== null && !expression.test(o)) {
|
|
68
|
+
throw new validation_error_1.ValidationError(`${name} is not a valid email address`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.isEmail = isEmail;
|
|
72
|
+
/**
|
|
73
|
+
* Throws a ValidationError if the value provided has a length that exceeds the provided length.
|
|
74
|
+
* Undefined and null values are skipped and not validated.
|
|
75
|
+
*
|
|
76
|
+
* @param name the name of the variable
|
|
77
|
+
* @param length the maximum length of the variable
|
|
78
|
+
* @param o the value to check
|
|
79
|
+
*/
|
|
80
|
+
function maxLength(name, length, o) {
|
|
81
|
+
if (o !== undefined && o !== null && o.length > length) {
|
|
82
|
+
throw new validation_error_1.ValidationError(`length of ${name} may not exceed ${length}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.maxLength = maxLength;
|
|
86
|
+
/**
|
|
87
|
+
* Throws a ValidationError if the value provided has a length that is less than the provided length.
|
|
88
|
+
* Undefined and null values are skipped and not validated.
|
|
89
|
+
*
|
|
90
|
+
* @param name the name of the variable
|
|
91
|
+
* @param length the minimum length of the variable
|
|
92
|
+
* @param o the value to check
|
|
93
|
+
*/
|
|
94
|
+
function minLength(name, length, o) {
|
|
95
|
+
if (o !== undefined && o !== null && o.length < length) {
|
|
96
|
+
throw new validation_error_1.ValidationError(`length of ${name} may not be less than ${length}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.minLength = minLength;
|
|
100
|
+
/**
|
|
101
|
+
* Throws a ValidationError if the value provided is not a number.
|
|
102
|
+
* Undefined and null values are skipped and not validated.
|
|
103
|
+
*
|
|
104
|
+
* @param name the name of the variable
|
|
105
|
+
* @param o the value to check
|
|
106
|
+
*/
|
|
107
|
+
function isNumber(name, o) {
|
|
108
|
+
if (o !== undefined && o !== null && isNaN(Number(o))) {
|
|
109
|
+
throw new validation_error_1.ValidationError(`${name} is not a number`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.isNumber = isNumber;
|
|
113
|
+
/**
|
|
114
|
+
* Throws a ValidationError if the value is less than the provided minimum value.
|
|
115
|
+
* Undefined and null values are skipped and not validated.
|
|
116
|
+
*
|
|
117
|
+
* @param name the name of the variable
|
|
118
|
+
* @param minValue the minimum value allowed
|
|
119
|
+
* @param o the value to check
|
|
120
|
+
*/
|
|
121
|
+
function minValue(name, minValue, o) {
|
|
122
|
+
if (o !== undefined && o !== null) {
|
|
123
|
+
if (typeof o === 'string') {
|
|
124
|
+
isNumber(name, o);
|
|
125
|
+
}
|
|
126
|
+
if (+o < minValue) {
|
|
127
|
+
throw new validation_error_1.ValidationError(`${name} may not be less than ${minValue}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.minValue = minValue;
|
|
132
|
+
/**
|
|
133
|
+
* Throws a ValidationError if the value is more than the provided maximum value.
|
|
134
|
+
* Undefined and null values are skipped and not validated.
|
|
135
|
+
*
|
|
136
|
+
* @param name the name of the variable
|
|
137
|
+
* @param maxValue the maximum value allowed
|
|
138
|
+
* @param o the value to check
|
|
139
|
+
*/
|
|
140
|
+
function maxValue(name, maxValue, o) {
|
|
141
|
+
if (o !== undefined && o !== null) {
|
|
142
|
+
if (typeof o === 'string') {
|
|
143
|
+
isNumber(name, o);
|
|
144
|
+
}
|
|
145
|
+
if (+o > maxValue) {
|
|
146
|
+
throw new validation_error_1.ValidationError(`${name} may not be greater than ${maxValue}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.maxValue = maxValue;
|
|
151
|
+
/**
|
|
152
|
+
* Throws a ValidationError if the value is not between the provided minimum and maximum values inclusive.
|
|
153
|
+
* Undefined and null values are skipped and not validated.
|
|
154
|
+
*
|
|
155
|
+
* @param name the name of the variable
|
|
156
|
+
* @param minValue the minimum value allowed
|
|
157
|
+
* @param maxValue the maximum value allowed
|
|
158
|
+
* @param o the value to check
|
|
159
|
+
*/
|
|
160
|
+
function betweenValues(name, minValue, maxValue, o) {
|
|
161
|
+
if (o !== undefined && o !== null) {
|
|
162
|
+
if (typeof o === 'string') {
|
|
163
|
+
isNumber(name, o);
|
|
164
|
+
}
|
|
165
|
+
if (+o < minValue || +o > maxValue) {
|
|
166
|
+
throw new validation_error_1.ValidationError(`${name} must be between ${minValue} and ${maxValue}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.betweenValues = betweenValues;
|
|
171
|
+
const isString = (value) => typeof value === 'string';
|
|
172
|
+
function validateString(options, value) {
|
|
173
|
+
if (options.required) {
|
|
174
|
+
notNull(options.name, value);
|
|
175
|
+
}
|
|
176
|
+
else if (value === undefined || value === null) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (!isString(value)) {
|
|
180
|
+
throw new validation_error_1.ValidationError(`${options.name} must be a string`);
|
|
181
|
+
}
|
|
182
|
+
if (options.minLength !== undefined && value.length < options.minLength) {
|
|
183
|
+
throw new validation_error_1.ValidationError(`${options.name} must be at least ${options.minLength} characters`);
|
|
184
|
+
}
|
|
185
|
+
if (options.maxLength !== undefined && value.length > options.maxLength) {
|
|
186
|
+
throw new validation_error_1.ValidationError(`${options.name} must be at most ${options.maxLength} characters`);
|
|
187
|
+
}
|
|
188
|
+
if (options.regex !== undefined && !options.regex.test(value)) {
|
|
189
|
+
throw new validation_error_1.ValidationError(`${options.name} must match ${options.regex}`);
|
|
190
|
+
}
|
|
191
|
+
if (options.notBlank !== undefined && options.notBlank) {
|
|
192
|
+
notBlank(options.name, value);
|
|
193
|
+
}
|
|
194
|
+
if (options.notEmpty !== undefined && options.notEmpty) {
|
|
195
|
+
notEmpty(options.name, value);
|
|
196
|
+
}
|
|
197
|
+
if (options.isEmail !== undefined && options.isEmail) {
|
|
198
|
+
isEmail(options.name, value);
|
|
199
|
+
}
|
|
200
|
+
if (options.isNumber !== undefined && options.isNumber) {
|
|
201
|
+
isNumber(options.name, value);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.validateString = validateString;
|
|
205
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":";;;AAAA,yDAAmD;AAEnD;;;;;;GAMG;AACH,SAAgB,OAAO,CACrB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,EAAE;QACjC,MAAM,IAAI,kCAAe,CAAC,GAAG,IAAI,+BAA+B,CAAC,CAAC;KACnE;AACH,CAAC;AAPD,0BAOC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CACtB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE;QAC9D,MAAM,IAAI,kCAAe,CAAC,GAAG,IAAI,mBAAmB,CAAC,CAAC;KACvD;AACH,CAAC;AAPD,4BAOC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CACtB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,CA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