Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misbehaviour with Regex and numbers #2604

Closed
jpdias opened this issue Mar 27, 2024 · 2 comments
Closed

Misbehaviour with Regex and numbers #2604

jpdias opened this issue Mar 27, 2024 · 2 comments

Comments

@jpdias
Copy link

jpdias commented Mar 27, 2024

Describe the bug
When trying to create a custom rule using the built-in pattern function we faced some issues as it is not able to validate number values correctly. More concretely, if we have an regex like '^-1$|^[1-9]\d*$' should not allow the numeric value -2 or bellow nor 0, and it accepts it. This problem does not happen if the value is set as string.

To Reproduce

If you create the unit test for this same regex you will get a pass while you should get a fail:

  it('given value matching the given match string regex without slashes, should return no error message', async () => {
    expect(await runPattern(-2, { match: '^-1$|^[1-9]\\d*$' })).toEqual([]);
  });

If you want you can also reproduce the same behaviour with a ruleset for any given value in a spec:

  function: pattern
  functionOptions:
    match: "^-1$|^[1-9]\\d*$"

Expected behavior
The pattern should work as expected and fail to validate negative values other than 1.

Environment (remove any that are not applicable):

  • Library version: Tested on both the develop branch as well as with version 1.18.0
  • OS: Tested on Ubuntu 22.04 and Mac OS 14.4

Additional context
I tried to understand the issue by running the validator code manually and change the tests but couldn't create a solution for the issue. I suspect something about expecting the entry value to be a String as it just does not work with numbers, but RegExp should handle both the same as in the following snippet in plain JS which works correctly as expected:

const regexPattern = '^-1$|^[1-9]\\d*$';
const regex = new RegExp(regexPattern);
const testStrings = [-1, "0", 1, "10", 100, -2, "abc"];
testStrings.forEach(testStr => {
    if (regex.test(testStr)) {
        console.log(`"${testStr}" matches the pattern.`);
    } else {
        console.log(`"${testStr}" does not match the pattern.`);
    }
});
@jpdias jpdias changed the title Misbheaviour with Regex and numbers Misbehaviour with Regex and numbers Mar 27, 2024
@mnaumanali94 mnaumanali94 added t/bug Something isn't working p/high triaged team/bad-news-bears and removed t/bug Something isn't working p/high labels May 3, 2024
@mnaumanali94
Copy link
Contributor

Regex cannot b used to validate numbers. Pattern only applies to strings.

@jpdias
Copy link
Author

jpdias commented May 3, 2024

Although I understand your observation @mnaumanali94, the value could always be converted to string when evaluated as a regex, thus this limitation would no longer apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants