Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Aug 14, 2024
2 parents f12d187 + 0ed0e7b commit 38ac0de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-field-form",
"version": "2.3.0",
"version": "2.4.0",
"description": "React Form Component",
"typings": "es/index.d.ts",
"engines": {
Expand Down
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!');
});
});

0 comments on commit 38ac0de

Please sign in to comment.