Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Oct 1, 2024
1 parent 61d6ab0 commit 5da782e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/rules/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions pkg/rules/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions pkg/rules/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 5da782e

Please sign in to comment.