mgocrud/validator.go

28 lines
604 B
Go
Raw Normal View History

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-14 16:01:12 +01:00
func ValidateObject(db Database, 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 21:57:31 +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
}