Skip to content

fix(helpers): convert every array index in error paths; escape wildcard prefix - #60

Merged
hugomallet merged 2 commits into
masterfrom
fix/helpers-paths-wildcard
Aug 6, 2026
Merged

hugomallet merged 2 commits into
masterfrom
fix/helpers-paths-wildcard

Conversation

@hugomallet

@hugomallet hugomallet commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

Observable defects

  1. formatErrors: the bracket-to-dot .replace(/\[([0-9]+)\]/, '.$1') lacked the g flag, so only the first array index was converted — an AJV error on items[0].tags[1] produced field = "items.0.tags[1]", and <FieldError name="items.0.tags.1"> never matched.
  2. filterByFieldNameWithWildcard: the wildcard prefix went raw into new RegExp — the dot in user.* matched any character, so userX was wrongly matched. A prefix with an unclosed bracket (e.g. items[0*) even threw SyntaxError: Unterminated character class.

Fix

  • Add the g flag to the index-conversion replace.
  • Escape regex metacharacters in the wildcard prefix before building the regex (also fixes the SyntaxError crash).

Tests

  • New test: items[0].tags[1] formats to items.0.tags.1 (fails on master with items.0.tags[1]).
  • New test: user.* matches user.email but not userX (fails on master with userX included).
  • New test: items[0* filters without crashing (fails on master with SyntaxError: Unterminated character class).
  • CI=true yarn test:runtime: 6 suites, 66 tests (63 before + 3 new), 5 snapshots — all green.

Heads-up for consumers

If you worked around the old bug by writing bracket-notation paths in <FieldError name> (e.g. name="items.0.tags[1]"), those names will no longer match: error paths are now fully dot-normalized (items.0.tags.1).

@hugomallet hugomallet left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tooled self-review (review53 skill) — PR authored by this account, so GitHub refuses an APPROVE; posting as COMMENT. Verdict: mergeable.

Verified

  • formatErrors g flag: correct. Multi-digit indices (a[12].b[345] → a.12.b.345) and mixed paths convert properly; the [0-9]+ class leaves AJV's quoted-key notation (['weird-key']) untouched.
  • Wildcard prefix escaping: the escape set is the standard MDN one and handles [, (, +, \ correctly (checked by direct evaluation). Bonus not stated in the PR description: the old code threw SyntaxError: Unterminated character class for a prefix containing an unbalanced [ (e.g. items[0.*) — the escape fixes that latent crash too.
  • Both call sites in src/lib/Form/Form.js (errors + touchedFields filtering) are consistent with the new behavior; scope is strictly helpers.js + helpers.test.js as announced.
  • New tests bite (fail on master), full suite green: 6 suites, 65 tests. Lint: the 6 jest/no-test-callback errors in helpers.test.js are pre-existing (untouched getFieldValue blocks), not introduced here.

Minor (grouped, non-blocking)

  1. The new wildcard test only exercises .; one extra case with a [ prefix (e.g. items[0].*) would lock in the SyntaxError fix above, which is currently untested.
  2. Consumers who worked around bug 1 by writing bracket paths in <FieldError name> (e.g. items.0.tags[1]) will see those stop matching — intentional, but worth a line in the changelog; same for the SyntaxError fix.

…rd prefix

- formatErrors: the bracket-to-dot replace lacked the g flag, so only the
  first array index was converted (items[0].tags[1] -> items.0.tags[1]),
  breaking FieldError matching on deeply indexed paths.
- filterByFieldNameWithWildcard: the wildcard prefix was injected raw into
  a RegExp, so the dot in 'user.*' matched any character and 'userX' was
  wrongly included.

Both fixes are covered by new unit tests.
…card prefixes

Before escaping, a wildcard like 'items[0*' compiled to /^items[0/ and
threw 'SyntaxError: Unterminated character class'. The escaping fix also
resolves this crash; this test locks it in.
@hugomallet
hugomallet force-pushed the fix/helpers-paths-wildcard branch from efcd643 to 6f25159 Compare August 6, 2026 13:13
@hugomallet
hugomallet merged commit fb5fa5d into master Aug 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant