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

Add export for any validation regex #219

Merged
merged 2 commits into from
Oct 21, 2023
Merged

Add export for any validation regex #219

merged 2 commits into from
Oct 21, 2023

Conversation

fabian-hiller
Copy link
Owner

No description provided.

@fabian-hiller fabian-hiller added the enhancement New feature or request label Oct 20, 2023
@fabian-hiller fabian-hiller self-assigned this Oct 20, 2023
@netlify
Copy link

netlify bot commented Oct 20, 2023

Deploy Preview for valibot canceled.

Name Link
🔨 Latest commit 236d6c1
🔍 Latest deploy log https://app.netlify.com/sites/valibot/deploys/65340a670e7d360007ec2aed

@@ -7,7 +12,7 @@
*/
export function isLuhnAlgo(input: string) {
// Remove any non-digit chars
const number = input.replace(/\D/gu, '');
const number = input.replace(NON_DIGIT_REGEX, '');
Copy link
Contributor

@kurtextrem kurtextrem Oct 21, 2023

Choose a reason for hiding this comment

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

needs replaceAll here (could be automatically captured by https://github.com/fabian-hiller/valibot/pull/213/files unicorn/prefer-string-replace-all I think.

reason is: a global regex (/g) will set lastIndex to the last index where the regex stopped. Which means if string a is "abc1" and string b is "a", lastIndex will be set to e.g. 3, but string b has no index 3, so the regex now fails at all times.
If you want to avoid replaceAll, you'll need to call
NON_DIGIT_REGEX.lastIndex = 0 before calling the replace

* [IPv4](https://en.wikipedia.org/wiki/IPv4) regex.
*/
// eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive
export const IPV4_REGEX = /^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)\.?\b){4}$/u;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export const IPV4_REGEX = /^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)\.?\b){4}$/u;
export const IPV4_REGEX = /^(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)){3}$/u;

@fabian-hiller fabian-hiller merged commit a43ac42 into main Oct 21, 2023
@fabian-hiller fabian-hiller deleted the feat/export-regex branch October 21, 2023 18:18
@fabian-hiller
Copy link
Owner Author

v0.20.0 is now available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants