UNPKG

@decaf-ts/db-decorators

Version:

Agnostic database decorators and repository

75 lines 3.62 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { validator, Validator } from "@decaf-ts/decorator-validation"; import { DEFAULT_ERROR_MESSAGES, UpdateValidationKeys } from "./../constants.js"; import { isEqual } from "@decaf-ts/decorator-validation"; /** * @description A validator that ensures properties marked as readonly cannot be modified during updates. * @summary Validator for the {@link readonly} decorator that checks if a value has been changed during an update operation. It compares the new value with the old value and returns an error message if they are not equal. * @param {any} value - The value to be validated * @param {any} oldValue - The previous value to compare against * @param {string} [message] - Optional custom error message * @class ReadOnlyValidator * @example * // Using ReadOnlyValidator with a readonly property * class User { * @readonly() * id: string; * * name: string; * * constructor(id: string, name: string) { * this.id = id; * this.name = name; * } * } * * // This will trigger validation error when trying to update * const user = new User('123', 'John'); * user.id = '456'; // Will be prevented by ReadOnlyValidator * @category Validators */ let ReadOnlyValidator = class ReadOnlyValidator extends Validator { constructor() { super(DEFAULT_ERROR_MESSAGES.READONLY.INVALID); } /** * @description Implementation of the base validator's hasErrors method. * @summary This method is required by the Validator interface but not used in this validator as validation only happens during updates. * @param {any} value - The value to validate * @param {any[]} args - Additional arguments * @return {string | undefined} Always returns undefined as this validator only works during updates */ // eslint-disable-next-line @typescript-eslint/no-unused-vars hasErrors(value, ...args) { return undefined; } /** * @description Checks if a value has been modified during an update operation. * @summary Validates a value has not changed by comparing it with the previous value using deep equality. * @param {any} value - The new value to validate * @param {any} oldValue - The original value to compare against * @param {string} [message] - Optional custom error message to override the default * @return {string | undefined} An error message if validation fails, undefined otherwise */ updateHasErrors(value, oldValue, message) { if (value === undefined) return; return isEqual(value, oldValue) ? undefined : this.getMessage(message || this.message); } }; ReadOnlyValidator = __decorate([ validator(UpdateValidationKeys.READONLY), __metadata("design:paramtypes", []) ], ReadOnlyValidator); export { ReadOnlyValidator }; //# sourceMappingURL=ReadOnlyValidator.js.map