Skip to content

Add IPv6 validator for strings #197

@rshelekhov

Description

@rshelekhov

Hi @Oudwins, following the recent implementation of the IPv4 validator (PR #192), I'd like to propose adding an IPv6 validator to complete the IP validation suite.

Example usage

field := z.String().IPv6()
err := field.Validate("2001:db8::1") // Valid
err = field.Validate("192.168.1.1") // Invalid (IPv4)
err = field.Validate("::ffff:192.168.1.1") // Valid (IPv6-mapped IPv4)

The groundwork is already in place in your lib:

  • IssueCodeIP constant exists
  • Error message template supports {{ip}} placeholder
  • Just need to add IPv6 = "IPv6" constant
  • Validator logic would be like this (I checked how it's implemented in go-playground/validator):
ip := net.ParseIP(string(*v));
return ip != nil && ip.To4() == nil

Unlike IPv4 validator which rejects any : syntax, IPv6 validator should:

  • Accept pure IPv6: "2001:db8::1"
  • Accept IPv6-mapped IPv4: "::ffff:192.168.1.1"
  • Reject pure IPv4: "192.168.1.1"

Question for Discussion

Should we also add a generic IP() validator that accepts both IPv4 and IPv6? Perhaps this would be useful for cases where users don't care about the specific version.

If you're open to this addition, I'm happy to implement it following the same patterns we established for the IPv4 validator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions