data-transfer-object
Version:
Data Transfer Object class built on `class-validator`.
29 lines (28 loc) • 919 B
JavaScript
import { getValidationErrorMap } from './validation-error-map';
/**
* The currently set custom validation error handler, if any.
* To set this, use `.setValidationErrorHandler()`.
*/
export let onValidationError;
/**
* Register a callback to run whenever a validation fails.
* @param handler The callback that will receive either an array or a map of validation errors,
* depending on options.
* @param opts Options
*/
export function setValidationErrorHandler(handler, opts) {
if (opts?.rawErrors) {
onValidationError = handler;
return;
}
onValidationError = errors => {
const errorMap = getValidationErrorMap(errors);
return handler(errorMap);
};
}
/**
* Unregisters the callback to run when a validation fails, and uses the default behavior.
*/
export function clearValidationErrorHandler() {
onValidationError = undefined;
}