Skip to content

Commit

Permalink
validation: use regex instead of dns lookup
Browse files Browse the repository at this point in the history
Doing a DNS lookup may fail for domain names that are valid
but currently not assigned.
The old test also breaks inside the bazel sandbox.
  • Loading branch information
derpsteb committed Nov 8, 2023
1 parent 8341db3 commit b1b8571
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/validation/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"regexp"
)

// Used to validate DNS names.
var domainRegex = regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)

// Constraint is a constraint on a document or a field of a document.
type Constraint struct {
// Satisfied returns no error if the constraint is satisfied.
Expand Down Expand Up @@ -208,7 +211,7 @@ func CIDR(s string) *Constraint {
func DNSName(s string) *Constraint {
return &Constraint{
Satisfied: func() *TreeError {
if _, err := net.LookupHost(s); err != nil {
if !domainRegex.MatchString(s) {
return NewErrorTree(fmt.Errorf("%s must be a valid DNS name", s))
}
return nil
Expand Down

0 comments on commit b1b8571

Please sign in to comment.