decoval
Version:
DecoVal is a decorator-driven validation pattern that allows you to add validation rules directly to the properties of TypeScript classes, simplifying data control and increasing code readability and reusability.
27 lines (26 loc) • 1.24 kB
JavaScript
import { ValidationError } from "./error/validation.error";
import { registerValidation } from "./register.validation";
export function DvBoolean(options = {}) {
const { message, optional = false, empty = false, null: allowNull = false } = options;
if (optional && allowNull) {
throw new Error("The 'optional' and 'null' options cannot be used together.");
}
return function (target, propertyKey) {
const alreadyApplied = Reflect.getMetadata("valboolean:applied", target, propertyKey);
if (alreadyApplied) {
throw new Error(`The @DvBoolean decorator has already been applied to the field"${propertyKey}"`);
}
Reflect.defineMetadata("valboolean:applied", true, target, propertyKey);
registerValidation(target, propertyKey, (value) => {
if (value === undefined && optional)
return;
if (value === null && allowNull)
return;
if (value === "" && empty)
return;
if (typeof value !== "boolean") {
throw new ValidationError(typeof message === "string" ? message : `The "${propertyKey}" field must be a boolean value.`);
}
});
};
}