validx
Version:
Validation library for MobX
197 lines (196 loc) • 5 kB
TypeScript
/**
* A validatable object where all keys are strings.
*/
export declare type Validatable = Record<string, unknown>;
/**
* Options passed to validators.
*
* @export
* @interface IValidatorOptions
* @template T The type of the object being validated.
* @template R The rule type.
*/
export interface IValidatorOptions<T> {
field: string;
value: any;
obj: T;
}
/**
* Definition of a validator function.
*
* @export
* @interface IValidator
* @template T Type of the object being validated.
* @template R The rule type.
*/
export interface IValidator<T> {
(opts: IValidatorOptions<T>): boolean | string;
}
/**
* The interface all rules must satisfy.
*
* @export
* @interface IRule
* @template T
*/
export interface IRule {
msg?: string;
}
/**
* Maps fields on an object to one or more rules.
*
* @export
* @interface IValidationSchema
* @template T The object type.
*/
export declare type IValidationSchema<T> = {
[P in keyof T]?: Array<IValidator<T>>;
};
/**
* Validation errors are stored as a map of fields to error strings.
*
* @export
* @interface IValidationErrors
*/
export interface IValidationErrors {
[key: string]: Array<string>;
}
/**
* The validation context interface.
*
* @export
* @interface IValidationContext
*/
export interface IValidationContext {
errors: IValidationErrors;
isValid: boolean;
reset(): this;
validate<T>(obj: T, schema: IValidationSchema<T>): this;
addErrors(errors: IValidationErrors | {
[key: string]: string[];
}): this;
getErrors(field: string): string[];
getError(field: string): string | undefined;
clearErrors(field: string): this;
}
/**
* Validation context with the object already bound to the validate function.
*/
export interface IBoundValidationContext<T> {
errors: IValidationErrors;
isValid: boolean;
reset(): this;
validate(schema: IValidationSchema<T>): this;
addErrors(errors: IValidationErrors | {
[key: string]: string[];
}): this;
getErrors(field: keyof T): string[];
getError(field: keyof T): string | undefined;
clearErrors(field: string): this;
}
/**
* Validation context with the object already bound to the validate function.
*/
export interface ISchemaBoundValidationContext<T> extends IBoundValidationContext<T> {
validate(): this;
}
/**
* Implementation of the validation context.
*
* @export
* @class ValidationContext
* @implements {IValidationContext}
*/
export declare class ValidationContext implements IValidationContext {
/**
* All validation errors are stored here. To clear, call `reset`.
*
* @readonly
* @type {IValidationErrors}
* @memberOf ValidationContext
*/
readonly errors: IValidationErrors;
/**
* Determines if the validation context is in a valid state (no errors)
*
* @readonly
* @type {boolean}
* @memberOf ValidationContext
*/
readonly isValid: boolean;
/**
* Internal map of the errors.
*
* @private
*
* @memberOf ValidationContext
*/
private errorsMap;
/**
* Initializes a new instance of ValidationContext.
*/
constructor();
/**
* Resets the errors.
*
* @returns {IValidationContext}
*
* @memberOf ValidationContext
*/
reset(): this;
/**
* Validates the input object and stores any errors that may have occurred
* in `errors`.
*
* @template T The type of the object being validated.
* @param {T} obj
* @param {IValidationSchema<T>} schema
* @returns {IValidationContext}
*
* @memberOf ValidationContext
*/
validate<T>(obj: T, schema: IValidationSchema<T>): this;
/**
* Adds errors to the context.
*/
addErrors(errors: IValidationErrors | {
[key: string]: string[];
}): this;
/**
* Gets the errors for the given field.
*/
getErrors(field: string): string[];
/**
* Gets the first error for the given field.
* If not found, returns undefined.
*/
getError(field: string): string;
/**
* Removes errors for a particular field.
*/
clearErrors(field: string): this;
/**
* Ensures that an entry in the internal error map
* exists for the specified field.
*
* @private
* @param {string} field
* @returns
*
* @memberOf ValidationContext
*/
private ensureErrors;
/**
* At the end of a validation run, if a field
* has no errors, it's entry in the error map
* is removed.
*
* @private
*
* @memberOf ValidationContext
*/
private cleanupErrors;
}
export declare function validationContext<T>(objectToValidate: T, schema: IValidationSchema<T>): ISchemaBoundValidationContext<T>;
export declare function validationContext<T>(objectToValidate: T): IBoundValidationContext<T>;
export declare function validationContext(): IValidationContext;