Added in validation function, tests, and docs

pull/355/head
josh 6 years ago
parent ba5836f763
commit 36d83b0b83
  1. 16
      baked_in.go
  2. 16
      doc.go
  3. 79
      validator_test.go

@ -105,6 +105,8 @@ var (
"isbn": isISBN,
"isbn10": isISBN10,
"isbn13": isISBN13,
"eth_addr": isEthereumAddress,
"btc_addr": isBitcoinAddress,
"uuid": isUUID,
"uuid3": isUUID3,
"uuid4": isUUID4,
@ -387,6 +389,20 @@ func isISBN10(fl FieldLevel) bool {
return checksum%11 == 0
}
// IsEthereumAddress is the validation function for validating if the field's value is a valid ethereum address based currently only on the format
func isEthereumAddress(fl FieldLevel) bool {
field := fl.Field()
return ethAddressRegex.MatchString(field.String())
}
// IsBitcoinAddress is the validation function for validating if the field's value is a valid btc address, currently only based on the format
func isBitcoinAddress(fl FieldLevel) bool {
field := fl.Field()
return btcAddressRegex.MatchString(field.String()) || btcAddressRegexBech32.MatchString(field.String())
}
// ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
func excludesRune(fl FieldLevel) bool {
return !containsRune(fl)

@ -609,6 +609,21 @@ this with the omitempty tag.
Usage: base64
Bitcoin Address
This validates that a string value contains a valid bitcoin address.
The format of the string is checked to ensure it matches one of the three formats
P2PKH, P2SH, or Bech32. Currently no further validation is performed.
Usage: btc_addr
Ethereum Address
This validates that a string value contains a valid ethereum address.
The format of the string is checked to ensure it matches the standard Ethereum address format
Usage: eth_addr
Contains
This validates that a string value contains the substring value.
@ -665,7 +680,6 @@ This validates that a string value contains a valid isbn13 value.
Usage: isbn13
Universally Unique Identifier UUID
This validates that a string value contains a valid UUID.

@ -4398,6 +4398,85 @@ func TestBase64Validation(t *testing.T) {
AssertError(t, errs, "", "", "", "", "base64")
}
func TestEthereumAddressValidation(t *testing.T){
validate := New()
tests := []struct {
param string
expected bool
}{
{"", false},
{"0x02F9AE5f22EA3fA88F05780B30385bEC", false},
{"123f681646d4a755815f9cb19e1acc8565a0c2ac", false},
{"0x02F9AE5f22EA3fA88F05780B30385bECFacbf130", true},
{"0x123f681646d4a755815f9cb19e1acc8565a0c2ac", true},
}
for i, test := range tests {
errs := validate.Var(test.param, "eth_addr")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d eth_addr failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d eth_addr failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "eth_addr" {
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs)
}
}
}
}
}
func TestBitcoinAddressValidation(t *testing.T){
validate := New()
tests := []struct {
param string
expected bool
}{
{"", false},
{"0x02F9AE5f22EA3fA88F05780B30385bEC", false},
{"123f681646d4a755815f9cb19e1acc8565a0c2ac", false},
{"1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", true},
{"3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy", true},
{"bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", true},
}
for i, test := range tests {
errs := validate.Var(test.param, "btc_addr")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d btc_addr failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d btc_addr failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "btc_addr" {
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs)
}
}
}
}
}
func TestNoStructLevelValidation(t *testing.T) {
type Inner struct {

Loading…
Cancel
Save