Closed jpdias closed 2 months ago
Regex cannot b used to validate numbers. Pattern only applies to strings.
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: