This commit is contained in:
2023-04-20 11:17:35 +03:00
parent 84b64b7d80
commit 7d6cda7119
40 changed files with 1337 additions and 126 deletions

View File

@@ -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
}

View File

@@ -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()

View File

@@ -159,5 +159,10 @@ func registerAllValidators(validate *validator.Validate) error {
return err
}
err = validate.RegisterValidation("hwPath", hwPathValidator)
if err != nil {
return err
}
return nil
}