@decaf-ts/db-decorators
Version:
Agnostic database decorators and repository
78 lines • 3.84 kB
JavaScript
;
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadOnlyValidator = void 0;
const decorator_validation_1 = require("@decaf-ts/decorator-validation");
const constants_1 = require("./../constants.cjs");
const reflection_1 = require("@decaf-ts/reflection");
/**
* @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 decorator_validation_1.Validator {
constructor() {
super(constants_1.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 (0, reflection_1.isEqual)(value, oldValue)
? undefined
: this.getMessage(message || this.message);
}
};
exports.ReadOnlyValidator = ReadOnlyValidator;
exports.ReadOnlyValidator = ReadOnlyValidator = __decorate([
(0, decorator_validation_1.validator)(constants_1.UpdateValidationKeys.READONLY),
__metadata("design:paramtypes", [])
], ReadOnlyValidator);
//# sourceMappingURL=ReadOnlyValidator.js.map