Skip to content
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/utils/validateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const AsyncValidator: any = RawAsyncValidator;
* `I'm ${name}` + { name: 'bamboo' } = I'm bamboo
*/
function replaceMessage(template: string, kv: Record<string, string>): 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];
});
Expand Down
24 changes: 24 additions & 0 deletions tests/validate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1081,4 +1081,28 @@ describe('Form.Validate', () => {

jest.useRealTimers();
});

it('should handle escaped and unescaped variables correctly', async () => {
const { container } = render(
<Form>
<InfoField
messageVariables={{
name: 'bamboo',
}}
name="test"
rules={[
{
validator: () => Promise.reject(new Error('\\${name} should be ${name}!')),
},
]}
>
<Input />
</InfoField>
</Form>,
);

// Wrong value
await changeValue(getInput(container), 'light');
matchError(container, '${name} should be bamboo!');
});
});