nestjs-custom-class-validators
Version:
This package contains a few custom validator I have found to be repetitive, So I made templates that handles both class-validator checks and Swagger configuration
33 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomMediaValidator = void 0;
const class_validator_1 = require("class-validator");
const nestjs_form_data_1 = require("nestjs-form-data");
const commonDecoratorFunctions_1 = require("../utils/commonDecoratorFunctions");
const common_1 = require("@nestjs/common");
function CustomMediaValidator(details) {
const { maxSizeInByte, mimetypes, isArray, optional, defaultValue, description, } = details;
const mySwaggerProp = (0, commonDecoratorFunctions_1.swaggerProp)({
optional,
description,
defaultValue,
isArray,
format: "binary",
type: "media",
});
return function (target, key) {
if (optional) {
(0, class_validator_1.IsOptional)()(target, key);
}
if (mimetypes.length == 0) {
throw new common_1.UnprocessableEntityException(`${key}: Please provide at least one mimetype`);
}
(0, nestjs_form_data_1.IsFile)(Object.assign(Object.assign({}, (isArray && { each: true })), { message: `${key}: Must be a file` }))(target, key);
(0, commonDecoratorFunctions_1.notEmptyFn)(key, isArray)(target, key);
(0, nestjs_form_data_1.MaxFileSize)(maxSizeInByte, Object.assign(Object.assign({}, (isArray && { each: true })), { message: `${key}: cannot exceed ${(maxSizeInByte) / 1e6} megabytes` }))(target, key);
(0, nestjs_form_data_1.HasMimeType)(mimetypes, Object.assign(Object.assign({}, (isArray && { each: true })), { message: `${key}: must have mimetype ${mimetypes.join(" or ")}` }))(target, key);
mySwaggerProp(target, key);
};
}
exports.CustomMediaValidator = CustomMediaValidator;
//# sourceMappingURL=media.decorator.js.map