diff --git a/src/utils/validateUtil.ts b/src/utils/validateUtil.ts index d6b4146ed..09106d1d2 100644 --- a/src/utils/validateUtil.ts +++ b/src/utils/validateUtil.ts @@ -19,7 +19,10 @@ const AsyncValidator: any = RawAsyncValidator; * `I'm ${name}` + { name: 'bamboo' } = I'm bamboo */ function replaceMessage(template: string, kv: Record): string { - return template.replace(/\$\{\w+\}/g, (str: string) => { + return template.replace(/\\?\$\{\w+\}/g, (str: string) => { + if (str.startsWith('\\')) { + return str.slice(1); + } const key = str.slice(2, -1); return kv[key]; }); diff --git a/tests/validate.test.tsx b/tests/validate.test.tsx index e5810d775..2a9d92271 100644 --- a/tests/validate.test.tsx +++ b/tests/validate.test.tsx @@ -1081,4 +1081,28 @@ describe('Form.Validate', () => { jest.useRealTimers(); }); + + it('should handle escaped and unescaped variables correctly', async () => { + const { container } = render( +
+ Promise.reject(new Error('\\${name} should be ${name}!')), + }, + ]} + > + + +
, + ); + + // Wrong value + await changeValue(getInput(container), 'light'); + matchError(container, '${name} should be bamboo!'); + }); });