validator-is
Version:
**validator-is: Simplify Your Validation and Swagger Documentation**
33 lines (32 loc) • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Is = Is;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const class_validator_1 = require("class-validator");
function Is(type, required, description) {
return (0, common_1.applyDecorators)(required ? (0, class_validator_1.IsNotEmpty)() : (0, class_validator_1.IsOptional)(), getValidatorByType(type), (0, swagger_1.ApiProperty)({
description,
required,
}));
}
function getValidatorByType(type) {
if (type === 'number') {
return (0, class_validator_1.IsNumber)();
}
else if (type === 'string') {
return (0, class_validator_1.IsString)();
}
else if (type === 'boolean') {
return (0, class_validator_1.IsBoolean)();
}
else if (type === 'numberString') {
return (0, class_validator_1.IsNumberString)();
}
else if (type === 'array') {
return (0, class_validator_1.IsArray)();
}
else {
throw new Error(`Type ${type} is not supported`);
}
}