Skip to content

Commit

Permalink
Add MinLen and MaxLen
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Jul 6, 2017
1 parent d789db2 commit 5e82d0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
22 changes: 21 additions & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ const (
RuleLatitude = "latitude"
RuleLongitude = "longitude"
RuleMax = "max"
RuleMaxLen = "maxlen"
RuleMin = "min"
RuleMinLen = "minlen"
RuleNumber = "number"
RulePhone = "phone"
RuleRange = "range"
RuleRequired = "required"
)

func errRuleFormat(rule string, args []string) error {
return fmt.Errorf("check: cannot parse rule %q", rule+":"+strings.Join(args, ":"))
if len(args) > 0 {
rule += ":" + strings.Join(args, ":")
}
return fmt.Errorf("check: cannot parse rule %q", rule)
}

// A Checker contains keys with their checking rules.
Expand Down Expand Up @@ -94,8 +99,12 @@ func ruleCheck(errs Errors, rule, k, v string) {
args := ruleParts[1:]
if rule == RuleMax {
errs.Add(k, IsMax(v, parseRuleFloat64(rule, args))...)
} else if rule == RuleMaxLen {
errs.Add(k, IsMaxLen(v, parseRuleInt(rule, args))...)
} else if rule == RuleMin {
errs.Add(k, IsMin(v, parseRuleFloat64(rule, args))...)
} else if rule == RuleMinLen {
errs.Add(k, IsMinLen(v, parseRuleInt(rule, args))...)
} else if rule == RuleRange {
min, max := parseRuleFloat64Float64(rule, args)
errs.Add(k, IsInRange(v, min, max)...)
Expand Down Expand Up @@ -128,3 +137,14 @@ func parseRuleFloat64Float64(rule string, args []string) (float64, float64) {
}
return f1, f2
}

func parseRuleInt(rule string, args []string) int {
if len(args) != 1 {
panic(errRuleFormat(rule, args))
}
i, err := strconv.Atoi(args[0])
if err != nil {
panic(errRuleFormat(rule, args))
}
return i
}
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ var (
ErrInvalid = errors.New("invalid")
ErrMax = errors.New("max")
ErrMaxFileSize = errors.New("maxFileSize")
ErrMaxLength = errors.New("maxLength")
ErrMaxLen = errors.New("maxLen")
ErrMin = errors.New("min")
ErrMinFileSize = errors.New("minFileSize")
ErrMinLength = errors.New("minLength")
ErrMinLen = errors.New("minLen")
ErrNotAlpha = errors.New("notAlpha")
ErrNotEmail = errors.New("notEmail")
ErrNotFloat = errors.New("notFloat")
Expand Down
4 changes: 2 additions & 2 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func IsMax(v string, max float64) []error {
// IsMaxLen check if v length is below or equals max.
func IsMaxLen(v string, max int) []error {
if len(v) > max {
return []error{fmt.Errorf("%v:%v", ErrMaxLength, max)}
return []error{fmt.Errorf("%v:%v", ErrMaxLen, max)}
}
return nil
}
Expand All @@ -101,7 +101,7 @@ func IsMin(v string, min float64) []error {
// IsMinLen check if v length is over or equals min.
func IsMinLen(v string, min int) []error {
if len(v) < min {
return []error{fmt.Errorf("%v:%v", ErrMinLength, min)}
return []error{fmt.Errorf("%v:%v", ErrMinLen, min)}
}
return nil
}
Expand Down

0 comments on commit 5e82d0a

Please sign in to comment.