Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ var (
"cron": isCron,
"spicedb": isSpiceDB,
"ein": isEIN,
"isin": isISIN,
"validateFn": isValidateFn,
}
)
Expand Down Expand Up @@ -3505,9 +3506,30 @@ func isEIN(fl FieldLevel) bool {
return einRegex().MatchString(field.String())
}

// isISIN is the validation function for validating if the current field's value is a valid International Securities Identification Number (ISIN)
func isISIN(fl FieldLevel) bool {
field := fl.Field()
isin := field.String()

if !isinRegex().MatchString(isin) {
return false
}

var digits []string
for _, c := range isin {
if c >= 'A' && c <= 'Z' {
val := int(c - 'A' + 10)
digits = append(digits, strconv.Itoa(val/10), strconv.Itoa(val%10))
} else {
digits = append(digits, string(c))
}
}

return digitsHaveLuhnChecksum(digits)
}

var (
errMethodNotFound = errors.New(`method not found`)
errMethodReturnNoValues = errors.New(`method return o values (void)`)
errMethodReturnInvalidType = errors.New(`method should return invalid type`)
)

2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
spicedbPermissionRegexString = "^([a-z][a-z0-9_]{1,62}[a-z0-9])?$"
spicedbTypeRegexString = "^([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]$"
einRegexString = "^(\\d{2}-\\d{7})$"
isinRegexString = `^[A-Z]{2}[A-Z0-9]{9}[0-9]$`
)

func lazyRegexCompile(str string) func() *regexp.Regexp {
Expand Down Expand Up @@ -170,4 +171,5 @@ var (
spicedbPermissionRegex = lazyRegexCompile(spicedbPermissionRegexString)
spicedbTypeRegex = lazyRegexCompile(spicedbTypeRegexString)
einRegex = lazyRegexCompile(einRegexString)
isinRegex = lazyRegexCompile(isinRegexString)
)
93 changes: 63 additions & 30 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6321,32 +6321,32 @@ func TestMIMETypeValidation(t *testing.T) {
}

tests := []struct {
title string
param string
tag string
expected bool
createFile func()
title string
param string
tag string
expected bool
createFile func()
}{
{
title: "empty path",
param: paths["empty"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
title: "empty path",
param: paths["empty"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
},
{
title: "directory, not a file",
param: paths["directory"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
title: "directory, not a file",
param: paths["directory"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
},
{
title: "missing file",
param: paths["missing"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
title: "missing file",
param: paths["missing"],
tag: "mimetype=image/png",
expected: false,
createFile: func() {},
},
{
title: "exact png match",
Expand Down Expand Up @@ -6401,11 +6401,11 @@ func TestMIMETypeValidation(t *testing.T) {
},
},
{
title: "type mismatch",
param: paths["go"],
tag: "mimetype=image/*",
expected: false,
createFile: func() {},
title: "type mismatch",
param: paths["go"],
tag: "mimetype=image/*",
expected: false,
createFile: func() {},
},
{
title: "subtype mismatch",
Expand All @@ -6425,11 +6425,11 @@ func TestMIMETypeValidation(t *testing.T) {
},
},
{
title: "invalid validator param missing subtype",
param: paths["go"],
tag: "mimetype=image",
expected: false,
createFile: func() {},
title: "invalid validator param missing subtype",
param: paths["go"],
tag: "mimetype=image",
expected: false,
createFile: func() {},
},
}

Expand Down Expand Up @@ -15504,6 +15504,39 @@ func TestEINStringValidation(t *testing.T) {
}
}

func TestISINStringValidation(t *testing.T) {
tests := []struct {
value string `validate:"isin"`
expected bool
}{
{"US0378331005", true},
{"GB0002634946", true},
{"DE0005140008", true},
{"US037833100", false},
{"US03783310055", false},
{"0S0378331005", false},
{"U00378331005", false},
{"US0378331006", false},
{"us0378331005", false},
{"US037833100a", false},
}
validate := New()

for i, test := range tests {
errs := validate.Var(test.value, "isin")

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d isin failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d isin failed Error: %s", i, errs)
}
}
}
}

func TestPrivateFieldsStruct(t *testing.T) {
type tc struct {
stct interface{}
Expand Down