providers/validate: Add more functions

This commit is contained in:
Sasha Koshka 2024-12-12 00:27:13 -05:00
parent a7e8c337db
commit 876c7d1a1b

View File

@ -1,6 +1,7 @@
package validate package validate
import "regexp" import "regexp"
import "unicode"
import "html/template" import "html/template"
import "git.tebibyte.media/sashakoshka/step" import "git.tebibyte.media/sashakoshka/step"
@ -17,6 +18,7 @@ func (this *Provider) Package () string {
return "validate" return "validate"
} }
// Configure fulfills the step.Configurable interface.
func (this *Provider) Configure (config step.Meta) error { func (this *Provider) Configure (config step.Meta) error {
emailRegexpText := config.Get("validate.email-regexp") emailRegexpText := config.Get("validate.email-regexp")
this.emailRegexp = nil this.emailRegexp = nil
@ -32,7 +34,9 @@ func (this *Provider) Configure (config step.Meta) error {
func (this *Provider) FuncMap () template.FuncMap { func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap { return template.FuncMap {
"isEmail": this.funcIsEmail, "isEmail": this.funcIsEmail,
"isPrint": this.funcIsPrint,
"inRange": this.funcInRange, "inRange": this.funcInRange,
"strInRange": this.funcStrInRange,
} }
} }
@ -42,6 +46,17 @@ func (this *Provider) funcIsEmail (input string) bool {
return emailRegexp.Match([]byte(input)) 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 { func (this *Provider) funcInRange (min, max, value int) bool {
return value >= min && value <= max return value >= min && value <= max
} }
func (this *Provider) funcStrInRange (min, max int, value string) bool {
return len(value) >= min && len(value) <= max
}