diff --git a/pkg/rules/regex.go b/pkg/rules/regex.go index 3029fd2..a3d88ea 100644 --- a/pkg/rules/regex.go +++ b/pkg/rules/regex.go @@ -12,7 +12,7 @@ var ( uuidRegexp = lazyRegexCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) asciiRegexp = lazyRegexCompile(`^[\x00-\x7F]*$`) // Ref: https://www.ietf.org/rfc/rfc1123.txt - rfc1123DnsLabelRegexp = lazyRegexCompile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$") + rfc1123DnsLabelRegexp = lazyRegexCompile("^[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?$") ) // lazyRegexCompile returns a function that compiles the regular expression diff --git a/pkg/rules/string.go b/pkg/rules/string.go index 66a5395..a3e33b5 100644 --- a/pkg/rules/string.go +++ b/pkg/rules/string.go @@ -63,13 +63,11 @@ func StringDenyRegexp(re *regexp.Regexp, examples ...string) govy.Rule[string] { } // StringDNSLabel ensures the property's value is a valid DNS label as defined by RFC 1123. -func StringDNSLabel() govy.RuleSet[string] { - return govy.NewRuleSet( - StringLength(1, 63), - StringMatchRegexp(rfc1123DnsLabelRegexp(), "my-name", "123-abc"). - WithDetails("an RFC-1123 compliant label name must consist of lower case alphanumeric characters or '-',"+ - " and must start and end with an alphanumeric character"), - ).WithErrorCode(ErrorCodeStringDNSLabel) +func StringDNSLabel() govy.Rule[string] { + return StringMatchRegexp(rfc1123DnsLabelRegexp(), "my-name", "123-abc"). + WithDetails("an RFC-1123 compliant label name must consist of lower case alphanumeric characters or '-'," + + " and must start and end with an alphanumeric character"). + WithErrorCode(ErrorCodeStringDNSLabel) } // StringEmail ensures the property's value is a valid email address. diff --git a/pkg/rules/string_test.go b/pkg/rules/string_test.go index 4dd5050..f8974bf 100644 --- a/pkg/rules/string_test.go +++ b/pkg/rules/string_test.go @@ -82,9 +82,7 @@ func TestStringDNSLabel(t *testing.T) { err := StringDNSLabel().Validate(tc.in) if tc.shouldFail { assert.Error(t, err) - for _, e := range err.(govy.RuleSetError) { - assert.True(t, govy.HasErrorCode(e, ErrorCodeStringDNSLabel)) - } + assert.True(t, govy.HasErrorCode(err, ErrorCodeStringDNSLabel)) } else { assert.NoError(t, err) }