Skip to content

Commit

Permalink
validator
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Mar 15, 2024
1 parent d496217 commit 5ba160d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var Validators = map[*regexp.Regexp]func(match []string, value *generic.Value) error{
regexp.MustCompile("^alpha$"): alphaValidator,
regexp.MustCompile("^digit$"): digitValidator,
regexp.MustCompile("^alphanumeric$"): alphaNumericValidator,
regexp.MustCompile("^required$"): requiredValidator,
regexp.MustCompile("^email$"): emailValidator,
Expand All @@ -26,6 +27,20 @@ var Validators = map[*regexp.Regexp]func(match []string, value *generic.Value) e
regexp.MustCompile(`^ip$`): ipValidator,
}

func digitValidator(match []string, value *generic.Value) error {
var v = value.String()
if v == "" {
return nil
}

var r = regexp.MustCompile("^[0-9]+$")
if !r.MatchString(v) {
return fmt.Errorf("invalid digit value: %s", v)
}

return nil
}

func ipValidator(match []string, value *generic.Value) error {
var v = value.String()
if v == "" {
Expand Down

0 comments on commit 5ba160d

Please sign in to comment.