You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.`);
}
});
The text was updated successfully, but these errors were encountered:
jpdias
changed the title
Misbheaviour with Regex and numbers
Misbehaviour with Regex and numbers
Mar 27, 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.
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:
If you want you can also reproduce the same behaviour with a ruleset for any given value in a spec:
Expected behavior
The pattern should work as expected and fail to validate negative values other than 1.
Environment (remove any that are not applicable):
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:
The text was updated successfully, but these errors were encountered: