UNPKG

@decaf-ts/db-decorators

Version:

Agnostic database decorators and repository

85 lines 4.18 kB
"use strict"; 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.TimestampValidator = void 0; const decorator_validation_1 = require("@decaf-ts/decorator-validation"); const constants_1 = require("./../constants.cjs"); /** * @description A validator that ensures timestamp values are only updated with newer timestamps. * @summary Validates the update of a timestamp by comparing the new timestamp with the old one, ensuring the new timestamp is more recent. * @param {Date|string|number} value - The timestamp value to validate * @param {Date|string|number} oldValue - The previous timestamp to compare against * @param {string} [message] - Optional custom error message * @class TimestampValidator * @example * // Using TimestampValidator with a timestamp property * class Document { * @timestamp() * updatedAt: Date; * * title: string; * * constructor(title: string) { * this.title = title; * this.updatedAt = new Date(); * } * } * * // This will trigger validation error when trying to update with an older timestamp * const doc = new Document('My Document'); * const oldDate = new Date(2020, 0, 1); * doc.updatedAt = oldDate; // Will be prevented by TimestampValidator * @category Validators */ let TimestampValidator = class TimestampValidator extends decorator_validation_1.Validator { constructor() { super(constants_1.DEFAULT_ERROR_MESSAGES.TIMESTAMP.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 timestamp 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 Validates that a timestamp is newer than its previous value. * @summary Checks if a timestamp has been updated with a more recent value by converting both values to Date objects and comparing them. * @param {Date|string|number} value - The new timestamp value to validate * @param {Date|string|number} oldValue - The original timestamp to compare against * @param {string} [message] - Optional custom error message to override the default * @return {string | undefined} An error message if validation fails (new timestamp is not newer), undefined otherwise */ updateHasErrors(value, oldValue, message) { if (value === undefined) return; message = message || this.getMessage(message || this.message); try { value = new Date(value); oldValue = new Date(oldValue); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (e) { return message; } return value <= oldValue ? message : undefined; } }; exports.TimestampValidator = TimestampValidator; exports.TimestampValidator = TimestampValidator = __decorate([ (0, decorator_validation_1.validator)(constants_1.UpdateValidationKeys.TIMESTAMP), __metadata("design:paramtypes", []) ], TimestampValidator); //# sourceMappingURL=TimestampValidator.js.map