Skip to content

Commit

Permalink
Fix ValidationError.formatError() clobbering path param (#2250)
Browse files Browse the repository at this point in the history
And export `AnyMessageParams`, `MessageParams` types.

Co-authored-by: Shaun Grady <[email protected]>
  • Loading branch information
shaungrady and Shaun Grady authored Nov 25, 2024
1 parent 5a22c16 commit 87be159
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import Schema, {
CustomSchemaMetadata,
} from './schema';
import type {
AnyMessageParams,
InferType,
ISchema,
Message,
MessageParams,
ValidateOptions,
DefaultThunk,
} from './types';
Expand Down Expand Up @@ -65,11 +67,13 @@ export type AnyObjectSchema = ObjectSchema<any, any, any, any>;
export type CastOptions = Omit<BaseCastOptions, 'path' | 'resolved'>;

export type {
AnyMessageParams,
AnyObject,
InferType,
InferType as Asserts,
ISchema,
Message,
MessageParams,
AnySchema,
MixedOptions,
TypeGuard as MixedTypeGuard,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface MessageParams {
path: string;
value: any;
originalValue: any;
originalPath: string;
label: string;
type: string;
spec: SchemaSpec<any> & Record<string, unknown>;
Expand Down

0 comments on commit 87be159

Please sign in to comment.