Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

37 linhas
930 B

  1. // Package validation uses github.com/go-playground/validator/v10 for API request validation.
  2. package validation
  3. import (
  4. "fmt"
  5. "github.com/go-playground/validator/v10"
  6. )
  7. var defaultValidator = validator.New()
  8. // Struct validates s and returns the first validation error as a readable message, or nil.
  9. func Struct(s interface{}) error {
  10. err := defaultValidator.Struct(s)
  11. if err == nil {
  12. return nil
  13. }
  14. if errs, ok := err.(validator.ValidationErrors); ok && len(errs) > 0 {
  15. fe := errs[0]
  16. return fmt.Errorf("%s: %s", fe.Field(), fe.Tag())
  17. }
  18. return err
  19. }
  20. // Var validates a single value with the given tag (e.g. "required") and returns an error or nil.
  21. func Var(field interface{}, tag string) error {
  22. err := defaultValidator.Var(field, tag)
  23. if err == nil {
  24. return nil
  25. }
  26. if errs, ok := err.(validator.ValidationErrors); ok && len(errs) > 0 {
  27. fe := errs[0]
  28. return fmt.Errorf("%s", fe.Tag())
  29. }
  30. return err
  31. }