Skip to content

Commit

Permalink
x/ibc/24-host: fix some comments (#7923)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 13, 2020
1 parent 6f5eaba commit 15103b6
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions x/ibc/core/24-host/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,37 @@ func defaultIdentifierValidator(id string, min, max int) error { //nolint:unpara
}

// ClientIdentifierValidator is the default validator function for Client identifiers.
// A valid Identifier must be between 9-64 characters and only contain lowercase
// alphabetic characters.
// A valid Identifier must be between 9-64 characters and only contain alphanumeric and some allowed
// special characters (see IsValidID).
func ClientIdentifierValidator(id string) error {
return defaultIdentifierValidator(id, 9, DefaultMaxCharacterLength)
}

// ConnectionIdentifierValidator is the default validator function for Connection identifiers.
// A valid Identifier must be between 10-64 characters and only contain lowercase
// alphabetic characters.
// A valid Identifier must be between 10-64 characters and only contain alphanumeric and some allowed
// special characters (see IsValidID).
func ConnectionIdentifierValidator(id string) error {
return defaultIdentifierValidator(id, 10, DefaultMaxCharacterLength)
}

// ChannelIdentifierValidator is the default validator function for Channel identifiers.
// A valid Identifier must be between 10-64 characters and only contain lowercase
// alphabetic characters.
// A valid Identifier must be between 10-64 characters and only contain alphanumeric and some allowed
// special characters (see IsValidID).
func ChannelIdentifierValidator(id string) error {
return defaultIdentifierValidator(id, 10, DefaultMaxCharacterLength)
}

// PortIdentifierValidator is the default validator function for Port identifiers.
// A valid Identifier must be between 2-64 characters and only contain lowercase
// alphabetic characters.
// A valid Identifier must be between 2-64 characters and only contain alphanumeric and some allowed
// special characters (see IsValidID).
func PortIdentifierValidator(id string) error {
return defaultIdentifierValidator(id, 2, DefaultMaxCharacterLength)
}

// NewPathValidator takes in a Identifier Validator function and returns
// a Path Validator function which requires path only has valid identifiers
// alphanumeric character strings, and "/" separators
// a Path Validator function which requires path to consist of `/`-separated valid identifiers,
// where a valid identifier is between 1-64 characters, contains only alphanumeric and some allowed
// special characters (see IsValidID), and satisfies the custom `idValidator` function.
func NewPathValidator(idValidator ValidateFn) ValidateFn {
return func(path string) error {
pathArr := strings.Split(path, "/")
Expand All @@ -112,25 +113,13 @@ func NewPathValidator(idValidator ValidateFn) ValidateFn {
}
}

// PathValidator takes in path string and validates with default identifier rules.
// This is optimized by simply checking that all path elements are alphanumeric.
// PathValidator takes in path string and validates with default identifier rules:
// path consists of `/`-separated valid identifiers,
// each identifier is between 1-64 characters and contains only alphanumeric and some allowed
// special characters (see IsValidID).
func PathValidator(path string) error {
pathArr := strings.Split(path, "/")
if len(pathArr) > 0 && pathArr[0] == path {
return sdkerrors.Wrapf(ErrInvalidPath, "path %s doesn't contain any separator '/'", path)
}

for _, p := range pathArr {
// a path beginning or ending in a separator returns empty string elements.
if p == "" {
return sdkerrors.Wrapf(ErrInvalidPath, "path %s cannot begin or end with '/'", path)
}

// Each path element must be a valid identifier or constant number
if err := defaultIdentifierValidator(p, 1, DefaultMaxCharacterLength); err != nil {
return sdkerrors.Wrapf(err, "path %s contains an invalid identifier: '%s'", path, p)
}
}

return nil
f := NewPathValidator(func(path string) error {
return nil
})
return f(path)
}

0 comments on commit 15103b6

Please sign in to comment.