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.

40 lines (39 loc) 2.12 kB
import { CanonicalLike, CanonicalLogicalTypes, ICanonical } from '@darlean/canonical'; import { CanonicalFieldName } from './valueobject'; export declare const VALIDATORS = "validators"; export declare const LOGICAL_TYPES = "logical=types"; export type Class<T> = { new (...args: any[]): T; name: string; }; export type ValidatorFunc<T = unknown> = (value: T, fail: (msg: string) => void) => void; export declare function required<T>(clazz: Class<T>): T; export declare function optional<T>(clazz: Class<T>): T | undefined; export type MethodKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]; export declare function valueobject(logicalType?: string): (constructor: Function) => void; export declare function validation<T>(validator: (value: T) => string | boolean | void, description?: string): (constructor: Function) => void; export interface IValueOptions { cacheCanonical?: boolean; value?: unknown; canonical?: CanonicalLike; } export interface IFromCanonicalOptions { cacheCanonical?: boolean; } export declare class Value { constructor(_options: IValueOptions); get _logicalTypes(): CanonicalLogicalTypes; equals(_other: unknown): boolean; static get logicalTypes(): CanonicalLogicalTypes; } export declare function constructValue<T>(clazz: Class<T>, options: IValueOptions): T; export type ValueClass<TValue extends Value = Value> = Class<TValue> & { get logicalTypes(): CanonicalLogicalTypes; }; export type ValueClassLike<TValue extends Value = Value> = ValueClass<TValue> | (() => ValueClass<TValue>); export declare function toValueClass<T extends ValueClassLike<TValue>, TValue extends Value = T extends ValueClassLike<infer X> ? X : never>(v: T): ValueClass<TValue>; export declare function aExtendsB(a: CanonicalFieldName[], b: CanonicalFieldName[]): boolean; export declare function shouldCacheCanonical(canonical: ICanonical, expectedTypes: CanonicalLogicalTypes, cacheCanonical: boolean | undefined): boolean; export declare function checkLogicalTypes(proto: Object): CanonicalLogicalTypes;