Passed
Push — main ( 08e96c...9065c5 )
by Igor
01:14 queued 11s
created

validator.ValidateTime   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
nop 3
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
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(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
	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
	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
	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
	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
	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
	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
	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
	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
	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
	return validator.ValidateValidatable(ctx, validatable, options...)
96
}
97
98
// ValidateBy is used to get the constraint from the internal validator store.
99
// If the constraint does not exist, then the validator will
100
// return a ConstraintNotFoundError during the validation process.
101
// For storing a constraint you should use the validation.StoredConstraint option.
102
func ValidateBy(constraintKey string) validation.Constraint {
103
	return validator.ValidateBy(constraintKey)
104
}
105
106
// WithLanguage method creates a new scoped validator with a given language tag. All created violations
107
// will be translated into this language.
108
//
109
// Example
110
//  err := validator.WithLanguage(language.Russian).Validate(
111
//      validation.ValidateString(&s, it.IsNotBlank()), // violation from this constraint will be translated
112
//  )
113
func WithLanguage(tag language.Tag) *validation.Validator {
114
	return validator.WithLanguage(tag)
115
}
116
117
// AtProperty method creates a new scoped validator with injected property name element to scope property path.
118
func AtProperty(name string) *validation.Validator {
119 1
	return validator.AtProperty(name)
120
}
121
122
// AtIndex method creates a new scoped validator with injected array index element to scope property path.
123
func AtIndex(index int) *validation.Validator {
124 1
	return validator.AtIndex(index)
125
}
126
127
// BuildViolation can be used to build a custom violation on the client-side.
128
//
129
// Example
130
//  err := validator.BuildViolation(context.Background(), "", "").
131
//      AddParameter("key", "value").
132
//      CreateViolation()
133
func BuildViolation(ctx context.Context, code, message string) *validation.ViolationBuilder {
134 1
	return validator.BuildViolation(ctx, code, message)
135
}
136