UNPKG

container.ts

Version:
61 lines (60 loc) 2.68 kB
import { ErrorChain } from "../error"; /** Field error codes. */ export declare enum EFieldError { AndFieldError = 0, OrFieldError = 1, NotFieldError = 2, OptionalFieldError = 3 } /** Field error chain class. */ export declare class FieldError extends ErrorChain { constructor(code: EFieldError, value?: any, cause?: Error); } /** Field abstract base class. */ export declare abstract class Field<T, C = object> { /** Validate method takes string input and returns typed output. */ abstract validate(value?: string | object, context?: C): T | null; /** Format method takes typed input and returns string output. */ format(value: T, context?: C): string | object | null; /** Return an optional instance of this field. */ optional(defaultValue?: T): OptionalField<T, C>; /** And operator chaining. */ and(...fields: Field<T, C>[]): Field<T, C>; /** Or operator chaining. */ or(...fields: Field<T, C>[]): Field<T, C>; /** Not operator chaining. */ not(...fields: Field<T, C>[]): Field<T, C>; } /** Field operator abstract base class. */ export declare abstract class OperatorField<T, C = object> extends Field<T, C> { /** Chained fields for use by operator methods. */ protected readonly fields: Field<T, C>[]; constructor(...fields: Field<T, C>[]); } /** * Optional field wrapper, if value is defined uses field in validation/formatting. * If value is undefined default or null value is returned. */ export declare class OptionalField<T, C = object> extends Field<T, C> { protected readonly field: Field<T, C>; protected readonly defaultValue?: T | undefined; protected readonly formatDefault: string | object | null; constructor(field: Field<T, C>, defaultValue?: T | undefined, context?: any); validate(value?: string, context?: C): T | null; format(value?: T, context?: C): string | object | null; } /** And field operator, all input fields used in validation/formatting. */ export declare class AndField<T, C = object> extends OperatorField<T, C> { validate(value: string, context?: C): T; format(value: T, context?: C): string | object; } /** Or field operator, at least one input field used in validation/formatting. */ export declare class OrField<T, C = object> extends OperatorField<T, C> { validate(value: string, context?: C): T; format(value: T, context?: C): string | object; } /** Not field operator, all input fields expected to throw error/fail in validation/formatting. */ export declare class NotField<T, C = object> extends OperatorField<T, C> { validate(value: string, context?: C): null; format(value: T, context?: C): null; }