-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
96 lines (73 loc) · 3.24 KB
/
validation.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ocmf_go
import (
"regexp"
"github.com/go-playground/validator/v10"
)
var messageValidator = validator.New()
func init() {
// Register custom validators for the validator
must(messageValidator.RegisterValidation("meterError", meterErrorValidator))
must(messageValidator.RegisterValidation("userAssignmentState", userAssignmentStateValidator))
must(messageValidator.RegisterValidation("pagination", paginationValidator))
must(messageValidator.RegisterValidation("iso5118State", iso5118StateValidator))
must(messageValidator.RegisterValidation("ocppState", ocppStateValidator))
must(messageValidator.RegisterValidation("rfidState", rfidStateValidator))
must(messageValidator.RegisterValidation("chargePointAssignment", chargePointAssignmentValidator))
must(messageValidator.RegisterValidation("timeStatus", timeStatusValidatorValidator))
must(messageValidator.RegisterValidation("unit", unitValidator))
must(messageValidator.RegisterValidation("currentType", currentTypeValidator))
must(messageValidator.RegisterValidation("iso8601", iso8601WithMillisValidator))
}
func must(err error) {
if err != nil {
panic(err)
}
}
func meterErrorValidator(fl validator.FieldLevel) bool {
return isValidMeterError(MeterError(fl.Field().String()))
}
func userAssignmentStateValidator(fl validator.FieldLevel) bool {
return isValidUserAssignmentState(UserAssignmentState(fl.Field().String()))
}
var indicatorNumberRegex = regexp.MustCompile(`^[TF]\d+$`)
func paginationValidator(fl validator.FieldLevel) bool {
return indicatorNumberRegex.MatchString(fl.Field().String())
}
func iso5118StateValidator(fl validator.FieldLevel) bool {
return isValidISO15118State(ISO15118State(fl.Field().String()))
}
func ocppStateValidator(fl validator.FieldLevel) bool {
return isValidOcppState(OcppState(fl.Field().String()))
}
func rfidStateValidator(fl validator.FieldLevel) bool {
return isValidRfidState(RfidState(fl.Field().String()))
}
func chargePointAssignmentValidator(fl validator.FieldLevel) bool {
return isValidChargePointAssignmentType(ChargePointAssignmentType(fl.Field().String()))
}
func timeStatusValidatorValidator(fl validator.FieldLevel) bool {
return isValidTimeStatus(TimeStatus(fl.Field().String()))
}
func unitValidator(fl validator.FieldLevel) bool {
return isValidUnit(Units(fl.Field().String()))
}
func currentTypeValidator(fl validator.FieldLevel) bool {
return isValidCurrentType(CurrentType(fl.Field().String()))
}
var iso8601WithMillisRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2},\d{3}[+-]\d{4} [S|U|I|R]$`)
func iso8601WithMillisValidator(fl validator.FieldLevel) bool {
return iso8601WithMillisRegex.MatchString(fl.Field().String())
}
// Signature validation
var signatureValidator = validator.New()
func init() {
// Register custom validators for the validator
must(signatureValidator.RegisterValidation("signatureAlgorithm", signatureAlgorithmValidator))
must(signatureValidator.RegisterValidation("signatureEncoding", signatureEncodingValidator))
}
func signatureEncodingValidator(fl validator.FieldLevel) bool {
return isValidSignatureEncoding(SignatureEncoding(fl.Field().String()))
}
func signatureAlgorithmValidator(fl validator.FieldLevel) bool {
return isValidSignatureAlgorithm(SignatureAlgorithm(fl.Field().String()))
}