api-qobiltu-dev / pkg /validation /validation.go
lifedebugger's picture
Deploy files from GitHub repository
6c0b3e3
package validation
import (
"context"
"time"
"gorm.io/gorm"
)
type ErrorMessage struct {
Field string `json:"field"`
Message string `json:"-"`
}
func New(db *gorm.DB) (*Validator, error) {
// Create source with 5 minute cache expiry
expiry := 5 * time.Minute
dbSource, err := NewDBOptionSource(db, expiry)
if err != nil {
return nil, err
}
// Start background refresh every 5 minutes
ctx := context.Background()
dbSource.StartAutoRefresh(ctx, 5*time.Minute)
// create validator
validator := NewValidator(dbSource)
if err := validator.RegisterAllCustomRules(); err != nil {
return nil, err
}
return validator, nil
}