UNPKG

object-shape-tester

Version:
30 lines (29 loc) 809 B
import { Kind } from '@sinclair/typebox'; import { DefaultErrorFunction, SetErrorFunction, } from '@sinclair/typebox/errors'; const customErrorCallbacks = {}; /** * Register a custom error message for the given schema kind. * * @category Internal */ export function registerErrorMessage(kind, callback) { if (!(kind in customErrorCallbacks)) { customErrorCallbacks[kind] = callback; } } let errorMessageSet = false; /** * Sets the custom error messages. * * @category Internal */ export function setShapeDefinitionErrorMessage() { if (errorMessageSet) { return; } errorMessageSet = true; SetErrorFunction((error) => { const errorCallback = customErrorCallbacks[error.schema[Kind]] || DefaultErrorFunction; return errorCallback(error); }); }