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
40 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomPasswordDecorator = void 0;
const class_validator_1 = require("class-validator");
const commonDecoratorFunctions_1 = require("../utils/commonDecoratorFunctions");
function CustomPasswordDecorator(details) {
const lengthFn = (customKey) => (0, class_validator_1.MinLength)(8, {
message: `${customKey}: must have at least 8 characters`,
});
const lowerCaseFn = (customKey) => (0, class_validator_1.Matches)(/(?=.*[a-z])/, {
message: `${customKey}: must contain at least one lowercase alphabet`,
});
const upperCaseFn = (customKey) => (0, class_validator_1.Matches)(/(?=.*[A-Z])/, {
message: `${customKey}: must contain at least one uppercase alphabet`,
});
const oneNumberFn = (customKey) => (0, class_validator_1.Matches)(/(?=.*[0-9])/, {
message: `${customKey}: must contain at least one number`,
});
const oneSpecialCharacterFn = (customKey) => (0, class_validator_1.Matches)(/\W|_/, {
message: `${customKey}: must contain at least one special character`,
});
const { description, defaultValue } = details;
const _swaggerProp = (0, commonDecoratorFunctions_1.swaggerProp)({
description,
defaultValue,
type: 'string',
});
return function (target, key) {
lengthFn(key)(target, key);
lowerCaseFn(key)(target, key);
upperCaseFn(key)(target, key);
oneNumberFn(key)(target, key);
oneSpecialCharacterFn(key)(target, key);
(0, commonDecoratorFunctions_1.isStringFn)(key)(target, key);
(0, commonDecoratorFunctions_1.notEmptyFn)(key)(target, key);
_swaggerProp(target, key);
};
}
exports.CustomPasswordDecorator = CustomPasswordDecorator;
//# sourceMappingURL=password.decorator.js.map