Passed
Pull Request — main (#57)
by Igor
01:50
created

validator.AtIndex   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
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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
// 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(ctx context.Context, arguments ...validation.Argument) error {
40 1
	return validator.Validate(ctx, arguments...)
41
}
42
43
// ValidateValue is an alias for validating a single value of any supported type.
44
func ValidateValue(ctx context.Context, value interface{}, options ...validation.Option) error {
45 1
	return validator.ValidateValue(ctx, value, options...)
46
}
47
48
// ValidateBool is an alias for validating a single boolean value.
49
func ValidateBool(ctx context.Context, value *bool, options ...validation.Option) error {
50 1
	return validator.ValidateBool(ctx, value, options...)
51
}
52
53
// ValidateNumber is an alias for validating a single numeric value (integer or float).
54
func ValidateNumber(ctx context.Context, value interface{}, options ...validation.Option) error {
55 1
	return validator.ValidateNumber(ctx, value, options...)
56
}
57
58
// ValidateString is an alias for validating a single string value.
59
func ValidateString(ctx context.Context, value *string, options ...validation.Option) error {
60 1
	return validator.ValidateString(ctx, value, options...)
61
}
62
63
// ValidateStrings is an alias for validating slice of strings.
64
func ValidateStrings(ctx context.Context, values []string, options ...validation.Option) error {
65 1
	return validator.ValidateStrings(ctx, values, options...)
66
}
67
68
// ValidateIterable is an alias for validating a single iterable value (an array, slice, or map).
69
func ValidateIterable(ctx context.Context, value interface{}, options ...validation.Option) error {
70 1
	return validator.ValidateIterable(ctx, value, options...)
71
}
72
73
// ValidateCountable is an alias for validating a single countable value (an array, slice, or map).
74
func ValidateCountable(ctx context.Context, count int, options ...validation.Option) error {
75 1
	return validator.ValidateCountable(ctx, count, options...)
76
}
77
78
// ValidateTime is an alias for validating a single time value.
79
func ValidateTime(ctx context.Context, value *time.Time, options ...validation.Option) error {
80 1
	return validator.ValidateTime(ctx, value, options...)
81
}
82
83
// ValidateEach is an alias for validating each value of an iterable (an array, slice, or map).
84
func ValidateEach(ctx context.Context, value interface{}, options ...validation.Option) error {
85 1
	return validator.ValidateEach(ctx, value, options...)
86
}
87
88
// ValidateEachString is an alias for validating each value of a strings slice.
89
func ValidateEachString(ctx context.Context, strings []string, options ...validation.Option) error {
90 1
	return validator.ValidateEachString(ctx, strings, options...)
91
}
92
93
// ValidateValidatable is an alias for validating value that implements the Validatable interface.
94
func ValidateValidatable(ctx context.Context, validatable validation.Validatable, options ...validation.Option) error {
95 1
	return validator.ValidateValidatable(ctx, validatable, options...)
96
}
97
98
// WithLanguage method creates a new scoped validator with a given language tag. All created violations
99
// will be translated into this language.
100
//
101
// Example
102
//  err := validator.WithLanguage(language.Russian).Validate(
103
//      validation.ValidateString(&s, it.IsNotBlank()), // violation from this constraint will be translated
104
//  )
105
func WithLanguage(tag language.Tag) *validation.Validator {
106
	return validator.WithLanguage(tag)
107
}
108
109
// AtProperty method creates a new scoped validator with injected property name element to scope property path.
110
func AtProperty(name string) *validation.Validator {
111 1
	return validator.AtProperty(name)
112
}
113
114
// AtIndex method creates a new scoped validator with injected array index element to scope property path.
115
func AtIndex(index int) *validation.Validator {
116 1
	return validator.AtIndex(index)
117
}
118
119
// BuildViolation can be used to build a custom violation on the client-side.
120
//
121
// Example
122
//  err := validator.BuildViolation(context.Background(), "", "").
123
//      AddParameter("key", "value").
124
//      CreateViolation()
125
func BuildViolation(ctx context.Context, code, message string) *validation.ViolationBuilder {
126 1
	return validator.BuildViolation(ctx, code, message)
127
}
128
129
// ValidateBy is used to get the constraint from the internal validator store.
130
// If the constraint does not exist, then the validator will
131
// return a ConstraintNotFoundError during the validation process.
132
// For storing a constraint you should use the validation.StoredConstraint option.
133
func ValidateBy(constraintKey string) validation.Constraint {
134
	return validator.ValidateBy(constraintKey)
135
}
136