nestjs-swagger-dto
Version:
Nestjs swagger dto decorators
43 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compose = exports.noop = void 0;
const swagger_1 = require("@nestjs/swagger");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const ForceArrayTransform = (0, class_transformer_1.Transform)(({ value }) => {
if (value === undefined) {
return value;
}
return Array.isArray(value) ? value : [value];
});
const noop = () => undefined;
exports.noop = noop;
const compose = (apiPropertyOptions, { isArray, nullable, example, optional, description, deprecated, default: def, name, }, ...decorators) => {
const arrayProps = typeof isArray === 'object' ? isArray : {};
const length = arrayProps.length;
const minLength = length ?? arrayProps.minLength;
const maxLength = length ?? arrayProps.maxLength;
const forceArray = arrayProps.force ?? false;
return applyDecorators(...decorators, (0, class_transformer_1.Expose)({ name }), optional ? (0, class_validator_1.ValidateIf)((_, v) => v !== undefined) : exports.noop, nullable ? (0, class_validator_1.ValidateIf)((_, v) => v !== null) : exports.noop, !!isArray ? (0, class_validator_1.IsArray)() : exports.noop, minLength ? (0, class_validator_1.ArrayMinSize)(minLength) : exports.noop, maxLength ? (0, class_validator_1.ArrayMaxSize)(maxLength) : exports.noop, forceArray ? ForceArrayTransform : exports.noop, def !== undefined ? (0, class_transformer_1.Transform)(({ value }) => (value === undefined ? def : value)) : exports.noop, (0, swagger_1.ApiProperty)({
...apiPropertyOptions,
minItems: minLength,
maxItems: maxLength,
...(nullable && { nullable }),
isArray: !!isArray,
name,
description,
deprecated,
example,
default: def,
required: !optional,
}));
};
exports.compose = compose;
function applyDecorators(...decorators) {
return (target, propertyKey) => {
for (const decorator of decorators) {
decorator(target, propertyKey);
}
};
}
//# sourceMappingURL=core.js.map