Passed
Pull Request — main (#36)
by Rushan
03:06
created

date_structValidationWithConditionalConstraint   A

Complexity

Conditions 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
dl 0
loc 23
rs 9.55
c 0
b 0
f 0
nop 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
11
type File struct {
12
	IsDocument   bool
13
	DocumentName string
14
	Name         string
15
}
16
17
func ExampleValidator_Validate_structValidationWithConditionalConstraint() {
18
	file := File{
19
		IsDocument: true,
20
		Name:       "file name",
21
	}
22
23
	err := validator.Validate(
24
		validation.StringProperty(
25
			"name",
26
			&file.Name,
27
			it.IsNotBlank(),
28
		),
29
		validation.StringProperty(
30
			"documentName",
31
			&file.DocumentName,
32
			validation.When(file.IsDocument).
33
				Then(it.IsNotBlank()),
34
		),
35
	)
36
37
	violations := err.(validation.ViolationList)
38
	for _, violation := range violations {
39
		fmt.Println(violation.Error())
40
	}
41
	// Output:
42
	// violation at 'documentName': This value should not be blank.
43
}
44