v1.3.1
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package validators
|
||||
|
||||
import "github.com/go-playground/validator/v10"
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// protoValidator is used to validate Proto fields.
|
||||
func protoValidator(fe validator.FieldLevel) bool {
|
||||
@@ -203,3 +206,12 @@ func sepFieldTypeValidator(fe validator.FieldLevel) bool {
|
||||
|
||||
return StringInSlice(fieldValue, sepFieldTypeValues)
|
||||
}
|
||||
|
||||
// hwPathValidator is used to validate HWPath field.
|
||||
func hwPathValidator(fe validator.FieldLevel) bool {
|
||||
fieldValue := fe.Field().String()
|
||||
|
||||
ok, _ := regexp.MatchString(`^\b[0-9a-f]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}.\d{1}$`, fieldValue)
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
@@ -187,6 +187,10 @@ func errorMessage(fe validator.FieldError) string {
|
||||
fe.Field(),
|
||||
joinValues(sepFieldTypeValues))
|
||||
|
||||
case "hwPath":
|
||||
return fmt.Sprintf("%s %s must be in format 0000:1f:2b.0",
|
||||
prefix,
|
||||
fe.Field())
|
||||
}
|
||||
|
||||
return fe.Error()
|
||||
|
||||
@@ -159,5 +159,10 @@ func registerAllValidators(validate *validator.Validate) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = validate.RegisterValidation("hwPath", hwPathValidator)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user