Passed
Pull Request — main (#55)
by Igor
04:23 queued 02:12
created

validator.ValidateStrings   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
// SetOptions can be used to set up a singleton validator. Make sure you call this function once
20
// at the initialization of your application.
21
func SetOptions(options ...validation.ValidatorOption) error {
22 1
	for _, setOption := range options {
23 1
		err := setOption(validator)
24 1
		if err != nil {
25
			return err
26
		}
27
	}
28
29 1
	return nil
30
}
31
32
// Reset function recreates singleton validator. Generally, it can be used in tests.
33
func Reset() {
34 1
	validator, _ = validation.NewValidator()
35
}
36
37
// Validate is the main validation method. It accepts validation arguments. Arguments can be
38
// used to tune up the validation process or to pass values of a specific type.
39
func Validate(arguments ...validation.Argument) error {
40 1
	return validator.Validate(arguments...)
41
}
42
43
// ValidateValue is an alias for validating a single value of any supported type.
44
func ValidateValue(value interface{}, options ...validation.Option) error {
45 1
	return validator.ValidateValue(value, options...)
46
}
47
48
// ValidateBool is an alias for validating a single boolean value.
49
func ValidateBool(value *bool, options ...validation.Option) error {
50 1
	return validator.ValidateBool(value, options...)
51
}
52
53
// ValidateNumber is an alias for validating a single numeric value (integer or float).
54
func ValidateNumber(value interface{}, options ...validation.Option) error {
55 1
	return validator.ValidateNumber(value, options...)
56
}
57
58
// ValidateString is an alias for validating a single string value.
59
func ValidateString(value *string, options ...validation.Option) error {
60 1
	return validator.ValidateString(value, options...)
61
}
62
63
// ValidateStrings is an alias for validating slice of strings.
64
func ValidateStrings(values []string, options ...validation.Option) error {
65 1
	return validator.ValidateStrings(values, options...)
66
}
67
68
// ValidateIterable is an alias for validating a single iterable value (an array, slice, or map).
69
func ValidateIterable(value interface{}, options ...validation.Option) error {
70 1
	return validator.ValidateIterable(value, options...)
71
}
72
73
// ValidateCountable is an alias for validating a single countable value (an array, slice, or map).
74
func ValidateCountable(count int, options ...validation.Option) error {
75 1
	return validator.ValidateCountable(count, options...)
76
}
77
78
// ValidateTime is an alias for validating a single time value.
79
func ValidateTime(value *time.Time, options ...validation.Option) error {
80 1
	return validator.ValidateTime(value, options...)
81
}
82
83
// ValidateEach is an alias for validating each value of an iterable (an array, slice, or map).
84
func ValidateEach(value interface{}, options ...validation.Option) error {
85 1
	return validator.ValidateEach(value, options...)
86
}
87
88
// ValidateEachString is an alias for validating each value of a strings slice.
89
func ValidateEachString(strings []string, options ...validation.Option) error {
90 1
	return validator.ValidateEachString(strings, options...)
91
}
92
93
// ValidateValidatable is an alias for validating value that implements the Validatable interface.
94
func ValidateValidatable(validatable validation.Validatable, options ...validation.Option) error {
95 1
	return validator.ValidateValidatable(validatable, options...)
96
}
97
98
// WithContext method creates a new scoped validator with a given context. You can use this method to pass
99
// a context value to all used constraints.
100
101
// Example
102
//  err := validator.WithContext(request.Context()).Validate(
103
//      String(&s, it.IsNotBlank()), // now all called constraints will use passed context in their methods
104
//  )
105
func WithContext(ctx context.Context) *validation.Validator {
106
	return validator.WithContext(ctx)
107
}
108
109
// WithLanguage method creates a new scoped validator with a given language tag. All created violations
110
// will be translated into this language.
111
//
112
// Example
113
//  err := validator.WithLanguage(language.Russian).Validate(
114
//      validation.ValidateString(&s, it.IsNotBlank()), // violation from this constraint will be translated
115
//  )
116
func WithLanguage(tag language.Tag) *validation.Validator {
117
	return validator.WithLanguage(tag)
118
}
119
120
// AtProperty method creates a new scoped validator with injected property name element to scope property path.
121
func AtProperty(name string) *validation.Validator {
122 1
	return validator.AtProperty(name)
123
}
124
125
// AtIndex method creates a new scoped validator with injected array index element to scope property path.
126
func AtIndex(index int) *validation.Validator {
127 1
	return validator.AtIndex(index)
128
}
129
130
// BuildViolation can be used to build a custom violation on the client-side.
131
//
132
// Example
133
//  err := validator.BuildViolation("", "").
134
//      AddParameter("key", "value").
135
//      CreateViolation()
136
func BuildViolation(code, message string) *validation.ViolationBuilder {
137 1
	return validator.BuildViolation(code, message)
138
}
139
140
// ValidateBy is used to get the constraint from the internal validator store.
141
// If the constraint does not exist, then the validator will
142
// return a ConstraintNotFoundError during the validation process.
143
// For storing a constraint you should use the validation.StoredConstraint option.
144
func ValidateBy(constraintKey string) validation.Constraint {
145
	return validator.ValidateBy(constraintKey)
146
}
147