validation: use regex instead of dns lookup

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.
This commit is contained in:
Otto Bittner 2023-11-08 11:39:35 +01:00
parent 8341db3c33
commit b1b8571877

View File

@ -13,6 +13,9 @@ import (
"regexp" "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. // Constraint is a constraint on a document or a field of a document.
type Constraint struct { type Constraint struct {
// Satisfied returns no error if the constraint is satisfied. // Satisfied returns no error if the constraint is satisfied.
@ -208,7 +211,7 @@ func CIDR(s string) *Constraint {
func DNSName(s string) *Constraint { func DNSName(s string) *Constraint {
return &Constraint{ return &Constraint{
Satisfied: func() *TreeError { 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 NewErrorTree(fmt.Errorf("%s must be a valid DNS name", s))
} }
return nil return nil