...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package apivalidator
17
18 import (
19 "regexp"
20 "time"
21
22 "github.com/go-playground/validator/v10"
23 "github.com/robfig/cron/v3"
24 )
25
26
27 func NameValid(fl validator.FieldLevel) bool {
28 name := fl.Field().String()
29 if name == "" {
30 return false
31 }
32
33 if len(name) > 63 {
34 return false
35 }
36
37 if !checkName(name) {
38 return false
39 }
40
41 return true
42 }
43
44 var namePattern = regexp.MustCompile(`^[-.\w]*$`)
45
46
47 func checkName(name string) bool {
48 return namePattern.MatchString(name)
49 }
50
51
52 func CronValid(fl validator.FieldLevel) bool {
53 cr := fl.Field().String()
54 if cr == "" {
55 return true
56 }
57
58 if _, err := cron.ParseStandard(cr); err != nil {
59 return false
60 }
61
62 return true
63 }
64
65
66 func DurationValid(fl validator.FieldLevel) bool {
67 dur := fl.Field().String()
68 if dur == "" {
69 return true
70 }
71
72 _, err := time.ParseDuration(dur)
73 return err == nil
74 }
75