diff --git a/src/ValidationError.ts b/src/ValidationError.ts index 20dee52e..2847dc7f 100644 --- a/src/ValidationError.ts +++ b/src/ValidationError.ts @@ -63,8 +63,11 @@ export default class ValidationError extends Error { message: string | ((params: Params) => string) | unknown, params: Params, ) { + // Attempt to make the path more friendly for error message interpolation. const path = params.label || params.path || 'this'; - if (path !== params.path) params = { ...params, path }; + // Store the original path under `originalPath` so it isn't lost to custom + // message functions; e.g., ones provided in `setLocale()` calls. + params = { ...params, path, originalPath: params.path }; if (typeof message === 'string') return message.replace(strReg, (_, key) => printValue(params[key])); diff --git a/src/index.ts b/src/index.ts index 2bdce99a..a129eee3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,9 +31,11 @@ import Schema, { CustomSchemaMetadata, } from './schema'; import type { + AnyMessageParams, InferType, ISchema, Message, + MessageParams, ValidateOptions, DefaultThunk, } from './types'; @@ -65,11 +67,13 @@ export type AnyObjectSchema = ObjectSchema; export type CastOptions = Omit; export type { + AnyMessageParams, AnyObject, InferType, InferType as Asserts, ISchema, Message, + MessageParams, AnySchema, MixedOptions, TypeGuard as MixedTypeGuard, diff --git a/src/types.ts b/src/types.ts index 4a04d59a..3fa8d5d9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -87,6 +87,7 @@ export interface MessageParams { path: string; value: any; originalValue: any; + originalPath: string; label: string; type: string; spec: SchemaSpec & Record;