add domain validation, allows most domains.

pull/998/head
aryehlev 2 years ago
parent 9e2ea40380
commit 0669d169a7
  1. 6
      baked_in.go
  2. 2
      regexes.go

@ -122,6 +122,7 @@ var (
"email": isEmail, "email": isEmail,
"url": isURL, "url": isURL,
"uri": isURI, "uri": isURI,
"domain": isDomain,
"urn_rfc2141": isUrnRFC2141, // RFC 2141 "urn_rfc2141": isUrnRFC2141, // RFC 2141
"file": isFile, "file": isFile,
"base64": isBase64, "base64": isBase64,
@ -1370,6 +1371,11 @@ func isURL(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface())) panic(fmt.Sprintf("Bad field type %T", field.Interface()))
} }
// isDomain is the validation function for validating if the current field's value is a valid domain.
func isDomain(fl FieldLevel) bool {
return domainRegex.MatchString(fl.Field().String())
}
// isUrnRFC2141 is the validation function for validating if the current field's value is a valid URN as per RFC 2141. // isUrnRFC2141 is the validation function for validating if the current field's value is a valid URN as per RFC 2141.
func isUrnRFC2141(fl FieldLevel) bool { func isUrnRFC2141(fl FieldLevel) bool {
field := fl.Field() field := fl.Field()

@ -64,6 +64,7 @@ const (
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$` bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
semverRegexString = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/ semverRegexString = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$" dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
domainRegexString = `^[a-z0-9-\.]{0,61}\.[a-z]{2,}$`
) )
var ( var (
@ -128,4 +129,5 @@ var (
bicRegex = regexp.MustCompile(bicRegexString) bicRegex = regexp.MustCompile(bicRegexString)
semverRegex = regexp.MustCompile(semverRegexString) semverRegex = regexp.MustCompile(semverRegexString)
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label) dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
domainRegex = regexp.MustCompile(domainRegexString)
) )

Loading…
Cancel
Save