providers/validate: add isEnglish, hasWords, and hasXML functions
This commit is contained in:
parent
8b00321812
commit
2293780131
@ -5,3 +5,6 @@ import "regexp"
|
|||||||
// see:
|
// see:
|
||||||
// https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
|
// https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
|
||||||
var defaultEmailRegexp = regexp.MustCompile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
|
var defaultEmailRegexp = regexp.MustCompile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
|
||||||
|
|
||||||
|
// looks for XML tags, this will throw false positives but not that many.
|
||||||
|
var defaultXMLRegexp = regexp.MustCompile("<.*\\w.*>")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package validate
|
package validate
|
||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
import "strings"
|
||||||
import "unicode"
|
import "unicode"
|
||||||
import "html/template"
|
import "html/template"
|
||||||
import "git.tebibyte.media/sashakoshka/step"
|
import "git.tebibyte.media/sashakoshka/step"
|
||||||
@ -11,6 +12,7 @@ var _ step.Configurable = new(Provider)
|
|||||||
// Provider provides validation functions.
|
// Provider provides validation functions.
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
emailRegexp *regexp.Regexp
|
emailRegexp *regexp.Regexp
|
||||||
|
xmlRegexp *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package fulfills the step.Provider interface.
|
// Package fulfills the step.Provider interface.
|
||||||
@ -27,6 +29,13 @@ func (this *Provider) Configure (config step.Meta) error {
|
|||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
this.emailRegexp = emailRegexp
|
this.emailRegexp = emailRegexp
|
||||||
}
|
}
|
||||||
|
xmlRegexpText := config.Get("validate.email-regexp")
|
||||||
|
this.xmlRegexp = nil
|
||||||
|
if xmlRegexpText != "" {
|
||||||
|
xmlRegexp, err := regexp.Compile(xmlRegexpText)
|
||||||
|
if err != nil { return err }
|
||||||
|
this.xmlRegexp = xmlRegexp
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,8 +44,10 @@ func (this *Provider) FuncMap () template.FuncMap {
|
|||||||
return template.FuncMap {
|
return template.FuncMap {
|
||||||
"isEmail": this.funcIsEmail,
|
"isEmail": this.funcIsEmail,
|
||||||
"isPrint": this.funcIsPrint,
|
"isPrint": this.funcIsPrint,
|
||||||
|
"isEnglish": this.funcIsEnglish,
|
||||||
"inRange": this.funcInRange,
|
"inRange": this.funcInRange,
|
||||||
"strInRange": this.funcStrInRange,
|
"strInRange": this.funcStrInRange,
|
||||||
|
"hasWords": this.funcHasWords,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +64,20 @@ func (this *Provider) funcIsPrint (input string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Provider) funcIsEnglish(value string) bool {
|
||||||
|
// TODO: make this shit better
|
||||||
|
score := 0
|
||||||
|
total := 0
|
||||||
|
for _, char := range value {
|
||||||
|
if char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z' || char >= '0' || char <= '9' {
|
||||||
|
score += 1
|
||||||
|
}
|
||||||
|
total += 1
|
||||||
|
}
|
||||||
|
// 3/4 of the text must be alphanumeric
|
||||||
|
return score > total * 3 / 4
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@ -60,3 +85,19 @@ func (this *Provider) funcInRange (min, max, value int) bool {
|
|||||||
func (this *Provider) funcStrInRange (min, max int, value string) bool {
|
func (this *Provider) funcStrInRange (min, max int, value string) bool {
|
||||||
return len(value) >= min && len(value) <= max
|
return len(value) >= min && len(value) <= max
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Provider) funcHasWords(words []string, value string) bool {
|
||||||
|
lowerValue := strings.ToLower(value)
|
||||||
|
for _, word := range words {
|
||||||
|
if strings.Contains(lowerValue, word) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Provider) funcHasXML(value string) bool {
|
||||||
|
xmlRegexp := this.xmlRegexp
|
||||||
|
if xmlRegexp == nil { xmlRegexp = defaultXMLRegexp }
|
||||||
|
return xmlRegexp.Match([]byte(value))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user