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

feat(#2758): z.string().ipRange() - add support for IP address range #3754

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

wataryooou
Copy link
Contributor

@wataryooou wataryooou commented Sep 14, 2024

This pull request fixes #2758 adding IP Address range (CIDR) validation in zod string.

  • Basic implementation
  • Choose what type of ip
  • Add IPv4/IPv6 address range
  • Add ipRange to docs

relate: #3820

Copy link

netlify bot commented Sep 14, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit deb9393
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/675d123218483f0008b89872
😎 Deploy Preview https://deploy-preview-3754--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@wataryooou wataryooou marked this pull request as draft October 27, 2024 03:57
@wataryooou wataryooou marked this pull request as ready for review October 27, 2024 08:11
@Tamas-hi
Copy link

Tamas-hi commented Nov 8, 2024

This looks really good, I was just in need for this exact functionality.
Is there any chance this can land in a release?

@clarkmcc
Copy link

I have nothing productive to contribute other than this would be really nice to have land in a release!

@wataryooou
Copy link
Contributor Author

Notice: Updated to follow the latest main branch.

Comment on lines -762 to -772
function isValidCidr(ip: string, version?: IpVersion) {
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
return true;
}
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
return true;
}

return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[FYI] Just moved isValidCidr slightly up.

Comment on lines +1087 to 1094
} else if (check.kind === "ipRange") {
if (!isValidIPRange(input.data, check.cidr, check.version)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "jwt",
validation: "ipRange",
code: ZodIssueCode.invalid_string,
message: check.message,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

[FYI] I just rearranged the order so that all ip-related code is grouped together for better readability.
The internal code hasn't been changed 👍

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

Successfully merging this pull request may close these issues.

Support IP Range Validation
5 participants