From 876c7d1a1b4b41190690b3fe2a92cd419c7cfed7 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 12 Dec 2024 00:27:13 -0500 Subject: [PATCH] providers/validate: Add more functions --- providers/validate/validate.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/providers/validate/validate.go b/providers/validate/validate.go index 3cc9994..978fce9 100644 --- a/providers/validate/validate.go +++ b/providers/validate/validate.go @@ -1,6 +1,7 @@ package validate import "regexp" +import "unicode" import "html/template" import "git.tebibyte.media/sashakoshka/step" @@ -17,6 +18,7 @@ func (this *Provider) Package () string { return "validate" } +// Configure fulfills the step.Configurable interface. func (this *Provider) Configure (config step.Meta) error { emailRegexpText := config.Get("validate.email-regexp") this.emailRegexp = nil @@ -31,8 +33,10 @@ func (this *Provider) Configure (config step.Meta) error { // FuncMap fulfills the step.FuncProvider interface. func (this *Provider) FuncMap () template.FuncMap { return template.FuncMap { - "isEmail": this.funcIsEmail, - "inRange": this.funcInRange, + "isEmail": this.funcIsEmail, + "isPrint": this.funcIsPrint, + "inRange": this.funcInRange, + "strInRange": this.funcStrInRange, } } @@ -42,6 +46,17 @@ func (this *Provider) funcIsEmail (input string) bool { return emailRegexp.Match([]byte(input)) } +func (this *Provider) funcIsPrint (input string) bool { + for _, run := range input { + if !unicode.IsPrint(run) { return false } + } + return true +} + func (this *Provider) funcInRange (min, max, value int) bool { return value >= min && value <= max } + +func (this *Provider) funcStrInRange (min, max int, value string) bool { + return len(value) >= min && len(value) <= max +}