1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isEnumMetadata = exports.isEnumDefined = exports.isEnumArray = exports.addEnumSchema = exports.addEnumArraySchema = exports.getEnumType = exports.getEnumValues = void 0;
|
4 | const lodash_1 = require("lodash");
|
5 | function getEnumValues(enumType) {
|
6 | if (Array.isArray(enumType)) {
|
7 | return enumType;
|
8 | }
|
9 | if (typeof enumType !== 'object') {
|
10 | return [];
|
11 | }
|
12 | const values = [];
|
13 | const uniqueValues = {};
|
14 | for (const key in enumType) {
|
15 | const value = enumType[key];
|
16 | if (!uniqueValues.hasOwnProperty(value) &&
|
17 | !uniqueValues.hasOwnProperty(key)) {
|
18 | values.push(value);
|
19 | uniqueValues[value] = value;
|
20 | }
|
21 | }
|
22 | return values;
|
23 | }
|
24 | exports.getEnumValues = getEnumValues;
|
25 | function getEnumType(values) {
|
26 | const hasString = values.filter(lodash_1.isString).length > 0;
|
27 | return hasString ? 'string' : 'number';
|
28 | }
|
29 | exports.getEnumType = getEnumType;
|
30 | function addEnumArraySchema(paramDefinition, decoratorOptions) {
|
31 | const paramSchema = paramDefinition.schema || {};
|
32 | paramDefinition.schema = paramSchema;
|
33 | paramSchema.type = 'array';
|
34 | delete paramDefinition.isArray;
|
35 | const enumValues = getEnumValues(decoratorOptions.enum);
|
36 | paramSchema.items = {
|
37 | type: getEnumType(enumValues),
|
38 | enum: enumValues
|
39 | };
|
40 | if (decoratorOptions.enumName) {
|
41 | paramDefinition.enumName = decoratorOptions.enumName;
|
42 | }
|
43 | }
|
44 | exports.addEnumArraySchema = addEnumArraySchema;
|
45 | function addEnumSchema(paramDefinition, decoratorOptions) {
|
46 | const paramSchema = paramDefinition.schema || {};
|
47 | const enumValues = getEnumValues(decoratorOptions.enum);
|
48 | paramDefinition.schema = paramSchema;
|
49 | paramSchema.enum = enumValues;
|
50 | paramSchema.type = getEnumType(enumValues);
|
51 | if (decoratorOptions.enumName) {
|
52 | paramDefinition.enumName = decoratorOptions.enumName;
|
53 | }
|
54 | }
|
55 | exports.addEnumSchema = addEnumSchema;
|
56 | const isEnumArray = (obj) => obj.isArray && obj.enum;
|
57 | exports.isEnumArray = isEnumArray;
|
58 | const isEnumDefined = (obj) => obj.enum;
|
59 | exports.isEnumDefined = isEnumDefined;
|
60 | const isEnumMetadata = (metadata) => { var _a; return metadata.enum || (metadata.isArray && ((_a = metadata.items) === null || _a === void 0 ? void 0 : _a['enum'])); };
|
61 | exports.isEnumMetadata = isEnumMetadata;
|