UNPKG

rich-domain

Version:

This package provide utils file and interfaces to assistant build a complex application with domain driving design

151 lines 5.95 kB
import { CreateManyDomain, _EntityGettersAndSetters, IParentName, Settings } from "../types"; import { Utils } from "../utils/util"; import { Validator } from "../utils/validator"; /** * @description GettersAndSetters provides a foundational mechanism for retrieving and updating properties * of domain objects (like Entities or Value Objects). It enables property validation and gives control * over whether getters or setters are active. When integrated with Entities or Value Objects, this class * ensures that changes to domain properties follow defined validation rules. */ export declare class GettersAndSetters<Props> implements _EntityGettersAndSetters<Props> { protected props: Props; protected validator: Validator; protected static validator: Validator; protected util: Utils; protected static util: Utils; protected parentName: IParentName; protected config: Settings; constructor(props: Props, parentName: IParentName, config?: Settings); /** * @description Creates multiple domain instances at once. * * @param data An array of options that includes class constructors and properties. * @returns An object containing: * - `result`: A combined result indicating overall success or failure. * - `data`: An iterator of Result objects, each for an attempted instance creation. * * @example * ```typescript * const { result, data } = ValueObject.createMany([ * Class<AgeProps>(Age, props), * Class<NameProps>(Name, props), * Class<PriceProps>(Price, props) * ]); * * if (result.isOk()) { * const ageResult = data.next().value; * const nameResult = data.next().value; * const priceResult = data.next().value; * * console.log(ageResult.value().get('value')); // e.g. 21 * } * ``` */ static createMany(data: CreateManyDomain): import("../types").CreateManyResult; /** * @description Validates the value before setting or changing a property. * Subclasses can override this method to implement custom validation rules. * * @param _value The property value to validate. * @param _key The property key. * @returns `true` if the value is considered valid; `false` otherwise. * * @example * ```typescript * interface Props { * value: string; * age: number; * } * * class StringVo extends ValueObject<Props> { * private constructor(props: Props) { super(props) } * * validation<Key extends keyof Props>(value: Props[Key], key: Key): boolean { * const options = { * value: (val: string) => val.length < 15, * age: (val: number) => val > 0 * } * return options[key](value); * } * * public static create(props: Props): IResult<ValueObject<Props>, string> { * return Result.Ok(new StringVo(props)); * } * } * ``` */ validation(_value: any, _key?: any): boolean; validation(_value: any, _key: any): boolean; /** * @description Sets a property value after validating it. Returns a chained method * (`to`) that accepts the new value and an optional validation function. * * @param key The property key to set. * @returns An object with a `to` function to finalize the property assignment. * * @example * ```typescript * entity.set('age').to(30, (age) => age > 0); * ``` */ set<Key extends keyof Props>(key: Key): { /** * @description Assigns the provided value to the specified property if it passes both * the optional provided validation function and the class's `validation` method. * * @param value The value to assign to the property. * @param validation An optional validation function that returns `true` if the value is valid. * @returns `true` if the value was successfully assigned, otherwise throws an error. * * @example * ```typescript * entity.set('name').to('Alice', (value) => value.length > 0); * ``` */ to: (value: Props[Key], validation?: (value: Props[Key]) => boolean) => boolean; }; /** * @description Changes the value of a specified property directly (without the chained approach). * Validates the value using both the provided validation function (if any) and the class's `validation` method. * * @param key The property key to change. * @param value The new value for the property. * @param validation An optional validation function that returns `true` if the value is valid. * @returns `true` if the value was successfully changed, otherwise throws an error. * * @example * ```typescript * entity.change('age', 25, (age) => age > 0); * ``` */ change<Key extends keyof Props>(key: Key, value: Props[Key], validation?: (value: Props[Key]) => boolean): boolean; /** * @description Retrieves the value of a specified property. * * @param key The property key to retrieve. * @returns The property's value as a read-only value. * * @throws Will throw an error if getters are disabled. * * @example * ```typescript * const age = entity.get('age'); * console.log(age); // e.g. 30 * ``` */ get<Key extends keyof Props>(key: Key): Readonly<Props[Key]>; /** * @description Returns the raw (immutable) properties object of the domain instance. * * @returns A frozen, read-only view of the properties. * * @example * ```typescript * const raw = entity.getRaw(); * console.log(raw); // returns a frozen object with all properties * ``` */ getRaw(): Readonly<Props>; } export default GettersAndSetters; //# sourceMappingURL=entity-getters-and-setters.d.ts.map