mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-24 14:35:53 +00:00
Fix validate amount regex
This commit is contained in:
parent
205573d217
commit
97c7e8e9e4
@ -2,7 +2,6 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
@ -13,7 +12,7 @@ import (
|
|||||||
*/
|
*/
|
||||||
func ValidateAmountFormat(amount string) error {
|
func ValidateAmountFormat(amount string) error {
|
||||||
// Check whether it's an integer, or a float with max 8 digits
|
// Check whether it's an integer, or a float with max 8 digits
|
||||||
match, err := regexp.MatchString("^\\d{1,19}(.\\d{0,8})?$", amount)
|
match, err := regexp.MatchString("^([1-9]\\d{0,11}|0)(\\.\\d{0,8})?$", amount)
|
||||||
|
|
||||||
if !match {
|
if !match {
|
||||||
return errors.Errorf("Invalid send amount")
|
return errors.Errorf("Invalid send amount")
|
||||||
@ -23,12 +22,5 @@ func ValidateAmountFormat(amount string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it parses properly, then this is valid
|
|
||||||
_, err = strconv.ParseFloat(amount, 64)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,10 @@ func TestValidateAmountFormat(t *testing.T) {
|
|||||||
"1.0",
|
"1.0",
|
||||||
"0.1",
|
"0.1",
|
||||||
"0.12345678",
|
"0.12345678",
|
||||||
|
"111111111111.11111111", // 12 digits to the left of decimal, 8 digits to the right
|
||||||
|
"184467440737.09551615", // Maximum input that can be represented in sompi later
|
||||||
|
"184467440737.09551616", // Cannot be represented in sompi, but we'll acccept for "correct format"
|
||||||
|
"999999999999.99999999", // Cannot be represented in sompi, but we'll acccept for "correct format"
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range validCases {
|
for _, testCase := range validCases {
|
||||||
@ -27,6 +31,14 @@ func TestValidateAmountFormat(t *testing.T) {
|
|||||||
"-1",
|
"-1",
|
||||||
"0.123456789", // 9 decimal digits
|
"0.123456789", // 9 decimal digits
|
||||||
".1", // decimal but no integer component
|
".1", // decimal but no integer component
|
||||||
|
"0a", // Extra character
|
||||||
|
"0000000000000", // 13 zeros
|
||||||
|
"012", // Int padded with zero
|
||||||
|
"00.1", // Decimal padded with zeros
|
||||||
|
"111111111111111111111", // all digits
|
||||||
|
"111111111111A11111111", // non-period/non-digit where decimal would be
|
||||||
|
"000000000000.00000000", // all zeros
|
||||||
|
"kaspa", // all text
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range invalidCases {
|
for _, testCase := range invalidCases {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user