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

options.go   A

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
cc 6
eloc 18
dl 0
loc 40
ccs 7
cts 8
cp 0.875
crap 6.0702
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validation.PropertyName 0 5 2
A validation.ArrayIndex 0 5 2
A validation.optionFunc.setUpOnScope 0 2 1
A validation.optionFunc.SetUp 0 2 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