Passed
Push — main ( 488c53...b50ef2 )
by Rushan
57s queued 11s
created

validation.ArrayIndex   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
nop 1
1
package validation
2
3
// Option is used to set up validation process of a value.
4
type Option interface {
5
	// SetUp is called when the validation process is initialized
6
	// and can be used to gracefully handle errors when initializing constraints.
7
	SetUp() error
8
}
9
10
// internalOption is used to tune the validation scope before starting the validation process.
11
type internalOption interface {
12
	setUpOnScope(scope *Scope) error
13
}
14
15
// optionFunc is an adapter that allows to use ordinary functions as validation options.
16
type optionFunc func(scope *Scope) error
17
18
func (f optionFunc) SetUp() error {
19
	return nil
20
}
21
22
func (f optionFunc) setUpOnScope(scope *Scope) error {
23 1
	return f(scope)
24
}
25
26
// PropertyName option adds name of the given property to current validation scope.
27
func PropertyName(propertyName string) Option {
28 1
	return optionFunc(func(scope *Scope) error {
29 1
		scope.propertyPath = append(scope.propertyPath, PropertyNameElement(propertyName))
30
31 1
		return nil
32
	})
33
}
34
35
// ArrayIndex option adds index of the given array to current validation scope.
36
func ArrayIndex(index int) Option {
37 1
	return optionFunc(func(scope *Scope) error {
38 1
		scope.propertyPath = append(scope.propertyPath, ArrayIndexElement(index))
39
40 1
		return nil
41
	})
42
}
43