Passed
Push — main ( 26a57f...819916 )
by Igor
386:22 queued 269:14
created

validation_test.ExampleConditionalConstraint_Else   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 0
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
1
package validation_test
2
3
import (
4
	"fmt"
5
6
	"github.com/muonsoft/validation"
7
	"github.com/muonsoft/validation/it"
8
	"github.com/muonsoft/validation/validator"
9
10
	"regexp"
11
)
12
13
func ExampleConditionalConstraint_Then() {
14
	v := "foo"
15
	err := validator.ValidateString(
16
		&v,
17
		validation.When(true).
18
			Then(
19
				it.Matches(regexp.MustCompile(`^\w+$`)),
20
			),
21
	)
22
	fmt.Println(err)
23
	// Output:
24
	// <nil>
25
}
26
27
func ExampleConditionalConstraint_Else() {
28
	v := "123"
29
	err := validator.ValidateString(
30
		&v,
31
		validation.When(false).
32
			Then(
33
				it.Matches(regexp.MustCompile(`^\w+$`)),
34
			).
35
			Else(
36
				it.Matches(regexp.MustCompile(`^\d+$`)),
37
			),
38
	)
39
	fmt.Println(err)
40
	// Output:
41
	// <nil>
42
}
43