-
Notifications
You must be signed in to change notification settings - Fork 11
/
validation.go
73 lines (61 loc) · 2.31 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
package types
import (
"fmt"
"regexp"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
DoNotModify = "[do-not-modify]"
MinDenomLen = 3
MaxDenomLen = 128
MaxTokenURILen = 256
ReservedIBC = "ibc"
)
var (
// IsAlphaNumeric only accepts [a-z0-9]
IsAlphaNumeric = regexp.MustCompile(`^[a-z0-9]+$`).MatchString
// IsBeginWithAlpha only begin with [a-z]
IsBeginWithAlpha = regexp.MustCompile(`^[a-z].*`).MatchString
keywords = strings.Join([]string{ReservedIBC}, "|")
regexpKeywordsFmt = fmt.Sprintf("^(%s).*", keywords)
regexpKeyword = regexp.MustCompile(regexpKeywordsFmt).MatchString
)
// ValidateDenomID verifies whether the parameters are legal
func ValidateDenomID(denomID string) error {
if len(denomID) < MinDenomLen || len(denomID) > MaxDenomLen {
return sdkerrors.Wrapf(ErrInvalidDenom, "the length of denom(%s) only accepts value [%d, %d]", denomID, MinDenomLen, MaxDenomLen)
}
if !IsBeginWithAlpha(denomID) || !IsAlphaNumeric(denomID) {
return sdkerrors.Wrapf(ErrInvalidDenom, "the denom(%s) only accepts alphanumeric characters, and begin with an english letter", denomID)
}
return nil
}
// ValidateTokenID verify that the tokenID is legal
func ValidateTokenID(tokenID string) error {
if len(tokenID) < MinDenomLen || len(tokenID) > MaxDenomLen {
return sdkerrors.Wrapf(ErrInvalidTokenID, "the length of nft id(%s) only accepts value [%d, %d]", tokenID, MinDenomLen, MaxDenomLen)
}
if !IsBeginWithAlpha(tokenID) || !IsAlphaNumeric(tokenID) {
return sdkerrors.Wrapf(ErrInvalidTokenID, "nft id(%s) only accepts alphanumeric characters, and begin with an english letter", tokenID)
}
return nil
}
// ValidateTokenURI verify that the tokenURI is legal
func ValidateTokenURI(tokenURI string) error {
if len(tokenURI) > MaxTokenURILen {
return sdkerrors.Wrapf(ErrInvalidTokenURI, "the length of nft uri(%s) only accepts value [0, %d]", tokenURI, MaxTokenURILen)
}
return nil
}
// Modified returns whether the field is modified
func Modified(target string) bool {
return target != DoNotModify
}
// ValidateKeywords checks if the given denomID begins with `DenomKeywords`
func ValidateKeywords(denomID string) error {
if regexpKeyword(denomID) {
return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denomID: %s, can not begin with keyword: (%s)", denomID, keywords)
}
return nil
}