...
1
9
10 package validate
11
12 import (
13 "fmt"
14 "regexp"
15 )
16
17 func (options *Options) ValidNumber() error {
18 if !options.Required && options.ValueNumber == 0 {
19 return nil
20 }
21
22 if options.Min == 0 && options.Max == 0 {
23 return nil
24 }
25
26 if options.ValueNumber < int64(options.Min) || options.ValueNumber > int64(options.Max) {
27 return fmt.Errorf("invalid type number: Number must be between %d and %d", options.Min, options.Max)
28 }
29
30 return nil
31 }
32
33 func (options *Options) ValidTextNumber() error {
34 if !options.Required && options.ValueText == "" {
35 return nil
36 }
37
38 regexPattern := "^[0-9]+$"
39
40 regex, err := regexp.Compile(regexPattern)
41 if err != nil {
42 return err
43 }
44
45 isValid := regex.MatchString(options.ValueText)
46 if !isValid {
47 return fmt.Errorf("invalid type text number: Text Number must be valid")
48 }
49
50 if options.Min == 0 && options.Max == 0 {
51 return nil
52 }
53
54 if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max {
55 return fmt.Errorf("invalid type text number: Text Number must be between %d and %d characters", options.Min, options.Max)
56 }
57
58 return nil
59 }
60
61 func (options *Options) ValidFload() error {
62 if !options.Required && options.ValueFloat == 0 {
63 return nil
64 }
65
66 if options.ValueFloat < float32(options.Min) || options.ValueFloat > float32(options.Max) {
67 return fmt.Errorf("invalid type float: Number must be between %f and %f", options.MinFloat, options.MaxFloat)
68 }
69
70 return nil
71 }
72
73 func (options *Options) ValidDouble() error {
74 if !options.Required && options.ValueDouble == 0 {
75 return nil
76 }
77
78 if options.ValueDouble < float64(options.Min) || options.ValueDouble > float64(options.Max) {
79 return fmt.Errorf("invalid type double: Number must be between %f and %f", options.MinFloat, options.MaxFloat)
80 }
81
82 return nil
83 }
84
View as plain text