Skip to content

Commit

Permalink
allow more symbols in account.Number
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Oct 29, 2024
1 parent 49737a9 commit 2b8b023
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions account/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/domonda/go-errs"
)

var numberRegex = regexp.MustCompile(`^[0-9A-Za-z_]+$`)
var numberRegex = regexp.MustCompile(`^[0-9A-Za-z][0-9A-Za-z_\-\/:.;,]*$`)

const (
ErrInvalidNumber errs.Sentinel = "invalid account number"
Expand Down Expand Up @@ -49,14 +49,14 @@ func NumberFromUint(u uint64) Number {
}

// Valid returns true if the Number matches
// the regular expression `^[0-9A-Za-z_]+$`
// the regular expression `^[0-9A-Za-z][0-9A-Za-z_\-\/:.;,]*$`
func (n Number) Valid() bool {
return numberRegex.MatchString(string(n))
}

// Validate returns a wrapped ErrInvalidNumber
// error if the Number does not match
// the regular expression `^[0-9A-Za-z_]+$`
// the regular expression `^[0-9A-Za-z][0-9A-Za-z_\-\/:.;,]*$`
func (n Number) Validate() error {
if !n.Valid() {
return fmt.Errorf("%w: %q", ErrInvalidNumber, n)
Expand Down
36 changes: 35 additions & 1 deletion account/number_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package account

import "testing"
import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNumber_TrimLeadingZeros(t *testing.T) {
tests := []struct {
Expand All @@ -27,3 +31,33 @@ func TestNumber_TrimLeadingZeros(t *testing.T) {
})
}
}

func TestNumber_Valid(t *testing.T) {
valid := []Number{
"0",
"0/",
"0/0",
"Hello_World",
"a.b",
"a:b",
"a,b",
"a;b",
"a.b.",
"a:b:",
"a,b,",
"a;b;",
}
for _, n := range valid {
t.Run(string(n), func(t *testing.T) {
require.Truef(t, n.Valid(), "Number(%#v).Valid()", n)
})
}
invalid := []Number{
"",
}
for _, n := range invalid {
t.Run(string(n), func(t *testing.T) {
require.Falsef(t, n.Valid(), "Number(%#v).Valid()", n)
})
}
}

0 comments on commit 2b8b023

Please sign in to comment.