adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

[Bug] Working with `mixed` type fields that generate property paths containing regex special characters l

Open Gres opened this issue 9 months ago • 0 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Problem Description

Issue: AdminJS throws a 400 Bad Request error with message "Invalid regular expression: /^cases\\.$*($|\\.)/: Nothing to repeat" when trying to edit records with key-value or mixed type fields that contain special regex characters in their property paths.

When it occurs:

  • Editing records through AdminJS interface
  • Working with key-value type fields
  • Working with mixed type fields that generate property paths containing regex special characters like $, *, +, ?, ^, {, }, (, ), |, [, ], \

Root Cause:

The propertyKeyRegex function in /lib/utils/flat/property-key-regex.js directly uses propertyPath parameter to construct a RegExp without escaping special regex characters. When AdminJS processes complex fields, it can generate property paths like cases.$* where $* are interpreted as regex metacharacters, causing "Nothing to repeat" error because * quantifier has nothing to operate on.

Error Details:

{
    "statusCode": 400,
    "message": "Invalid regular expression: /^cases\\.$*($|\\.)/: Nothing to repeat",
    "path": "/admin/api/resources/DeclensionEntry/records/682585e79edd269dff69734d/edit",
    "timestamp": "2025-06-05T16:45:01.269Z"
}

How to Reproduce:

  1. Create a resource with a key-value or mixed type field
  2. Configure the field to handle Map-like data structures
  3. Try to edit a record through AdminJS interface
  4. AdminJS will generate property paths containing special characters
  5. The propertyKeyRegex function will fail to create a valid RegExp

Example Resource Configuration:

// This configuration can trigger the issue
{
  resource: MyModel,
  options: {
    properties: {
      cases: {
        type: 'key-value', // or 'mixed'
        isRequired: true,
        description: 'Cases data'
      }
    }
  }
}

Current Behavior:

  • AdminJS crashes with 400 error when processing fields with special regex characters
  • Unable to edit records containing such fields
  • The error occurs in the regex construction phase

Expected Behavior:

  • AdminJS should handle property paths with special characters gracefully
  • Records with complex field types should be editable without errors
  • Property paths should be properly escaped before being used in RegExp construction

Impact:

  • High: Makes AdminJS unusable for resources with key-value or mixed type fields
  • Affects any project using AdminJS with complex data structures
  • No workaround available without patching the library

Environment:

  • AdminJS version: 7.8.15
  • Node.js version:v20.16.0
  • Operating System: mac os 15.1.1 (24B91)

Additional Context:

This issue affects real-world usage scenarios where developers need to manage complex data structures like:

  • Multi-language content with different cases/declensions
  • Configuration objects with dynamic keys
  • Metadata fields with flexible schemas
  • Any Map-like data structures

The fix is straightforward - escape special regex characters in the property path before using it to construct RegExp patterns.

Here is the diff that solved my problem:

[diff content as provided]

This issue body was partially generated by patch-package.

Gres avatar Jun 05 '25 22:06 Gres