UNPKG

@decaf-ts/db-decorators

Version:

Agnostic database decorators and repository

48 lines (47 loc) 2.22 kB
import { Validator } 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 */ export declare class ReadOnlyValidator extends Validator { constructor(); /** * @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 */ hasErrors(value: any, ...args: any[]): string | 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: any, oldValue: any, message?: string): string | undefined; }