rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
72 lines • 3.86 kB
TypeScript
import { Adapter, AutoMapperSerializer, _Adapter, _Result, _ValueObject, _VoSettings } from "../types";
import { ReadonlyDeep } from "../types-util";
import AutoMapper from "./auto-mapper";
import BaseGettersAndSetters from "./base-getters-and-setters";
/**
* @description A `ValueObject` represents a domain object characterized by its properties rather than a unique identifier.
* Commonly used in domain-driven design, value objects are immutable and should be structurally equal
* (two value objects with the same properties are considered equal).
* This class provides functionalities to:
* - Validate properties
* - Compare equality between value objects
* - Convert the value object into a plain object representation
* - Clone the value object
*/
export declare class ValueObject<Props> extends BaseGettersAndSetters<Props> implements _ValueObject<Props> {
protected autoMapper: AutoMapper<Props>;
/**
* @description Initializes a new ValueObject instance.
* @param props Properties that define the ValueObject.
* @param config Optional configuration settings for getter/setter behavior.
*/
constructor(props: Props, config?: _VoSettings);
/**
* @description Determines if the current value object is equal to another value object of the same type.
* Equality is defined by comparing properties, excluding `createdAt` and `updatedAt`.
* Primitive values (strings, numbers, booleans), dates, arrays, and IDs are compared by value.
* Complex object structures are compared by their JSON-serialized form (excluding `createdAt` and `updatedAt`).
*
* @param other The value object to compare against.
* @returns `true` if all considered properties are equal; `false` otherwise.
*/
isEqual(other: this): boolean;
/**
* @description Creates a new instance of the value object, optionally overriding properties.
* Deep cloning is performed for object-based props, ensuring immutability of value objects.
*
* @param props Optional partial properties to override in the cloned instance.
* @returns A new ValueObject instance with updated properties.
*/
clone(props?: Props extends object ? Partial<Props> : never): this;
/**
* @description Converts the value object into a plain object or a format defined by the given adapter.
* If no adapter is provided, the object is serialized using an `AutoMapper` and frozen to ensure immutability.
*
* @param adapter Optional adapter to transform the value object into a custom format.
* @returns A deeply frozen, plain object representation of the value object properties.
*/
toObject<T>(adapter?: Adapter<this, T> | _Adapter<this, T>): T extends {} ? T : ReadonlyDeep<AutoMapperSerializer<Props>>;
/**
* @description Checks if a given value is considered valid for creating a ValueObject instance.
* Subclasses can override this to apply additional validation logic.
* @param value The value to validate.
* @returns `true` if valid; `false` otherwise.
*/
static isValid(value: any): boolean;
/**
* @description Validates the provided properties before creating a new value object instance.
* @param props The properties to validate.
* @returns `true` if the properties are valid; `false` otherwise.
*/
static isValidProps(props: any): boolean;
/**
* @description Intended to initialize a new value object instance with a given value.
* This method should be implemented by subclasses as needed.
* @param value The initial value or properties.
* @throws An error indicating the method is not implemented.
*/
static init(value: any): any;
static create(props: any): _Result<any, any, any>;
}
export default ValueObject;
//# sourceMappingURL=value-object.d.ts.map