roqueform-external-errors-plugin
Version:
The plugin that associates external errors with Roqueform fields using adapters.
54 lines (53 loc) • 2.3 kB
TypeScript
import { FieldPlugin } from 'roqueform';
/**
* A callback that receives an external error and returns an error that must be associated with the field. Or returns
* `null` if the external error must not be associated with the field.
*/
export type ExternalErrorAdapter<ExternalError, Error> = (externalError: ExternalError) => Error | null | undefined;
/**
* The plugin that associates external errors with fields using adapters.
*
* @template ExternalError The external error that can be adapted by the field.
* @template Error The error that is associated with the field.
*/
export interface ExternalErrorsMixin<ExternalError, Error> {
/**
* An array of adapters that associate errors with this field.
*/
externalErrorAdapters: ExternalErrorAdapter<ExternalError, Error>[];
/**
* Sets external errors to the field and its descendants.
*
* @param externalErrors The external errors to set.
* @param options Additional options.
* @returns An array of external errors that were added to fields.
*/
setExternalErrors(externalErrors: ExternalError[] | null | undefined, options?: SetExternalErrorsOptions<ExternalError, Error>): ExternalError[];
}
/**
* Options of the {@link ExternalErrorsMixin.setExternalErrors} method.
*
* @template ExternalError The external error that can be adapted by the field.
* @template Error The error that is associated with the field.
*/
export interface SetExternalErrorsOptions<ExternalError, Error> {
/**
* If `true` then external errors are adapted by both this field and all of its descendant fields.
*
* @default false
*/
isRecursive?: boolean;
/**
* Called when there are external errors that were not associated with any field.
*
* @param externalError The external error to set.
*/
orphanExternalErrorAdapter?: ExternalErrorAdapter<ExternalError, Error>;
}
/**
* The plugin that associates external errors with fields using adapters.
*
* @template ExternalError The external error that can be adapted by the field.
* @template Error The error that is associated with the field.
*/
export default function externalErrorsPlugin<ExternalError = any, Error = ExternalError>(): FieldPlugin<any, ExternalErrorsMixin<ExternalError, Error>>;