You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/pkg/app/form.go

49 lines
805 B

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
3 years ago
package app
import (
"strings"
"github.com/gin-gonic/gin"
)
type ValidError struct {
Message string
}
type ValidErrors []*ValidError
func (v *ValidError) Error() string {
return v.Message
}
func (v ValidErrors) Error() string {
return strings.Join(v.Errors(), ",")
}
func (v ValidErrors) Errors() []string {
var errs []string
for _, err := range v {
errs = append(errs, err.Error())
}
return errs
}
func BindAndValid(c *gin.Context, v any) (bool, ValidErrors) {
3 years ago
var errs ValidErrors
err := c.ShouldBind(v)
if err != nil {
errs = append(errs, &ValidError{
Message: err.Error(),
})
return false, errs
}
return true, nil
}