joshuar-go-hass-agent/internal/validation/validation.go

41 lines
832 B
Go

// Copyright 2024 Joshua Rich <joshua.rich@gmail.com>.
// SPDX-License-Identifier: MIT
package validation
import (
"strings"
"github.com/go-playground/validator/v10"
)
var Validate *validator.Validate
func init() {
Validate = validator.New(validator.WithRequiredStructEnabled())
}
//nolint:errorlint
//revive:disable:unhandled-error
func ParseValidationErrors(validation error) string {
validationErrs, ok := validation.(validator.ValidationErrors)
if !ok {
return "internal validation error"
}
var message strings.Builder
for _, err := range validationErrs {
switch err.Tag() {
case "required":
message.WriteString("field " + err.Field() + " is required")
default:
message.WriteString("field " + err.Field() + " should match " + err.Tag())
}
message.WriteRune(' ')
}
return message.String()
}