From 5ba160d284689485ce604580f69a91785f354849 Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 15 Mar 2024 20:00:06 +0100 Subject: [PATCH] validator --- lib/validation/validators.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/validation/validators.go b/lib/validation/validators.go index 5b23948..9a1a708 100644 --- a/lib/validation/validators.go +++ b/lib/validation/validators.go @@ -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, @@ -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 == "" {