forked from Finschia/finschia-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
99 lines (83 loc) · 2.46 KB
/
validation.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package token
import (
"fmt"
"regexp"
"unicode/utf8"
sdk "github.com/line/lbm-sdk/types"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/x/token/class"
)
const (
maxName = 20
maxImageURI = 1000
maxMeta = 1000
)
var (
reSymbolString = `[A-Z][A-Z0-9]{1,4}`
reSymbol = regexp.MustCompile(fmt.Sprintf(`^%s$`, reSymbolString))
)
func stringInSize(str string, size int) bool {
return utf8.RuneCountInString(str) <= size
}
func validateName(name string) error {
if len(name) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("name cannot be empty")
} else if !stringInSize(name, maxName) {
return sdkerrors.ErrInvalidRequest.Wrapf("name cannot be longer than %d", maxName)
}
return nil
}
func validateSymbol(symbol string) error {
if !reSymbol.MatchString(symbol) {
return sdkerrors.ErrInvalidRequest.Wrapf("invalid symbol: %s, valid expression is: %s", symbol, reSymbolString)
}
return nil
}
func validateImageURI(uri string) error {
if !stringInSize(uri, maxImageURI) {
return sdkerrors.ErrInvalidRequest.Wrapf("image_uri cannot be longer than %d", maxImageURI)
}
return nil
}
func validateMeta(meta string) error {
if !stringInSize(meta, maxMeta) {
return sdkerrors.ErrInvalidRequest.Wrapf("meta cannot be longer than %d", maxMeta)
}
return nil
}
func validateDecimals(decimals int32) error {
if decimals < 0 || decimals > 18 {
return sdkerrors.ErrInvalidRequest.Wrapf("invalid decimals: %d", decimals)
}
return nil
}
func validateAmount(amount sdk.Int) error {
if !amount.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrapf("amount must be positive: %s", amount)
}
return nil
}
func validateLegacyPermission(permission string) error {
return ValidatePermission(Permission(LegacyPermissionFromString(permission)))
}
func ValidatePermission(permission Permission) error {
if p := Permission_value[Permission_name[int32(permission)]]; p == 0 {
return sdkerrors.ErrInvalidRequest.Wrapf("invalid permission: %s", permission)
}
return nil
}
func validateChange(change Pair) error {
validators := map[string]func(string) error{
AttributeKeyName.String(): validateName,
AttributeKeyImageURI.String(): validateImageURI,
AttributeKeyMeta.String(): validateMeta,
}
validator, ok := validators[change.Field]
if !ok {
return sdkerrors.ErrInvalidRequest.Wrapf("invalid field: %s", change.Field)
}
return validator(change.Value)
}
func ValidateContractID(id string) error {
return class.ValidateID(id)
}