Passed
Push — main ( f2c4e5...99f745 )
by Igor
57s queued 11s
created

validator.SetUp   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
ccs 2
cts 2
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
// 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
// WithLanguage method creates a new scoped validator with a given language tag. All created violations
114
// will be translated into this language.
115
func WithLanguage(tag language.Tag) *validation.Validator {
116
	return validator.WithLanguage(tag)
117
}
118
119
// AtProperty method creates a new scoped validator with injected property name element to scope property path.
120
func AtProperty(name string) *validation.Validator {
121 1
	return validator.AtProperty(name)
122
}
123
124
// AtIndex method creates a new scoped validator with injected array index element to scope property path.
125
func AtIndex(index int) *validation.Validator {
126 1
	return validator.AtIndex(index)
127
}
128
129
// BuildViolation can be used to build a custom violation on the client-side.
130
func BuildViolation(ctx context.Context, code, message string) *validation.ViolationBuilder {
131 1
	return validator.BuildViolation(ctx, code, message)
132
}
133