-
Notifications
You must be signed in to change notification settings - Fork 349
/
validation.go
83 lines (70 loc) · 2.74 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
// Copyright (c) 2016-2021 Shanghai Bianjie AI Technology Inc. (licensed under the Apache License, Version 2.0)
// Modifications Copyright (c) 2021-present Crypto.org (licensed under the Apache License, Version 2.0)
package types
import (
"regexp"
"strings"
sdkerrors "cosmossdk.io/errors"
)
const (
DoNotModify = "[do-not-modify]"
MinDenomLen = 3
MaxDenomLen = 64
IBCDenomLen = 68
MaxTokenURILen = 256
)
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
)
// 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 lowercase alphanumeric characters, and begin with an english letter", denomID)
}
return nil
}
// ValidateDenomIDWithIBC verifies whether the parameters are legal and considers IBC denom IDs when checking
func ValidateDenomIDWithIBC(denomID string) error {
if strings.HasPrefix(denomID, "ibc/") {
if len(denomID) != IBCDenomLen {
return sdkerrors.Wrapf(ErrInvalidDenom, "the length of ibc denom(%s) only accepts value [%d]", denomID, IBCDenomLen)
}
return nil
}
return ValidateDenomID(denomID)
}
// ValidateDenomName verifies whether the parameters are legal
func ValidateDenomName(denomName string) error {
denomName = strings.TrimSpace(denomName)
if len(denomName) == 0 {
return sdkerrors.Wrapf(ErrInvalidDenomName, "denom name(%s) can not be space", denomName)
}
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 lowercase 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
}