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 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | let ParseUUIDPipe = ParseUUIDPipe_1 = class ParseUUIDPipe {
|
19 | constructor(options) {
|
20 | this.options = options;
|
21 | options = options || {};
|
22 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST, version, } = options;
|
23 | this.version = version;
|
24 | this.exceptionFactory =
|
25 | exceptionFactory ||
|
26 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
27 | }
|
28 | async transform(value, metadata) {
|
29 | if ((0, shared_utils_1.isNil)(value) && this.options?.optional) {
|
30 | return value;
|
31 | }
|
32 | if (!this.isUUID(value, this.version)) {
|
33 | throw this.exceptionFactory(`Validation failed (uuid${this.version ? ` v ${this.version}` : ''} is expected)`);
|
34 | }
|
35 | return value;
|
36 | }
|
37 | isUUID(str, version = 'all') {
|
38 | if (!(0, shared_utils_1.isString)(str)) {
|
39 | throw this.exceptionFactory('The value passed as UUID is not a string');
|
40 | }
|
41 | const pattern = ParseUUIDPipe_1.uuidRegExps[version];
|
42 | return pattern?.test(str);
|
43 | }
|
44 | };
|
45 | exports.ParseUUIDPipe = ParseUUIDPipe;
|
46 | ParseUUIDPipe.uuidRegExps = {
|
47 | 3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
|
48 | 4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
|
49 | 5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
|
50 | 7: /^[0-9A-F]{8}-[0-9A-F]{4}-7[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
|
51 | all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
|
52 | };
|
53 | exports.ParseUUIDPipe = ParseUUIDPipe = ParseUUIDPipe_1 = tslib_1.__decorate([
|
54 | (0, injectable_decorator_1.Injectable)(),
|
55 | tslib_1.__param(0, (0, optional_decorator_1.Optional)()),
|
56 | tslib_1.__metadata("design:paramtypes", [Object])
|
57 | ], ParseUUIDPipe);
|