Module
simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, and Deno
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447export = adjusterexport as namespace adjuster
declare namespace adjuster{ /** * adjust multiple variables (as object) * @param data data to be adjusted * @param constraints adjuster objects * @param [onError] error handler * @returns adjusted data */ function adjust(data: Input, constraints: Constraints, onError?: ErrorHandler): CollectionObject
function boolean(): BooleanAdjuster function number(): NumberAdjuster function string(): StringAdjuster function numericString(): NumericStringAdjuster function email(): EmailAdjuster function array(): ArrayAdjuster function object(): ObjectAdjuster
const CAUSE: ConstantsCause; const STRING: ConstantsStringOptions; const NUMERIC_STRING: ConstantsNumericStringOptions;}
// interface definitionsinterface AdjusterError extends Error{ name: string cause: string value: any keyStack: Key[]}
interface AdjusterBase<T>{ /** * do adjust * @param value to be checked * @param [onError=null] callback function on error * @returns adjusted value */ adjust(value: Input, onError?: ErrorHandler<T>): T}
interface BooleanAdjuster extends AdjusterBase<boolean>{ /** * enable strict type check * @returns chainable instance */ strict(): this
/** * accept all numbers as boolean * @returns chainable instance */ acceptAllNumbers(): this
/** * set default value * @param value default value * @returns chainable instance */ default(value: boolean): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: boolean | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: boolean | null): this}
interface NumberAdjuster extends AdjusterBase<number>{ /** * enable strict type check * @returns chainable instance */ strict(): this
/** * set default value * @param value default value * @returns chainable instance */ default(value: number): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: number | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: number | null): this
/** * accept all special formats; i.e., "1e+10", "0x100" * @returns chainable instance */ acceptSpecialFormats(): this
/** * limit value to integer chain * @param [adjust=false] adjust to integer value is not an integer; default is ERROR * @returns chainable instance */ integer(adjust?: boolean): this
/** * accept only specified values * @param values values to be accepted * @returns chainable instance */ only(...values: number[]): this
/** * set min-value * @param value min-value * @param [adjust=false] adjust to min-value if value < min-value; default is ERROR * @returns chainable instance */ minValue(value: number, adjust?: boolean): this
/** * set max-value * @param value max-value * @param [adjust=false] adjust to max-value if value > max-value; default is ERROR * @returns chainable instance */ maxValue(value: number, adjust?: boolean): this}
interface StringAdjuster extends AdjusterBase<string>{ /** * enable strict type check * @returns chainable instance */ strict(): this
/** * set default value * @param value default value * @returns chainable instance */ default(value: string): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: string | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: string | null): this
/** * remove whitespace from both ends * @returns chainable instance */ trim(): this
/** * accept only specified values * @param values values to be accepted * @returns chainable instance */ only(...values: string[]): this
/** * set min-length * @param length min-length; error if shorter * @returns chainable instance */ minLength(length: number): this
/** * set max-length * @param length max-length; error if longer * @param [adjust=false] truncate if longer; default is ERROR * @returns chainable instance */ maxLength(length: number, adjust?: boolean): this
/** * specify acceptable pattern by regular expression * @param pattern acceptable pattern(regular expression) * @returns chainable instance */ pattern(pattern: RegExp): this}
interface NumericStringAdjuster extends AdjusterBase<string>{ /** * set default value * @param value default value * @returns chainable instance */ default(value: string): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: string | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: string | null): this
/** * join array into string * @returns chainable instance */ joinArray(): this
/** * ignore separator * @param separator separator * @returns chainable instance */ separatedBy(separator: Separator): this
/** * set min-length * @param length min-length; error if shorter * @returns chainable instance */ minLength(length: number): this
/** * set max-length * @param length max-length; error if longer * @param [adjust=false] truncate if longer; default is ERROR * @returns chainable instance */ maxLength(length: number, adjust?: boolean): this
/** * validate by checksum * @param algorithm checksum algorithm * @returns chainable instance */ checksum(algorithm: string): this}
interface EmailAdjuster extends AdjusterBase<string>{ /** * set default value * @param value default value * @returns chainable instance */ default(value: string): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: string | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: string | null): this
/** * remove whitespace from both ends * @returns chainable instance */ trim(): this
/** * specify acceptable pattern by regular expression * @param pattern acceptable pattern(regular expression) * @returns chainable instance */ pattern(pattern: RegExp): this}
interface ArrayAdjuster extends AdjusterBase<CollectionArray>{ /** * set default value; enable to omit * @param value default value * @returns chainable instance */ default(value: CollectionArray): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: CollectionArray | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: CollectionArray | null): this
/** * accept string and set separator * @param separator separator * @returns chainable instance */ separatedBy(separator: Separator): this
/** * convert to array, if not * @returns chainable instance */ toArray(): this
/** * set min-length of array elements * @param length min-length; error if shorter * @returns chainable instance */ minLength(length: number): this
/** * set max-length of array elements * @param length max-length * @param [adjust=false] truncate if longer; default is ERROR * @returns chainable instance */ maxLength(length: number, adjust?: boolean): this
/** * apply constraints for each elements * @param adjusterInstance adjuster to apply * @param [ignoreEachErrors=false] ignore errors of each elements * @returns chainable instance */ each(adjusterInstance: AdjusterBase<any>, ignoreEachErrors?: boolean): this}
interface ObjectAdjuster extends AdjusterBase<CollectionObject>{ /** * set default value; enable to omit * @param value default value * @returns chainable instance */ default(value: CollectionObject): this
/** * accept null * @param [value=null] value on null * @returns chainable instance */ acceptNull(value?: CollectionObject | null): this
/** * accept empty string * @param [value=null] value on empty * @returns chainable instance */ acceptEmptyString(value?: CollectionObject | null): this
/** * apply constraints * @param constraints constraints to apply * @returns chainable instance */ constraints(constraints: Constraints): this}
// type definitionstype CollectionArray = any[]type CollectionObject = { [name: string]: any }type Constraints = { [name: string]: AdjusterBase<any> }type Input = null | boolean | number | string | CollectionArray | CollectionObjecttype ErrorHandler<T = any> = (err: AdjusterError | null) => T | voidtype Key = string | numbertype Separator = RegExp | string
type ConstantsCause = { TYPE: string, REQUIRED: string, NULL: string, EMPTY: string, ONLY: string,
MIN_VALUE: string, MAX_VALUE: string,
MIN_LENGTH: string, MAX_LENGTH: string, PATTERN: string,
CHECKSUM: string,}type ConstantsStringOptions = { PATTERN: { EMAIL: RegExp, HTTP: RegExp, IPV4: RegExp, IPV6: RegExp, URI: RegExp, },}type ConstantsNumericStringOptions = { CHECKSUM_ALGORITHM: { LUHN: string, CREDIT_CARD: string,
MODULUS10_WEIGHT3_1: string, ISBN13: string, EAN: string, JAN: string, },}