Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ValidationError.formatError() clobbering path param #2250

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading