Skip to content

Commit

Permalink
pkg/utils/validation: Fix CIDRv4 validator
Browse files Browse the repository at this point in the history
  • Loading branch information
MusicDin committed Mar 9, 2024
1 parent 95f8fa7 commit 9b638cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/utils/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func initialize() {
validate.RegisterValidation("extra_vsemver", extra_VSemVer)
validate.RegisterValidation("extra_semverinrange", extra_SemVersionInRange)
validate.RegisterValidation("extra_ipinrange", extra_IPInRange)
validate.RegisterValidation("extra_cidrv4", extra_CIDRv4)
validate.RegisterValidation("extra_uniquefield", extra_UniqueField)
validate.RegisterValidation("extra_regexany", extra_RegexAny)
validate.RegisterValidation("extra_regexall", extra_RegexAll)
Expand Down Expand Up @@ -276,7 +277,7 @@ func CIDR() Validator {
// CIDRv4 checks whether the field value is a valid v4 CIDR address.
func CIDRv4() Validator {
return Validator{
Tags: "cidrv4",
Tags: "extra_cidrv4",
Err: "Field '{.Field}' must be a valid CIDRv4 address (actual: {.Value}).",
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/validation/validators_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func extra_IPInRange(fl validator.FieldLevel) bool {
return subnet.Contains(ip)
}

// extra_CIDRv4 returns true if struct filed is a valid v4 CIDR address.
// Note: Required since breaking change in go-playground/validation@10.15.5
func extra_CIDRv4(fl validator.FieldLevel) bool {
ip, _, err := net.ParseCIDR(fl.Field().String())
return err == nil && ip.To4() != nil
}

// extra_UniqueField returns true if struct field with a given name is unique for
// all slice elements.
func extra_UniqueField(fl validator.FieldLevel) bool {
Expand Down

0 comments on commit 9b638cf

Please sign in to comment.