forked from cerc-io/laconicd-deprecated
types: add ValidateNonZeroAddress
utility function (#1033)
* Add ValidateNonZeroAddress validation utility * Update types/validation.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
parent
c70701e785
commit
e1512c52e9
@ -29,6 +29,18 @@ func ValidateAddress(address string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateNonZeroAddress returns an error if the provided string is not a hex
|
||||
// formatted string address or is equal to zero
|
||||
func ValidateNonZeroAddress(address string) error {
|
||||
if IsZeroAddress(address) {
|
||||
return sdkerrors.Wrapf(
|
||||
sdkerrors.ErrInvalidAddress, "address '%s' must not be zero",
|
||||
address,
|
||||
)
|
||||
}
|
||||
return ValidateAddress(address)
|
||||
}
|
||||
|
||||
// SafeInt64 checks for overflows while casting a uint64 to int64 value.
|
||||
func SafeInt64(value uint64) (int64, error) {
|
||||
if value > uint64(math.MaxInt64) {
|
||||
|
@ -85,6 +85,37 @@ func TestValidateAddress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNonZeroAddress(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
address string
|
||||
expError bool
|
||||
}{
|
||||
{
|
||||
"empty string", "", true,
|
||||
},
|
||||
{
|
||||
"invalid address", "0x", true,
|
||||
},
|
||||
{
|
||||
"zero address", common.Address{}.String(), true,
|
||||
},
|
||||
{
|
||||
"valid address", tests.GenerateAddress().Hex(), false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
err := ValidateNonZeroAddress(tc.address)
|
||||
|
||||
if tc.expError {
|
||||
require.Error(t, err, tc.name)
|
||||
} else {
|
||||
require.NoError(t, err, tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeInt64(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
Loading…
Reference in New Issue
Block a user