From 42d9020832beb50e083363576656b4a3bcfde2b7 Mon Sep 17 00:00:00 2001 From: derin Date: Thu, 15 Sep 2022 14:24:30 +0200 Subject: [PATCH 1/3] add variable sanitizer --- manipulate/sanitize.go | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/manipulate/sanitize.go b/manipulate/sanitize.go index 7a420ea..102785f 100644 --- a/manipulate/sanitize.go +++ b/manipulate/sanitize.go @@ -10,32 +10,56 @@ import ( "gopkg.in/guregu/null.v4" ) -var ( - Sanitizer = bluemonday.StrictPolicy() +type ( + Sanitizer struct { + policy *bluemonday.Policy + } + + Kind int +) + +const ( + Strict Kind = iota + 1 + UGC +) +var ( spaceRegex = regexp.MustCompile(`\s+`) nidRegex = regexp.MustCompile(`^[A-Z]{3}\d{7}$`) ) var ErrNIDInvalid = errors.New("nid_invalid") -func Sanitize(s *string) { - *s = strings.TrimSpace(Sanitizer.Sanitize(*s)) +func NewSanitizer(kind Kind) *Sanitizer { + var policy *bluemonday.Policy + + switch kind { + case UGC: + policy = bluemonday.UGCPolicy() + default: + policy = bluemonday.StrictPolicy() + } + + return &Sanitizer{policy: policy} +} + +func (p *Sanitizer) Sanitize(s *string) { + *s = strings.TrimSpace(p.policy.Sanitize(*s)) } -func SanitizeNull(s *null.String) { - s.String = strings.TrimSpace(Sanitizer.Sanitize(s.String)) +func (p *Sanitizer) SanitizeNull(s *null.String) { + s.String = strings.TrimSpace(p.policy.Sanitize(s.String)) } -func SanitizeIBAN(s *null.String) { +func (p *Sanitizer) SanitizeIBAN(s *null.String) { s.String = spaceRegex.ReplaceAllString(strings.TrimSpace(strings.ToUpper(s.String)), "") } -func SanitizeVAT(s *null.String) { +func (p *Sanitizer) SanitizeVAT(s *null.String) { s.String = spaceRegex.ReplaceAllString(strings.TrimSpace(strings.ToUpper(s.String)), "") } -func SanitizeNID(s *string) error { +func (p *Sanitizer) SanitizeNID(s *string) error { *s = strings.TrimSpace(strings.ToUpper(*s)) if !nidRegex.MatchString(*s) { From 114a4653a889729e068f71e3d3c597edc4580287 Mon Sep 17 00:00:00 2001 From: derin Date: Thu, 15 Sep 2022 14:25:36 +0200 Subject: [PATCH 2/3] remove receiver --- manipulate/sanitize.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manipulate/sanitize.go b/manipulate/sanitize.go index 102785f..bb8d2c5 100644 --- a/manipulate/sanitize.go +++ b/manipulate/sanitize.go @@ -51,15 +51,15 @@ func (p *Sanitizer) SanitizeNull(s *null.String) { s.String = strings.TrimSpace(p.policy.Sanitize(s.String)) } -func (p *Sanitizer) SanitizeIBAN(s *null.String) { +func SanitizeIBAN(s *null.String) { s.String = spaceRegex.ReplaceAllString(strings.TrimSpace(strings.ToUpper(s.String)), "") } -func (p *Sanitizer) SanitizeVAT(s *null.String) { +func SanitizeVAT(s *null.String) { s.String = spaceRegex.ReplaceAllString(strings.TrimSpace(strings.ToUpper(s.String)), "") } -func (p *Sanitizer) SanitizeNID(s *string) error { +func SanitizeNID(s *string) error { *s = strings.TrimSpace(strings.ToUpper(*s)) if !nidRegex.MatchString(*s) { From 3a1b859b7418483a628be55b1eadfafd66997d8c Mon Sep 17 00:00:00 2001 From: derin Date: Thu, 15 Sep 2022 14:33:40 +0200 Subject: [PATCH 3/3] global policies --- manipulate/sanitize.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/manipulate/sanitize.go b/manipulate/sanitize.go index bb8d2c5..ffe99ce 100644 --- a/manipulate/sanitize.go +++ b/manipulate/sanitize.go @@ -24,8 +24,10 @@ const ( ) var ( - spaceRegex = regexp.MustCompile(`\s+`) - nidRegex = regexp.MustCompile(`^[A-Z]{3}\d{7}$`) + ugcPolicy = bluemonday.UGCPolicy() + strictPolicy = bluemonday.StrictPolicy() + spaceRegex = regexp.MustCompile(`\s+`) + nidRegex = regexp.MustCompile(`^[A-Z]{3}\d{7}$`) ) var ErrNIDInvalid = errors.New("nid_invalid") @@ -35,9 +37,9 @@ func NewSanitizer(kind Kind) *Sanitizer { switch kind { case UGC: - policy = bluemonday.UGCPolicy() + policy = ugcPolicy default: - policy = bluemonday.StrictPolicy() + policy = strictPolicy } return &Sanitizer{policy: policy}