Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into develop
  • Loading branch information
mpraski committed Sep 16, 2022
2 parents 3748c68 + c183b41 commit 984515e
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions manipulate/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,47 @@ import (
"gopkg.in/guregu/null.v4"
)

var (
Sanitizer = bluemonday.StrictPolicy()
type (
Sanitizer struct {
policy *bluemonday.Policy
}

Kind int
)

const (
Strict Kind = iota + 1
UGC
)

spaceRegex = regexp.MustCompile(`\s+`)
nidRegex = regexp.MustCompile(`^[A-Z]{3}\d{7}$`)
var (
ugcPolicy = bluemonday.UGCPolicy()
strictPolicy = bluemonday.StrictPolicy()
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 = ugcPolicy
default:
policy = 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) {
Expand Down

0 comments on commit 984515e

Please sign in to comment.