-
Notifications
You must be signed in to change notification settings - Fork 30
/
validation.go
110 lines (93 loc) · 2.64 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
100
101
102
103
104
105
106
107
108
109
110
package token
import (
"fmt"
"regexp"
"unicode/utf8"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/x/token/class"
)
const (
maxName = 20
maxURI = 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 ErrInvalidTokenName.Wrap("name cannot be empty")
} else if !stringInSize(name, maxName) {
return ErrInvalidNameLength.Wrapf("name cannot be longer than %d", maxName)
}
return nil
}
func validateSymbol(symbol string) error {
if !reSymbol.MatchString(symbol) {
return ErrInvalidTokenSymbol.Wrapf("invalid symbol: %s, valid expression is: %s", symbol, reSymbolString)
}
return nil
}
func validateURI(uri string) error {
if !stringInSize(uri, maxURI) {
return ErrInvalidImageURILength.Wrapf("uri cannot be longer than %d", maxURI)
}
return nil
}
func validateMeta(meta string) error {
if !stringInSize(meta, maxMeta) {
return ErrInvalidMetaLength.Wrapf("meta cannot be longer than %d", maxMeta)
}
return nil
}
func validateDecimals(decimals int32) error {
if decimals < 0 || decimals > 18 {
return ErrInvalidTokenDecimals.Wrapf("invalid decimals: %d", decimals)
}
return nil
}
func validateAmount(amount sdk.Int) error {
if !amount.IsPositive() {
return ErrInvalidAmount.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.ErrInvalidPermission.Wrap(permission.String())
}
return nil
}
func validateChange(change Attribute) error {
validators := map[string]func(string) error{
AttributeKeyName.String(): validateName,
AttributeKeyImageURI.String(): validateURI,
AttributeKeyMeta.String(): validateMeta,
AttributeKeyURI.String(): validateURI,
}
validator, ok := validators[change.Key]
if !ok {
return ErrInvalidChangesField.Wrapf("invalid field of key: %s", change.Key)
}
return validator(change.Value)
}
func ValidateContractID(id string) error {
return class.ValidateID(id)
}
func canonicalKey(key string) string {
convert := map[string]string{
AttributeKeyImageURI.String(): AttributeKeyURI.String(),
}
if converted, ok := convert[key]; ok {
return converted
}
return key
}