1 | "use strict";
|
2 | var ParseUUIDPipe_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.ParseUUIDPipe = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 | const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
|
7 | const optional_decorator_1 = require("../decorators/core/optional.decorator");
|
8 | const http_status_enum_1 = require("../enums/http-status.enum");
|
9 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
10 | const shared_utils_1 = require("../utils/shared.utils");
|
11 | let ParseUUIDPipe = ParseUUIDPipe_1 = class ParseUUIDPipe {
|
12 | constructor(options) {
|
13 | options = options || {};
|
14 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST, version, } = options;
|
15 | this.version = version;
|
16 | this.exceptionFactory =
|
17 | exceptionFactory ||
|
18 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
19 | }
|
20 | async transform(value, metadata) {
|
21 | if (!this.isUUID(value, this.version)) {
|
22 | throw this.exceptionFactory(`Validation failed (uuid${this.version ? ` v ${this.version}` : ''} is expected)`);
|
23 | }
|
24 | return value;
|
25 | }
|
26 | isUUID(str, version = 'all') {
|
27 | if (!(0, shared_utils_1.isString)(str)) {
|
28 | throw this.exceptionFactory('The value passed as UUID is not a string');
|
29 | }
|
30 | const pattern = ParseUUIDPipe_1.uuidRegExps[version];
|
31 | return pattern === null || pattern === void 0 ? void 0 : pattern.test(str);
|
32 | }
|
33 | };
|
34 | ParseUUIDPipe.uuidRegExps = {
|
35 | 3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
|
36 | 4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
|
37 | 5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
|
38 | all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
|
39 | };
|
40 | ParseUUIDPipe = ParseUUIDPipe_1 = tslib_1.__decorate([
|
41 | (0, injectable_decorator_1.Injectable)(),
|
42 | tslib_1.__param(0, (0, optional_decorator_1.Optional)()),
|
43 | tslib_1.__metadata("design:paramtypes", [Object])
|
44 | ], ParseUUIDPipe);
|
45 | exports.ParseUUIDPipe = ParseUUIDPipe;
|