29 lines
637 B
Go
29 lines
637 B
Go
package mgocrud
|
|
|
|
import (
|
|
validator "gopkg.in/go-playground/validator.v8"
|
|
mgo "gopkg.in/mgo.v2"
|
|
"gopkg.in/mgo.v2/bson"
|
|
)
|
|
|
|
// ValidateObject validates object via validator tag and custom method
|
|
func ValidateObject(db *mgo.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 *mgo.Database, changes bson.M) error
|
|
}); ok {
|
|
return i.Validate(db, changes)
|
|
}
|
|
|
|
return nil
|
|
}
|