Passed
Pull Request — main (#37)
by Rushan
01:59
created

examples_condition_validation_test.go   A

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 27
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validation_test.ExampleValidator_Validate_structValidationWithConditionalConstraint 0 23 2
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