Passed
Pull Request — main (#68)
by Igor
02:03
created

validator.WithLanguage   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
nop 1
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
// Reset function recreates singleton validator. Generally, it can be used in tests.
38
//
39
// Deprecated: use SetUp function instead.
40
func Reset() {
41
	_ = SetUp()
42
}
43
44
// Validate is the main validation method. It accepts validation arguments. Arguments can be
45
// used to tune up the validation process or to pass values of a specific type.
46
func Validate(ctx context.Context, arguments ...validation.Argument) error {
47 1
	return validator.Validate(ctx, arguments...)
48
}
49
50
// ValidateValue is an alias for validating a single value of any supported type.
51
func ValidateValue(ctx context.Context, value interface{}, options ...validation.Option) error {
52
	return validator.ValidateValue(ctx, value, options...)
53
}
54
55
// ValidateBool is an alias for validating a single boolean value.
56
func ValidateBool(ctx context.Context, value bool, options ...validation.Option) error {
57
	return validator.ValidateBool(ctx, value, options...)
58
}
59
60
// ValidateNumber is an alias for validating a single numeric value (integer or float).
61
func ValidateNumber(ctx context.Context, value interface{}, options ...validation.Option) error {
62 1
	return validator.ValidateNumber(ctx, value, options...)
63
}
64
65
// ValidateString is an alias for validating a single string value.
66
func ValidateString(ctx context.Context, value string, options ...validation.Option) error {
67 1
	return validator.ValidateString(ctx, value, options...)
68
}
69
70
// ValidateStrings is an alias for validating slice of strings.
71
func ValidateStrings(ctx context.Context, values []string, options ...validation.Option) error {
72
	return validator.ValidateStrings(ctx, values, options...)
73
}
74
75
// ValidateIterable is an alias for validating a single iterable value (an array, slice, or map).
76
func ValidateIterable(ctx context.Context, value interface{}, options ...validation.Option) error {
77 1
	return validator.ValidateIterable(ctx, value, options...)
78
}
79
80
// ValidateCountable is an alias for validating a single countable value (an array, slice, or map).
81
func ValidateCountable(ctx context.Context, count int, options ...validation.Option) error {
82
	return validator.ValidateCountable(ctx, count, options...)
83
}
84
85
// ValidateTime is an alias for validating a single time value.
86
func ValidateTime(ctx context.Context, value time.Time, options ...validation.Option) error {
87 1
	return validator.ValidateTime(ctx, value, options...)
88
}
89
90
// ValidateEach is an alias for validating each value of an iterable (an array, slice, or map).
91
func ValidateEach(ctx context.Context, value interface{}, options ...validation.Option) error {
92
	return validator.ValidateEach(ctx, value, options...)
93
}
94
95
// ValidateEachString is an alias for validating each value of a strings slice.
96
func ValidateEachString(ctx context.Context, strings []string, options ...validation.Option) error {
97
	return validator.ValidateEachString(ctx, strings, options...)
98
}
99
100
// ValidateValidatable is an alias for validating value that implements the Validatable interface.
101
func ValidateValidatable(ctx context.Context, validatable validation.Validatable, options ...validation.Option) error {
102
	return validator.ValidateValidatable(ctx, validatable, options...)
103
}
104
105
// ValidateBy is used to get the constraint from the internal validator store.
106
// If the constraint does not exist, then the validator will
107
// return a ConstraintNotFoundError during the validation process.
108
// For storing a constraint you should use the validation.StoredConstraint option.
109
func ValidateBy(constraintKey string) validation.Constraint {
110
	return validator.ValidateBy(constraintKey)
111
}
112
113
// WithGroups is used to execute conditional validation based on validation groups. It creates
114
// a new scoped validation with a given set of groups.
115
//
116
// By default, when validating an object all constraints of it will be checked whether or not
117
// they pass. In some cases, however, you will need to validate an object against
118
// only some specific group of constraints. To do this, you can organize each constraint
119
// into one or more validation groups and then apply validation against one group of constraints.
120
//
121
// Validation groups are working together only with validation groups passed
122
// to a constraint by WhenGroups() method. This method is implemented in all built-in constraints.
123
// If you want to use validation groups for your own constraints do not forget to implement
124
// this method in your constraint.
125
//
126
// Be careful, empty groups are considered as the default group.
127
// Its value is equal to the validation.DefaultGroup ("default").
128
func WithGroups(groups ...string) *validation.Validator {
129 1
	return validator.WithGroups(groups...)
130
}
131
132
// WithLanguage method creates a new scoped validator with a given language tag. All created violations
133
// will be translated into this language.
134
func WithLanguage(tag language.Tag) *validation.Validator {
135
	return validator.WithLanguage(tag)
136
}
137
138
// AtProperty method creates a new scoped validator with injected property name element to scope property path.
139
func AtProperty(name string) *validation.Validator {
140 1
	return validator.AtProperty(name)
141
}
142
143
// AtIndex method creates a new scoped validator with injected array index element to scope property path.
144
func AtIndex(index int) *validation.Validator {
145 1
	return validator.AtIndex(index)
146
}
147
148
// BuildViolation can be used to build a custom violation on the client-side.
149
func BuildViolation(ctx context.Context, code, message string) *validation.ViolationBuilder {
150 1
	return validator.BuildViolation(ctx, code, message)
151
}
152