Repository: react-hook-form/resolvers (ts)

Issue:
Validation error when field's name contains special characters since v4.0.0
**Describe the bug**
Form validation error when the field's name contains some special characters since v4.0.0.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to [this sandbox](https://codesandbox.io/p/devbox/react-hook-form-root-error-forked-8hs5n6?layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522cm0b3xoks00063b6mue46slnk%2522%252C%2522sizes%2522%253A%255B100%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522cm0b3xoks00023b6m7js2xvio%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522cm0b3xoks00033b6mmstu343b%2522%257D%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522cm0b3xoks00053b6mdfc7brti%2522%257D%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522cm0b3xoks00023b6m7js2xvio%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522cm0b3xoks00013b6mb5lm115a%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fsrc%252Findex.tsx%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522id%2522%253A%2522cm0i6iny600023b6jugx6j61f%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fsrc%252FApp.tsx%2522%252C%2522state%2522%253A%2522IDLE%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A41%252C%2522startColumn%2522%253A33%252C%2522endLineNumber%2522%253A41%252C%2522endColumn%2522%253A33%257D%255D%257D%255D%252C%2522id%2522%253A%2522cm0b3xoks00023b6m7js2xvio%2522%252C%2522activeTabId%2522%253A%2522cm0i6iny600023b6jugx6j61f%2522%257D%252C%2522cm0b3xoks00053b6mdfc7brti%2522%253A%257B%2522id%2522%253A%2522cm0b3xoks00053b6mdfc7brti%2522%252C%2522activeTabId%2522%253A%2522cm0b4l4yx005l3b6m0szymcu6%2522%252C%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522cm0b3xoks00043b6mqzrwg2dn%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%257D%252C%257B%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%252C%2522id%2522%253A%2522cm0b4l4yx005l3b6m0szymcu6%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%257D%252C%2522cm0b3xoks00033b6mmstu343b%2522%253A%257B%2522tabs%2522%253A%255B%255D%252C%2522id%2522%253A%2522cm0b3xoks00033b6mmstu343b%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Afalse%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D)
2. Type "A" in the field
3. Submit the form
4. See the error:
> Invalid regular expression: /^[array-2]\.\d+/: Range out of order in character class


**Codesandbox link (Required)**
[link to the sandbox](https://codesandbox.io/p/devbox/react-hook-form-root-error-forked-8hs5n6?layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522cm0b3xoks00063b6mue46slnk%2522%252C%2522sizes%2522%253A%255B100%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522cm0b3xoks00023b6m7js2xvio%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522cm0b3xoks00033b6mmstu343b%2522%257D%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522cm0b3xoks00053b6mdfc7brti%2522%257D%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522cm0b3xoks00023b6m7js2xvio%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522cm0b3xoks00013b6mb5lm115a%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fsrc%252Findex.tsx%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522id%2522%253A%2522cm0i6iny600023b6jugx6j61f%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fsrc%252FApp.tsx%2522%252C%2522state%2522%253A%2522IDLE%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A41%252C%2522startColumn%2522%253A33%252C%2522endLineNumber%2522%253A41%252C%2522endColumn%2522%253A33%257D%255D%257D%255D%252C%2522id%2522%253A%2522cm0b3xoks00023b6m7js2xvio%2522%252C%2522activeTabId%2522%253A%2522cm0i6iny600023b6jugx6j61f%2522%257D%252C%2522cm0b3xoks00053b6mdfc7brti%2522%253A%257B%2522id%2522%253A%2522cm0b3xoks00053b6mdfc7brti%2522%252C%2522activeTabId%2522%253A%2522cm0b4l4yx005l3b6m0szymcu6%2522%252C%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522cm0b3xoks00043b6mqzrwg2dn%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%257D%252C%257B%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%252C%2522id%2522%253A%2522cm0b4l4yx005l3b6m0szymcu6%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%257D%252C%2522cm0b3xoks00033b6mmstu343b%2522%253A%257B%2522tabs%2522%253A%255B%255D%252C%2522id%2522%253A%2522cm0b3xoks00033b6mmstu343b%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Afalse%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D)

**Expected behavior**
Before v4.0.0 the validation was working fine (you can change the version in the package.json to v3.9.0 to see that everything is ok.
I'm suspecting https://github.com/react-hook-form/resolvers/commit/985c48dab56636759914b41af1d57274ebd7d20a to be the reason of this error.

According to me it's related to the `-` in the field's name. For the context we have this character because some of our fields contain UUID.


**Screenshots**

<img width="1021" alt="Image" src="https://github.com/user-attachments/assets/a24ce392-ad61-410d-91a0-d55f7821e457" />

**Desktop (please complete the following information):**

- OS: iOS
- Browser: Chrome
- Version 133.0.6943.127 


Pull request description:
fix: escape square brackets in field name regex pattern
Fixes #750 

Patch:
diff --git a/src/toNestErrors.ts b/src/toNestErrors.ts
index 454f7d4..4bd48b5 100644
--- a/src/toNestErrors.ts
+++ b/src/toNestErrors.ts
@@ -38,4 +38,18 @@ export const toNestErrors = <TFieldValues extends FieldValues>(
 const isNameInFieldArray = (
   names: InternalFieldName[],
   name: InternalFieldName,
-) => names.some((n) => n.match(`^${name}\\.\\d+`));
+) => {
+  const path = escapeBrackets(name);
+  return names.some((n) => escapeBrackets(n).match(`^${path}\\.\\d+`));
+};
+
+/**
+ * Escapes special characters in a string to be used in a regex pattern.
+ * it removes the brackets from the string to match the `set` method.
+ *
+ * @param input - The input string to escape.
+ * @returns The escaped string.
+ */
+function escapeBrackets(input: string): string {
+  return input.replace(/\]|\[/g, '');
+}


Test patch:
diff --git a/src/__tests__/toNestErrors.ts b/src/__tests__/toNestErrors.ts
index ebea917..67d493c 100644
--- a/src/__tests__/toNestErrors.ts
+++ b/src/__tests__/toNestErrors.ts
@@ -284,3 +284,24 @@ test('ensures consistent ordering when a field array has a root error and an err
     },
   });
 });
+
+test('should correctly validate object with special characters', () => {
+  const result = toNestErrors(
+    { '[array-2]': { type: 'string', message: 'string is required' } },
+    {
+      names: ['[array-2]'],
+      fields: {
+        '[array-2]': { name: '[array-2]', ref: { name: '[array-2]' } },
+      },
+      shouldUseNativeValidation: false,
+    },
+  );
+
+  expect(result).toEqual({
+    'array-2': {
+      type: 'string',
+      message: 'string is required',
+      ref: { name: '[array-2]' },
+    },
+  });
+});