@nr1e/commons 0.0.2-alpha.10

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 ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ ...require('gts/.prettierrc.json'),
3
+ };
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
@@ -0,0 +1,120 @@
1
+ import { HttpStatusCode } from '../http';
2
+ interface IError {
3
+ stack?: string;
4
+ name?: string;
5
+ statusCode?: HttpStatusCode | number;
6
+ }
7
+ /**
8
+ * Checks if the given parameter is a NotFoundError.
9
+ *
10
+ * @param e the parameter to check
11
+ */
12
+ export declare function isNotFoundError(e?: IError | null): e is NotFoundError;
13
+ /**
14
+ * Thrown when a resource cannot be found.
15
+ */
16
+ export declare class NotFoundError extends Error {
17
+ readonly statusCode = HttpStatusCode.NOT_FOUND;
18
+ readonly body: string | undefined;
19
+ constructor(message?: string);
20
+ }
21
+ /**
22
+ * Checks if the given parameter is a ForbiddenError.
23
+ *
24
+ * @param e the parameter to check
25
+ */
26
+ export declare function isForbiddenError(e?: IError | null): e is ForbiddenError;
27
+ /**
28
+ * Thrown when a requested operations is not allowed.
29
+ */
30
+ export declare class ForbiddenError extends Error {
31
+ readonly statusCode = HttpStatusCode.FORBIDDEN;
32
+ readonly body: string | undefined;
33
+ constructor(message?: string);
34
+ }
35
+ /**
36
+ * Checks if the given variable is a ValidationError.
37
+ *
38
+ * @param e the variable to check
39
+ */
40
+ export declare function isValidationError(e?: IError | null): e is ValidationError;
41
+ /**
42
+ * Thrown when a validation error occurs.
43
+ */
44
+ export declare class ValidationError extends Error {
45
+ readonly statusCode = HttpStatusCode.BAD_REQUEST;
46
+ readonly body: string | undefined;
47
+ constructor(message?: string);
48
+ }
49
+ /**
50
+ * Checks if the given parameter is a BadRequestError.
51
+ *
52
+ * @param e the parameter to check
53
+ */
54
+ export declare function isBadRequestError(e?: IError | null): e is BadRequestError;
55
+ /**
56
+ * Thrown when a bad request is made.
57
+ */
58
+ export declare class BadRequestError extends Error {
59
+ readonly statusCode = HttpStatusCode.BAD_REQUEST;
60
+ readonly body: string | undefined;
61
+ constructor(message?: string);
62
+ }
63
+ /**
64
+ * Checks if the given parameter is a InternalServerError.
65
+ *
66
+ * @param e the parameter to check
67
+ */
68
+ export declare function isInternalServerError(e?: IError | null): e is InternalServerError;
69
+ /**
70
+ * Throws when an internal server error occurs.
71
+ */
72
+ export declare class InternalServerError extends Error {
73
+ readonly statusCode = HttpStatusCode.INTERNAL_SERVER_ERROR;
74
+ readonly body: string | undefined;
75
+ constructor(message?: string);
76
+ }
77
+ /**
78
+ * Checks if the given parameter is a ConflictError.
79
+ *
80
+ * @param e the parameter to check
81
+ */
82
+ export declare function isConflictError(e?: IError | null): e is ConflictError;
83
+ /**
84
+ * Thrown when a conflict occurs.
85
+ */
86
+ export declare class ConflictError extends Error {
87
+ readonly statusCode = HttpStatusCode.CONFLICT;
88
+ readonly body: string | undefined;
89
+ constructor(message?: string);
90
+ }
91
+ /**
92
+ * Checks if the given parameter is a UnsupportedMediaTypeError.
93
+ *
94
+ * @param e the parameter to check
95
+ */
96
+ export declare function isUnsupportedMediaTypeError(e?: IError | null): e is UnsupportedMediaTypeError;
97
+ /**
98
+ * Thrown when a unsupported media type is used.
99
+ */
100
+ export declare class UnsupportedMediaTypeError extends Error {
101
+ readonly statusCode = HttpStatusCode.UNSUPPORTED_MEDIA_TYPE;
102
+ readonly body: string | undefined;
103
+ constructor(message?: string);
104
+ }
105
+ /**
106
+ * Checks if the given parameter is a NotImplementedError.
107
+ *
108
+ * @param e the parameter to check
109
+ */
110
+ export declare function isNotImplementedError(e?: IError | null): e is NotImplementedError;
111
+ /**
112
+ * Thrown when a requested operation is not implemented.
113
+ */
114
+ export declare class NotImplementedError extends Error {
115
+ readonly statusCode = HttpStatusCode.NOT_IMPLEMENTED;
116
+ readonly body: string | undefined;
117
+ readonly expose = true;
118
+ constructor(message?: string);
119
+ }
120
+ export {};
@@ -0,0 +1,185 @@
1
+ "use strict";
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 = void 0;
4
+ const http_1 = require("../http");
5
+ /**
6
+ * Checks if the given parameter is a NotFoundError.
7
+ *
8
+ * @param e the parameter to check
9
+ */
10
+ function isNotFoundError(e) {
11
+ return !!(e &&
12
+ e.stack &&
13
+ e.statusCode &&
14
+ e.name &&
15
+ e.name === 'NotFoundError');
16
+ }
17
+ exports.isNotFoundError = isNotFoundError;
18
+ /**
19
+ * Thrown when a resource cannot be found.
20
+ */
21
+ class NotFoundError extends Error {
22
+ constructor(message) {
23
+ super(message !== null && message !== void 0 ? message : 'Not found');
24
+ this.statusCode = http_1.HttpStatusCode.NOT_FOUND;
25
+ this.body = message;
26
+ this.name = 'NotFoundError';
27
+ }
28
+ }
29
+ exports.NotFoundError = NotFoundError;
30
+ /**
31
+ * Checks if the given parameter is a ForbiddenError.
32
+ *
33
+ * @param e the parameter to check
34
+ */
35
+ function isForbiddenError(e) {
36
+ return !!(e &&
37
+ e.stack &&
38
+ e.statusCode &&
39
+ e.name &&
40
+ e.name === 'ForbiddenError');
41
+ }
42
+ exports.isForbiddenError = isForbiddenError;
43
+ /**
44
+ * Thrown when a requested operations is not allowed.
45
+ */
46
+ class ForbiddenError extends Error {
47
+ constructor(message) {
48
+ super(message !== null && message !== void 0 ? message : 'Forbidden');
49
+ this.statusCode = http_1.HttpStatusCode.FORBIDDEN;
50
+ this.body = message;
51
+ this.name = 'ForbiddenError';
52
+ }
53
+ }
54
+ exports.ForbiddenError = ForbiddenError;
55
+ /**
56
+ * Checks if the given variable is a ValidationError.
57
+ *
58
+ * @param e the variable to check
59
+ */
60
+ function isValidationError(e) {
61
+ return !!(e && e.stack && e.statusCode && e.name === 'ValidationError');
62
+ }
63
+ exports.isValidationError = isValidationError;
64
+ /**
65
+ * Thrown when a validation error occurs.
66
+ */
67
+ class ValidationError extends Error {
68
+ constructor(message) {
69
+ super(message !== null && message !== void 0 ? message : 'Validation error');
70
+ this.statusCode = http_1.HttpStatusCode.BAD_REQUEST;
71
+ this.body = message;
72
+ this.name = 'ValidationError';
73
+ }
74
+ }
75
+ exports.ValidationError = ValidationError;
76
+ /**
77
+ * Checks if the given parameter is a BadRequestError.
78
+ *
79
+ * @param e the parameter to check
80
+ */
81
+ function isBadRequestError(e) {
82
+ return !!(e && e.stack && e.statusCode && e.name === 'BadRequestError');
83
+ }
84
+ exports.isBadRequestError = isBadRequestError;
85
+ /**
86
+ * Thrown when a bad request is made.
87
+ */
88
+ class BadRequestError extends Error {
89
+ constructor(message) {
90
+ super(message !== null && message !== void 0 ? message : 'Bad request');
91
+ this.statusCode = http_1.HttpStatusCode.BAD_REQUEST;
92
+ this.body = message;
93
+ this.name = 'BadRequestError';
94
+ }
95
+ }
96
+ exports.BadRequestError = BadRequestError;
97
+ /**
98
+ * Checks if the given parameter is a InternalServerError.
99
+ *
100
+ * @param e the parameter to check
101
+ */
102
+ function isInternalServerError(e) {
103
+ return !!(e && e.stack && e.statusCode && e.name === 'InternalServerError');
104
+ }
105
+ exports.isInternalServerError = isInternalServerError;
106
+ /**
107
+ * Throws when an internal server error occurs.
108
+ */
109
+ class InternalServerError extends Error {
110
+ constructor(message) {
111
+ super(message !== null && message !== void 0 ? message : 'Internal server error');
112
+ this.statusCode = http_1.HttpStatusCode.INTERNAL_SERVER_ERROR;
113
+ this.body = message;
114
+ this.name = 'InternalServerError';
115
+ }
116
+ }
117
+ exports.InternalServerError = InternalServerError;
118
+ /**
119
+ * Checks if the given parameter is a ConflictError.
120
+ *
121
+ * @param e the parameter to check
122
+ */
123
+ function isConflictError(e) {
124
+ return !!(e && e.stack && e.statusCode && e.name === 'ConflictError');
125
+ }
126
+ exports.isConflictError = isConflictError;
127
+ /**
128
+ * Thrown when a conflict occurs.
129
+ */
130
+ class ConflictError extends Error {
131
+ constructor(message) {
132
+ super(message !== null && message !== void 0 ? message : 'Conflict');
133
+ this.statusCode = http_1.HttpStatusCode.CONFLICT;
134
+ this.body = message;
135
+ this.name = 'ConflictError';
136
+ }
137
+ }
138
+ exports.ConflictError = ConflictError;
139
+ /**
140
+ * Checks if the given parameter is a UnsupportedMediaTypeError.
141
+ *
142
+ * @param e the parameter to check
143
+ */
144
+ function isUnsupportedMediaTypeError(e) {
145
+ return !!(e &&
146
+ e.stack &&
147
+ e.statusCode &&
148
+ e.name === 'UnsupportedMediaTypeError');
149
+ }
150
+ exports.isUnsupportedMediaTypeError = isUnsupportedMediaTypeError;
151
+ /**
152
+ * Thrown when a unsupported media type is used.
153
+ */
154
+ class UnsupportedMediaTypeError extends Error {
155
+ constructor(message) {
156
+ super(message !== null && message !== void 0 ? message : 'Unsupported media type');
157
+ this.statusCode = http_1.HttpStatusCode.UNSUPPORTED_MEDIA_TYPE;
158
+ this.body = message;
159
+ this.name = 'UnsupportedMediaTypeError';
160
+ }
161
+ }
162
+ exports.UnsupportedMediaTypeError = UnsupportedMediaTypeError;
163
+ /**
164
+ * Checks if the given parameter is a NotImplementedError.
165
+ *
166
+ * @param e the parameter to check
167
+ */
168
+ function isNotImplementedError(e) {
169
+ return !!(e && e.stack && e.statusCode && e.name === 'NotImplementedError');
170
+ }
171
+ exports.isNotImplementedError = isNotImplementedError;
172
+ /**
173
+ * Thrown when a requested operation is not implemented.
174
+ */
175
+ class NotImplementedError extends Error {
176
+ constructor(message) {
177
+ super(message !== null && message !== void 0 ? message : 'Not implemented');
178
+ this.statusCode = http_1.HttpStatusCode.NOT_IMPLEMENTED;
179
+ this.expose = true;
180
+ this.body = message;
181
+ this.name = 'NotImplementedError';
182
+ }
183
+ }
184
+ exports.NotImplementedError = NotImplementedError;
185
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["errors.ts"],"names":[],"mappings":";;;AAAA,kCAAuC;AAQvC;;;;GAIG;AACH,SAAgB,eAAe,CAAC,CAAiB;IAC/C,OAAO,CAAC,CAAC,CACP,CAAC;QACD,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,IAAI,KAAK,eAAe,CAC3B,CAAC;AACJ,CAAC;AARD,0CAQC;AAED;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;IAGtC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,WAAW,CAAC,CAAC;QAHvB,eAAU,GAAG,qBAAc,CAAC,SAAS,CAAC;QAI7C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AARD,sCAQC;AAED;;;;GAIG;AACH,SAAgB,gBAAgB,CAAC,CAAiB;IAChD,OAAO,CAAC,CAAC,CACP,CAAC;QACD,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,IAAI,KAAK,gBAAgB,CAC5B,CAAC;AACJ,CAAC;AARD,4CAQC;AAED;;GAEG;AACH,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,WAAW,CAAC,CAAC;QAHvB,eAAU,GAAG,qBAA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O,aAAP,OAAO,cAAP,OAAO,GAAI,wBAAwB,CAAC,CAAC;QAHpC,eAAU,GAAG,qBAAc,CAAC,sBAAsB,CAAC;QAI1D,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF;AARD,8DAQC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CACnC,CAAiB;IAEjB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,KAAK,qBAAqB,CAAC,CAAC;AAC9E,CAAC;AAJD,sDAIC;AAED;;GAEG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IAI5C,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,iBAAiB,CAAC,CAAC;QAJ7B,eAAU,GAAG,qBAAc,CAAC,eAAe,CAAC;QAE5C,WAAM,GAAG,IAAI,CAAC;QAGrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AATD,kDASC"}
@@ -0,0 +1 @@
1
+ export * from './errors';
@@ -0,0 +1,18 @@
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("./errors"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
@@ -0,0 +1,9 @@
1
+ export declare enum HttpMethod {
2
+ GET = "GET",
3
+ POST = "POST",
4
+ PUT = "PUT",
5
+ PATCH = "PATCH",
6
+ DELETE = "DELETE",
7
+ HEAD = "HEAD",
8
+ OPTIONS = "OPTIONS"
9
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpMethod = void 0;
4
+ var HttpMethod;
5
+ (function (HttpMethod) {
6
+ HttpMethod["GET"] = "GET";
7
+ HttpMethod["POST"] = "POST";
8
+ HttpMethod["PUT"] = "PUT";
9
+ HttpMethod["PATCH"] = "PATCH";
10
+ HttpMethod["DELETE"] = "DELETE";
11
+ HttpMethod["HEAD"] = "HEAD";
12
+ HttpMethod["OPTIONS"] = "OPTIONS";
13
+ })(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
14
+ //# sourceMappingURL=http-method.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-method.js","sourceRoot":"","sources":["http-method.ts"],"names":[],"mappings":";;;AAAA,IAAY,UAQX;AARD,WAAY,UAAU;IACpB,yBAAW,CAAA;IACX,2BAAa,CAAA;IACb,yBAAW,CAAA;IACX,6BAAe,CAAA;IACf,+BAAiB,CAAA;IACjB,2BAAa,CAAA;IACb,iCAAmB,CAAA;AACrB,CAAC,EARW,UAAU,0BAAV,UAAU,QAQrB"}
@@ -0,0 +1,42 @@
1
+ export declare enum HttpStatusCode {
2
+ CONTINUE = 100,
3
+ SWITCHING_PROTOCOLS = 101,
4
+ OK = 200,
5
+ CREATED = 201,
6
+ ACCEPTED = 202,
7
+ NON_AUTHORITATIVE_INFORMATION = 203,
8
+ NO_CONTENT = 204,
9
+ RESET_CONTENT = 205,
10
+ PARTIAL_CONTENT = 206,
11
+ MULTIPLE_CHOICES = 300,
12
+ PERMANENT_REDIRECT = 301,
13
+ FOUND = 302,
14
+ SEE_OTHER = 303,
15
+ NOT_MODIFIED = 304,
16
+ USE_PROXY = 305,
17
+ TEMPORARY_REDIRECT = 307,
18
+ BAD_REQUEST = 400,
19
+ UNAUTHORIZED = 401,
20
+ PAYMENT_REQUIRED = 402,
21
+ FORBIDDEN = 403,
22
+ NOT_FOUND = 404,
23
+ METHOD_NOT_ALLOWED = 405,
24
+ NOT_ACCEPTABLE = 406,
25
+ PROXY_AUTHENTICATION_REQUIRED = 407,
26
+ REQUEST_TIMEOUT = 408,
27
+ CONFLICT = 409,
28
+ GONE = 410,
29
+ LENGTH_REQUIRED = 411,
30
+ PRECONDITION_FAILED = 412,
31
+ REQUEST_ENTITY_TOO_LARGE = 413,
32
+ REQUEST_URI_TOO_LONG = 414,
33
+ UNSUPPORTED_MEDIA_TYPE = 415,
34
+ RANGE_NOT_SATISFIABLE = 416,
35
+ EXPECTATION_FAILED = 417,
36
+ INTERNAL_SERVER_ERROR = 500,
37
+ NOT_IMPLEMENTED = 501,
38
+ BAD_GATEWAY = 502,
39
+ SERVICE_UNAVAILABLE = 503,
40
+ GATEWAY_TIMEOUT = 504,
41
+ HTTP_VERSION_NOT_SUPPORTED = 505
42
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpStatusCode = void 0;
4
+ var HttpStatusCode;
5
+ (function (HttpStatusCode) {
6
+ HttpStatusCode[HttpStatusCode["CONTINUE"] = 100] = "CONTINUE";
7
+ HttpStatusCode[HttpStatusCode["SWITCHING_PROTOCOLS"] = 101] = "SWITCHING_PROTOCOLS";
8
+ HttpStatusCode[HttpStatusCode["OK"] = 200] = "OK";
9
+ HttpStatusCode[HttpStatusCode["CREATED"] = 201] = "CREATED";
10
+ HttpStatusCode[HttpStatusCode["ACCEPTED"] = 202] = "ACCEPTED";
11
+ HttpStatusCode[HttpStatusCode["NON_AUTHORITATIVE_INFORMATION"] = 203] = "NON_AUTHORITATIVE_INFORMATION";
12
+ HttpStatusCode[HttpStatusCode["NO_CONTENT"] = 204] = "NO_CONTENT";
13
+ HttpStatusCode[HttpStatusCode["RESET_CONTENT"] = 205] = "RESET_CONTENT";
14
+ HttpStatusCode[HttpStatusCode["PARTIAL_CONTENT"] = 206] = "PARTIAL_CONTENT";
15
+ HttpStatusCode[HttpStatusCode["MULTIPLE_CHOICES"] = 300] = "MULTIPLE_CHOICES";
16
+ HttpStatusCode[HttpStatusCode["PERMANENT_REDIRECT"] = 301] = "PERMANENT_REDIRECT";
17
+ HttpStatusCode[HttpStatusCode["FOUND"] = 302] = "FOUND";
18
+ HttpStatusCode[HttpStatusCode["SEE_OTHER"] = 303] = "SEE_OTHER";
19
+ HttpStatusCode[HttpStatusCode["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
20
+ HttpStatusCode[HttpStatusCode["USE_PROXY"] = 305] = "USE_PROXY";
21
+ HttpStatusCode[HttpStatusCode["TEMPORARY_REDIRECT"] = 307] = "TEMPORARY_REDIRECT";
22
+ HttpStatusCode[HttpStatusCode["BAD_REQUEST"] = 400] = "BAD_REQUEST";
23
+ HttpStatusCode[HttpStatusCode["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
24
+ HttpStatusCode[HttpStatusCode["PAYMENT_REQUIRED"] = 402] = "PAYMENT_REQUIRED";
25
+ HttpStatusCode[HttpStatusCode["FORBIDDEN"] = 403] = "FORBIDDEN";
26
+ HttpStatusCode[HttpStatusCode["NOT_FOUND"] = 404] = "NOT_FOUND";
27
+ HttpStatusCode[HttpStatusCode["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
28
+ HttpStatusCode[HttpStatusCode["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
29
+ HttpStatusCode[HttpStatusCode["PROXY_AUTHENTICATION_REQUIRED"] = 407] = "PROXY_AUTHENTICATION_REQUIRED";
30
+ HttpStatusCode[HttpStatusCode["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
31
+ HttpStatusCode[HttpStatusCode["CONFLICT"] = 409] = "CONFLICT";
32
+ HttpStatusCode[HttpStatusCode["GONE"] = 410] = "GONE";
33
+ HttpStatusCode[HttpStatusCode["LENGTH_REQUIRED"] = 411] = "LENGTH_REQUIRED";
34
+ HttpStatusCode[HttpStatusCode["PRECONDITION_FAILED"] = 412] = "PRECONDITION_FAILED";
35
+ HttpStatusCode[HttpStatusCode["REQUEST_ENTITY_TOO_LARGE"] = 413] = "REQUEST_ENTITY_TOO_LARGE";
36
+ HttpStatusCode[HttpStatusCode["REQUEST_URI_TOO_LONG"] = 414] = "REQUEST_URI_TOO_LONG";
37
+ HttpStatusCode[HttpStatusCode["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
38
+ HttpStatusCode[HttpStatusCode["RANGE_NOT_SATISFIABLE"] = 416] = "RANGE_NOT_SATISFIABLE";
39
+ HttpStatusCode[HttpStatusCode["EXPECTATION_FAILED"] = 417] = "EXPECTATION_FAILED";
40
+ HttpStatusCode[HttpStatusCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
41
+ HttpStatusCode[HttpStatusCode["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
42
+ HttpStatusCode[HttpStatusCode["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
43
+ HttpStatusCode[HttpStatusCode["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
44
+ HttpStatusCode[HttpStatusCode["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
45
+ HttpStatusCode[HttpStatusCode["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
46
+ })(HttpStatusCode || (exports.HttpStatusCode = HttpStatusCode = {}));
47
+ //# sourceMappingURL=http-status-code.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-status-code.js","sourceRoot":"","sources":["http-status-code.ts"],"names":[],"mappings":";;;AAAA,IAAY,cAyCX;AAzCD,WAAY,cAAc;IACxB,6DAAc,CAAA;IACd,mFAAyB,CAAA;IACzB,iDAAQ,CAAA;IACR,2DAAa,CAAA;IACb,6DAAc,CAAA;IACd,uGAAmC,CAAA;IACnC,iEAAgB,CAAA;IAChB,uEAAmB,CAAA;IACnB,2EAAqB,CAAA;IACrB,6EAAsB,CAAA;IACtB,iFAAwB,CAAA;IACxB,uDAAW,CAAA;IACX,+DAAe,CAAA;IACf,qEAAkB,CAAA;IAClB,+DAAe,CAAA;IACf,iFAAwB,CAAA;IACxB,mEAAiB,CAAA;IACjB,qEAAkB,CAAA;IAClB,6EAAsB,CAAA;IACtB,+DAAe,CAAA;IACf,+DAAe,CAAA;IACf,iFAAwB,CAAA;IACxB,yEAAoB,CAAA;IACpB,uGAAmC,CAAA;IACnC,2EAAqB,CAAA;IACrB,6DAAc,CAAA;IACd,qDAAU,CAAA;IACV,2EAAqB,CAAA;IACrB,mFAAyB,CAAA;IACzB,6FAA8B,CAAA;IAC9B,qFAA0B,CAAA;IAC1B,yFAA4B,CAAA;IAC5B,uFAA2B,CAAA;IAC3B,iFAAwB,CAAA;IACxB,uFAA2B,CAAA;IAC3B,2EAAqB,CAAA;IACrB,mEAAiB,CAAA;IACjB,mFAAyB,CAAA;IACzB,2EAAqB,CAAA;IACrB,iGAAgC,CAAA;AAClC,CAAC,EAzCW,cAAc,8BAAd,cAAc,QAyCzB"}
@@ -0,0 +1,2 @@
1
+ export * from './http-status-code';
2
+ export * from './http-method';
package/http/index.js ADDED
@@ -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("./http-status-code"), exports);
18
+ __exportStar(require("./http-method"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,gDAA8B"}
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * as validator from './validator';
2
+ export * as errors from './errors';
3
+ export * as http from './http';
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.http = exports.errors = exports.validator = void 0;
4
+ exports.validator = require("./validator");
5
+ exports.errors = require("./errors");
6
+ exports.http = require("./http");
7
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,2CAAyC;AACzC,qCAAmC;AACnC,iCAA+B"}
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ roots: ['<rootDir>'],
4
+ testMatch: ['**/*.test.ts'],
5
+ transform: {
6
+ '^.+\\.tsx?$': 'ts-jest',
7
+ },
8
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@nr1e/commons",
3
+ "description": "Provides common patterns for validation",
4
+ "version": "0.0.2-alpha.10",
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
+ "./http": "./http/index.js",
34
+ "./errors": "./errors/index.js",
35
+ "./validator": "./validator/index.js"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc",
39
+ "watch": "tsc -w",
40
+ "test": "jest",
41
+ "lint": "gts lint",
42
+ "clean": "gts clean",
43
+ "compile": "tsc",
44
+ "fix": "gts fix",
45
+ "pretest": "pnpm run compile",
46
+ "posttest": "pnpm run lint",
47
+ "check": "prettier --check .",
48
+ "makepretty": "prettier --write ."
49
+ }
50
+ }
@@ -0,0 +1 @@
1
+ export * from './validators';
@@ -0,0 +1,18 @@
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
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B"}
@@ -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 errors_1 = require("../errors");
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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_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 errors_1.ValidationError(`${options.name} must be a string`);
181
+ }
182
+ if (options.minLength !== undefined && value.length < options.minLength) {
183
+ throw new errors_1.ValidationError(`${options.name} must be at least ${options.minLength} characters`);
184
+ }
185
+ if (options.maxLength !== undefined && value.length > options.maxLength) {
186
+ throw new errors_1.ValidationError(`${options.name} must be at most ${options.maxLength} characters`);
187
+ }
188
+ if (options.regex !== undefined && !options.regex.test(value)) {
189
+ throw new errors_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,sCAA0C;AAE1C;;;;;;GAMG;AACH,SAAgB,OAAO,CACrB,IAAY,EACZ,CAAW;IAEX,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,EAAE;QACjC,MAAM,IAAI,wBAAe,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,wBAAe,CAAC,GAAG,IAAI,mBAAmB,CAAC,CAA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