Test Setup Failed
Pull Request — main (#71)
by Igor
02:10
created

validation.WhenArgument.With   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package validation
2
3
// WhenArgument is used to build conditional validation. Use the When function to initiate a conditional check.
4
// If the condition is true, then the arguments passed through the Then function will be processed.
5
// Otherwise, the arguments passed through the Else function will be processed.
6
type WhenArgument struct {
7
	isTrue        bool
8
	options       []Option
9
	thenArguments []Argument
10
	elseArguments []Argument
11
}
12
13
// When function is used to initiate conditional validation.
14
// If the condition is true, then the arguments passed through the Then function will be processed.
15
// Otherwise, the arguments passed through the Else function will be processed.
16
func When(isTrue bool) WhenArgument {
17
	return WhenArgument{isTrue: isTrue}
18
}
19
20
// Then function is used to set a sequence of arguments to be processed if the condition is true.
21
func (arg WhenArgument) Then(arguments ...Argument) WhenArgument {
22
	arg.thenArguments = arguments
23
	return arg
24
}
25
26
// Else function is used to set a sequence of arguments to be processed if a condition is false.
27
func (arg WhenArgument) Else(arguments ...Argument) WhenArgument {
28
	arg.elseArguments = arguments
29
	return arg
30
}
31
32
// With returns a copy of WhenArgument with appended options.
33
func (arg WhenArgument) With(options ...Option) WhenArgument {
34
	arg.options = append(arg.options, options...)
35
	return arg
36
}
37
38
func (arg WhenArgument) setUp(ctx *executionContext) {
39
	ctx.addValidator(arg.options, func(scope Scope) (*ViolationList, error) {
40
		var err error
41
		if arg.isTrue {
42
			err = scope.validate(arg.thenArguments...)
43
		} else {
44
			err = scope.validate(arg.elseArguments...)
45
		}
46
47
		return unwrapViolationList(err)
48
	})
49
}
50
51
// SequentialArgument can be used to interrupt validation process when the first violation is raised.
52
type SequentialArgument struct {
53
	isIgnored bool
54
	groups    []string
55
	options   []Option
56
	arguments []Argument
57
}
58
59
// Sequentially function used to run validation process step-by-step.
60
func Sequentially(arguments ...Argument) SequentialArgument {
61
	return SequentialArgument{arguments: arguments}
62
}
63
64
// With returns a copy of SequentialArgument with appended options.
65
func (arg SequentialArgument) With(options ...Option) SequentialArgument {
66
	arg.options = append(arg.options, options...)
67
	return arg
68
}
69
70
// When enables conditional validation of this argument. If the expression evaluates to false,
71
// then the argument will be ignored.
72
func (arg SequentialArgument) When(condition bool) SequentialArgument {
73
	arg.isIgnored = !condition
74
	return arg
75
}
76
77
// WhenGroups enables conditional validation of the argument by using the validation groups.
78
func (arg SequentialArgument) WhenGroups(groups ...string) SequentialArgument {
79
	arg.groups = groups
80
	return arg
81
}
82
83
func (arg SequentialArgument) setUp(ctx *executionContext) {
84
	ctx.addValidator(arg.options, func(scope Scope) (*ViolationList, error) {
85
		if arg.isIgnored || scope.IsIgnored(arg.groups...) {
86
			return nil, nil
87
		}
88
89
		violations := &ViolationList{}
90
91
		for _, argument := range arg.arguments {
92
			err := violations.AppendFromError(scope.validate(argument))
93
			if err != nil {
94
				return nil, err
95
			}
96
			if violations.len > 0 {
97
				return violations, nil
98
			}
99
		}
100
101
		return violations, nil
102
	})
103
}
104
105
// AtLeastOneOfArgument can be used to set up validation process to check that the value satisfies
106
// at least one of the given constraints. The validation stops as soon as one constraint is satisfied.
107
type AtLeastOneOfArgument struct {
108
	isIgnored bool
109
	groups    []string
110
	options   []Option
111
	arguments []Argument
112
}
113
114
// AtLeastOneOf can be used to set up validation process to check that the value satisfies
115
// at least one of the given constraints. The validation stops as soon as one constraint is satisfied.
116
func AtLeastOneOf(arguments ...Argument) AtLeastOneOfArgument {
117
	return AtLeastOneOfArgument{arguments: arguments}
118
}
119
120
// With returns a copy of AtLeastOneOfArgument with appended options.
121
func (arg AtLeastOneOfArgument) With(options ...Option) AtLeastOneOfArgument {
122
	arg.options = append(arg.options, options...)
123
	return arg
124
}
125
126
// When enables conditional validation of this argument. If the expression evaluates to false,
127
// then the argument will be ignored.
128
func (arg AtLeastOneOfArgument) When(condition bool) AtLeastOneOfArgument {
129
	arg.isIgnored = !condition
130
	return arg
131
}
132
133
// WhenGroups enables conditional validation of the argument by using the validation groups.
134
func (arg AtLeastOneOfArgument) WhenGroups(groups ...string) AtLeastOneOfArgument {
135
	arg.groups = groups
136
	return arg
137
}
138
139
func (arg AtLeastOneOfArgument) setUp(ctx *executionContext) {
140
	ctx.addValidator(arg.options, func(scope Scope) (*ViolationList, error) {
141
		if arg.isIgnored || scope.IsIgnored(arg.groups...) {
142
			return nil, nil
143
		}
144
145
		violations := &ViolationList{}
146
147
		for _, argument := range arg.arguments {
148
			violation := scope.validate(argument)
149
			if violation == nil {
150
				return nil, nil
151
			}
152
153
			err := violations.AppendFromError(violation)
154
			if err != nil {
155
				return nil, err
156
			}
157
		}
158
159
		return violations, nil
160
	})
161
}
162