...
1
9
10 package validate
11
12 import (
13 "fmt"
14 "regexp"
15 )
16
17 func (options *Options) ValidText() error {
18 if !options.Required && options.ValueText == "" {
19 return nil
20 }
21
22 regexPattern := options.Regex
23
24 regex, err := regexp.Compile(regexPattern)
25 if err != nil {
26 return err
27 }
28
29 isValid := regex.MatchString(options.ValueText)
30 if !isValid {
31 return fmt.Errorf("invalid type text: Text must be valid")
32 }
33
34 if options.Min == 0 && options.Max == 0 {
35 return nil
36 }
37
38 if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max {
39 return fmt.Errorf("invalid type text: Text must be between %d and %d characters", options.Min, options.Max)
40 }
41
42 return nil
43 }
44
View as plain text