-
Notifications
You must be signed in to change notification settings - Fork 6
/
validators.go
49 lines (39 loc) · 1.42 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package aznum2words
import (
"regexp"
"strings"
)
//var validNumberRegex = regexp.MustCompile("^-?\\d+(\\.\\d+)?$")
var validateNumberRegex2 = regexp.MustCompile("^(-|\\+)?(([1-9][0-9]*)|(0))(?:\\.[0-9]+)?$")
func validateInput(numberAsStr string) error {
if ok := validateNumberRegex2.MatchString(numberAsStr); !ok {
return ErrInvalidArgument
}
return nil
}
// checkConstraints as name explains, intended to check predefined constraints for given input number
// basically checks whether given number satisfy the limitations for conversion
// There is two basic limitation:
// Max allowed precision for real number should be less or equal to 15.
// Max allowed number digits - only ones from left side of decimal point should be less or equal to 66.
func checkConstraints(numberAsStr string) error {
numberAsStr = removeSignMarkIfExists(numberAsStr)
isFloatingNumber := strings.Contains(numberAsStr, DecimalPointSeparator)
if !isFloatingNumber {
if len(numberAsStr) > MaxNumberDigitCount {
return ErrTooBigNumber
}
return nil
}
slices := strings.Split(numberAsStr, DecimalPointSeparator)
//check number of digits on the left side of decimal point
if len(slices[0]) > MaxNumberDigitCount {
return ErrTooBigNumber
}
//sanitize floatingPart by removing trailing zeros if exist
floatingPart := strings.TrimRight(slices[1], "0")
if len(floatingPart) > MaxNumberScaleCount {
return ErrTooBigScale
}
return nil
}