mgocrud/validator.go

28 lines
606 B
Go

package mgocrud
import (
validator "gopkg.in/go-playground/validator.v8"
"gopkg.in/mgo.v2/bson"
)
// ValidateObject validates object via validator tag and custom method
func ValidateObject(db *Database, m ModelInterface, changes bson.M) error {
// first validate via struct tag
validator := validator.New(&validator.Config{
TagName: "validator",
})
err := validator.Struct(m)
if err != nil {
return err
}
// next execute custom model validator if exists
if i, ok := m.(interface {
Validate(db *Database, changes bson.M) error
}); ok {
return i.Validate(db, changes)
}
return nil
}