1
|
|
|
// Copyright 2021 Igor Lazarev. All rights reserved. |
2
|
|
|
// Use of this source code is governed by a MIT-style |
3
|
|
|
// license that can be found in the LICENSE file. |
4
|
|
|
|
5
|
|
|
// Package validator contains Validator service singleton. |
6
|
|
|
// It can be used in a custom application to perform the validation process. |
7
|
|
|
package validator |
8
|
|
|
|
9
|
|
|
import ( |
10
|
|
|
"context" |
11
|
|
|
"time" |
12
|
|
|
|
13
|
|
|
"github.com/muonsoft/validation" |
14
|
|
|
"golang.org/x/text/language" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
var validator, _ = validation.NewValidator() |
18
|
|
|
|
19
|
|
|
// SetUp can be used to set up a new instance of singleton validator. Make sure you call this function once |
20
|
|
|
// at the initialization of your application because it totally replaces validator instance. |
21
|
|
|
func SetUp(options ...validation.ValidatorOption) (err error) { |
22
|
1 |
|
validator, err = validation.NewValidator(options...) |
23
|
|
|
|
24
|
1 |
|
return err |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// SetOptions can be used to set up a new instance of singleton validator. Make sure you call this function once |
28
|
|
|
// at the initialization of your application because it totally replaces validator instance. |
29
|
|
|
// |
30
|
|
|
// Deprecated: use SetUp function instead. |
31
|
|
|
func SetOptions(options ...validation.ValidatorOption) (err error) { |
32
|
|
|
validator, err = validation.NewValidator(options...) |
33
|
|
|
|
34
|
|
|
return err |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Validate is the main validation method. It accepts validation arguments. executionContext can be |
38
|
|
|
// used to tune up the validation process or to pass values of a specific type. |
39
|
|
|
func Validate(ctx context.Context, arguments ...validation.Argument) error { |
40
|
|
|
return validator.Validate(ctx, arguments...) |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// ValidateBool is an alias for validating a single boolean value. |
44
|
|
|
func ValidateBool(ctx context.Context, value bool, constraints ...validation.BoolConstraint) error { |
45
|
|
|
return validator.ValidateBool(ctx, value, constraints...) |
46
|
|
|
} |
47
|
1 |
|
|
48
|
|
|
// ValidateInt is an alias for validating a single integer value. |
49
|
|
|
func ValidateInt(ctx context.Context, value int, constraints ...validation.NumberConstraint[int]) error { |
50
|
|
|
return validator.Validate(ctx, validation.Number(value, constraints...)) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// ValidateFloat is an alias for validating a single float value. |
54
|
|
|
func ValidateFloat(ctx context.Context, value float64, constraints ...validation.NumberConstraint[float64]) error { |
55
|
|
|
return validator.Validate(ctx, validation.Number(value, constraints...)) |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// ValidateString is an alias for validating a single string value. |
59
|
|
|
func ValidateString(ctx context.Context, value string, constraints ...validation.StringConstraint) error { |
60
|
|
|
return validator.ValidateString(ctx, value, constraints...) |
61
|
|
|
} |
62
|
1 |
|
|
63
|
|
|
// ValidateStrings is an alias for validating slice of strings. |
64
|
|
|
func ValidateStrings(ctx context.Context, values []string, constraints ...validation.ComparablesConstraint[string]) error { |
65
|
|
|
return validator.ValidateStrings(ctx, values, constraints...) |
66
|
|
|
} |
67
|
1 |
|
|
68
|
|
|
// ValidateCountable is an alias for validating a single countable value (an array, slice, or map). |
69
|
|
|
func ValidateCountable(ctx context.Context, count int, constraints ...validation.CountableConstraint) error { |
70
|
|
|
return validator.ValidateCountable(ctx, count, constraints...) |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// ValidateTime is an alias for validating a single time value. |
74
|
|
|
func ValidateTime(ctx context.Context, value time.Time, constraints ...validation.TimeConstraint) error { |
75
|
|
|
return validator.ValidateTime(ctx, value, constraints...) |
76
|
|
|
} |
77
|
1 |
|
|
78
|
|
|
// ValidateEachString is an alias for validating each value of a strings slice. |
79
|
|
|
func ValidateEachString(ctx context.Context, strings []string, constraints ...validation.StringConstraint) error { |
80
|
|
|
return validator.ValidateEachString(ctx, strings, constraints...) |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// ValidateIt is an alias for validating value that implements the Validatable interface. |
84
|
|
|
func ValidateIt(ctx context.Context, validatable validation.Validatable) error { |
85
|
|
|
return validator.ValidateIt(ctx, validatable) |
86
|
|
|
} |
87
|
1 |
|
|
88
|
|
|
// GetConstraint is used to get the constraint from the internal validator store. |
89
|
|
|
// If the constraint does not exist, then the validator will return nil. |
90
|
|
|
// For storing a constraint you should use the StoredConstraint option. |
91
|
|
|
// |
92
|
|
|
// Experimental. This feature is experimental and may be changed in future versions. |
93
|
|
|
func GetConstraint(key string) interface{} { |
94
|
|
|
return validator.GetConstraint(key) |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// WithGroups is used to execute conditional validation based on validation groups. It creates |
98
|
|
|
// a new scoped validation with a given set of groups. |
99
|
|
|
// |
100
|
|
|
// By default, when validating an object all constraints of it will be checked whether or not |
101
|
|
|
// they pass. In some cases, however, you will need to validate an object against |
102
|
|
|
// only some specific group of constraints. To do this, you can organize each constraint |
103
|
|
|
// into one or more validation groups and then apply validation against one group of constraints. |
104
|
|
|
// |
105
|
|
|
// Validation groups are working together only with validation groups passed |
106
|
|
|
// to a constraint by WhenGroups() method. This method is implemented in all built-in constraints. |
107
|
|
|
// If you want to use validation groups for your own constraints do not forget to implement |
108
|
|
|
// this method in your constraint. |
109
|
|
|
// |
110
|
|
|
// Be careful, empty groups are considered as the default group. |
111
|
|
|
// Its value is equal to the validation.DefaultGroup ("default"). |
112
|
|
|
func WithGroups(groups ...string) *validation.Validator { |
113
|
|
|
return validator.WithGroups(groups...) |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// WithLanguage method creates a new scoped validator with a given language tag. All created violations |
117
|
|
|
// will be translated into this language. |
118
|
|
|
func WithLanguage(tag language.Tag) *validation.Validator { |
119
|
|
|
return validator.WithLanguage(tag) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// AtProperty method creates a new scoped validator with injected property name element to scope property path. |
123
|
|
|
func AtProperty(name string) *validation.Validator { |
124
|
|
|
return validator.AtProperty(name) |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// AtIndex method creates a new scoped validator with injected array index element to scope property path. |
128
|
|
|
func AtIndex(index int) *validation.Validator { |
129
|
1 |
|
return validator.AtIndex(index) |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// BuildViolation can be used to build a custom violation on the client-side. |
133
|
|
|
func BuildViolation(ctx context.Context, code, message string) *validation.ViolationBuilder { |
134
|
|
|
return validator.BuildViolation(ctx, code, message) |
135
|
|
|
} |
136
|
|
|
|