From b1b8571877a767b848cf575309c8100f84336e6c Mon Sep 17 00:00:00 2001 From: Otto Bittner Date: Wed, 8 Nov 2023 11:39:35 +0100 Subject: [PATCH] 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. --- internal/validation/constraints.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/validation/constraints.go b/internal/validation/constraints.go index 4862c828d..6de84d8d1 100644 --- a/internal/validation/constraints.go +++ b/internal/validation/constraints.go @@ -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. @@ -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