UNPKG

@darlean/valueobjects

Version:

Library for DDD-like value objects that can be validated, serialized and represented in other programming languages as native objects with native naming.

47 lines (46 loc) 2.85 kB
import { ICanonical, ICanonicalSource } from '@darlean/canonical'; import { Class, IFromCanonicalOptions, IValueOptions, Value, ValueClass } from './base'; import { NoInfer } from './utils'; type AnyFieldName = string; type MappingMap<TElem extends Value & ICanonicalSource> = Map<AnyFieldName, TElem>; type MappingDict<TElem extends Value & ICanonicalSource> = { [key: AnyFieldName]: TElem; }; export declare class MappingValue<TElem extends Value & ICanonicalSource> extends Value implements ICanonicalSource { private _slots?; private _canonical?; static required<T extends typeof MappingValue>(this: T): InstanceType<T>; static optional<T extends typeof MappingValue>(this: T): InstanceType<T> | undefined; /** * Creates a new mapping value from a map of values. */ static from<T extends MappingValue<TElem2>, TElem2 extends Value & ICanonicalSource = T extends MappingValue<infer X> ? X : never>(this: Class<T>, value: MappingDict<NoInfer<TElem2>> | MappingMap<NoInfer<TElem2>>): T; static fromCanonical<T extends MappingValue<TElem2>, TElem2 extends Value & ICanonicalSource = T extends MappingValue<infer X> ? X : never>(this: Class<T>, value: ICanonical): T; constructor(options: IValueOptions); _peekCanonicalRepresentation(): ICanonical<this>; equals(other: unknown): boolean; keys(): IterableIterator<AnyFieldName>; values(): IterableIterator<TElem>; entries(): IterableIterator<[AnyFieldName, TElem]>; /** * Extracts the current slots and their values. */ _extractSlots(): MappingMap<TElem>; get size(): number; get _logicalTypes(): string[]; protected _deriveCanonicalRepresentation(): ICanonical; get(slot: string): TElem | undefined; /** * Extracts the current elements. After that, the sequence value should not be * used anymore and throws errors when you try to access values. */ extractElements(): MappingMap<TElem>; protected _fromCanonical(canonical: ICanonical, options?: IFromCanonicalOptions): MappingMap<TElem>; protected _toCanonical(value: MappingMap<Value & ICanonicalSource>, logicalTypes: string[]): ICanonical<this>; protected _validate(v: MappingMap<Value & ICanonicalSource>, fail: (msg: string) => void): (Value & ICanonicalSource)[] | void; private _checkSlots; } export declare function ensureMappingDefForConstructor<TElem extends Value>(constructor: Function, elemClass: Class<TElem> | undefined): void; export declare function mappingvalidation<T extends Value & ICanonicalSource>(validator: (value: MappingMap<T>) => string | boolean | void, description?: string): (constructor: Function) => void; export declare function mappingvalue(elemClass: ValueClass, logicalName?: string): (constructor: Function) => void; export {};