@react-input-validator/rules
Version:
The validation rule objects used by the packages: `@react-input-validator/core`, `@react-input-validator/native` and `@react-input-validator/web`
69 lines • 2.67 kB
TypeScript
export type Nullable<T> = T | null | undefined;
export type LangFunction = (s: string) => string;
export type MessageFunction<T, V> = Nullable<(rule: IRule<T, V>) => Nullable<string>>;
export type ValidateParam = {
inputValues?: Rule['inputValues'];
name?: string | null;
resultValue?: any;
};
export type ValidateFunction<T> = (value: T, param: ValidateParam) => boolean | string;
export type ValidateFunctionAsync<T> = (value: T, resolve: (result: boolean | string) => void, param: ValidateParam) => void;
export type ComparableType = number | string | bigint | Date;
export type LengthType = {
length: number;
} | number;
export type HttpReqOption = {
data?: URLSearchParams | {
[prop: string]: unknown;
};
headers?: {
[name: string]: string;
};
silentOnFailure?: boolean;
timeout?: number;
withCredentials?: boolean;
};
/**
* We need to use this interface (`IRule`) to replace `Rule` class if used as a type.
* Sometimes, Typescript exposes private fields (their names are prefixed by '#').
* The example below raises a typing error:
* ```
* import type Rule from '@react-input-validator/rules/Rule';
* import {email} from '@react-input-validator/rules';
*
* let rule: Rule<any, any> = email;
* ```
* The error message says that `email` doesn't have private fields like `#arrayAsSingle` but `rule` has.
*/
export interface IRule<V = any, R = any> extends Pick<Rule<V, R>, keyof Rule<V, R>> {
}
export type Rules<V = any, R = any> = [IRule<V, R>, ...IRule<V, R>[]] | IRule<V, R>;
export declare const str: (template: Nullable<string>, params: any) => Nullable<string>;
export declare function isFilled(value: unknown): boolean;
export default class Rule<V = any, R = any> implements IRule<V, R> {
#private;
static get defaultLang(): LangFunction;
static get defaultArrayAsSingle(): boolean;
static set defaultArrayAsSingle(value: boolean);
constructor();
inputValues?: {
[p: string]: any;
};
isValid: boolean;
lang: LangFunction;
name: Nullable<string>;
get errorMessage(): Nullable<string>;
get isArrayAsSingle(): boolean;
get messageFunc(): MessageFunction<V, R>;
get priority(): number;
get resultValue(): R;
get value(): V;
arrayAsSingle(isSingle?: boolean): IRule<V, R>;
setErrorMessage(message: string): IRule<V, R>;
setMessageFunc(func: MessageFunction<V, R>): IRule<V, R>;
setName(name: string): IRule<V, R>;
setPriority(priority: number): IRule<V, R>;
setValue(value: V): IRule<V, R>;
validate(): IRule<V, R> | Promise<IRule<V, R>>;
}
//# sourceMappingURL=Rule.d.ts.map