2018-03-20 14:16:01 +01:00
|
|
|
package mgocrud
|
|
|
|
|
|
|
|
import (
|
|
|
|
validator "gopkg.in/go-playground/validator.v8"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateObject validates object via validator tag and custom method
|
2022-02-09 20:09:13 +01:00
|
|
|
func (db *Database) ValidateObject(m ModelInterface, changes bson.M) error {
|
2018-03-20 14:16:01 +01:00
|
|
|
// 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 {
|
2022-02-09 15:11:15 +01:00
|
|
|
Validate(db *Database, changes bson.M) error
|
2018-03-20 14:16:01 +01:00
|
|
|
}); ok {
|
|
|
|
return i.Validate(db, changes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|