File size: 647 Bytes
602f6ba
 
 
1cea019
 
602f6ba
1cea019
602f6ba
 
 
 
 
 
 
1cea019
602f6ba
1cea019
 
 
602f6ba
1cea019
602f6ba
58a3ab8
6c0b3e3
1cea019
6c0b3e3
602f6ba
1cea019
 
 
 
602f6ba
 
1cea019
602f6ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
}