/
regex.go
55 lines (46 loc) · 1.25 KB
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package validators
import (
"context"
"regexp"
"flamingo.me/flamingo/core/form2/domain"
"gopkg.in/go-playground/validator.v9"
)
type (
// RegexValidator defines regex validator. Name and regex pattern is passed to fresh instance during creation of validator.
//
// validator := NewRegexValidator("postcode", "^[0-9]{5}$")
//
// ...
//
// Data struct {
// PostCode string `validate:"postcode"`
// }
//
RegexValidator struct {
name string
regex *regexp.Regexp
}
)
var _ domain.FieldValidator = &RegexValidator{}
// NewRegexValidator creates new instance of RegexValidator by defining it's tag name and regex pattern
func NewRegexValidator(name string, regex string) *RegexValidator {
return &RegexValidator{
name: name,
regex: regexp.MustCompile(regex),
}
}
// ValidatorName defines tag name of regex validator
func (v *RegexValidator) ValidatorName() string {
return v.name
}
// ValidateField validates string if match right regex. Valid if string is empty or match defined regex pattern.
func (v *RegexValidator) ValidateField(_ context.Context, fl validator.FieldLevel) bool {
converted, ok := fl.Field().Interface().(string)
if !ok {
return false
}
if len(converted) == 0 {
return true
}
return v.regex.MatchString(converted)
}