Passed
Pull Request — main (#48)
by Igor
02:21
created

example_struct_validation_test.go   A

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validation_test.ExampleValidator_Validate_basicStructValidation 0 15 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 Document struct {
12
	Title    string
13
	Keywords []string
14
}
15
16
func ExampleValidator_Validate_basicStructValidation() {
17
	document := Document{
18
		Title:    "",
19
		Keywords: []string{""},
20
	}
21
22
	err := validator.Validate(
23
		validation.StringProperty("title", &document.Title, it.IsNotBlank()),
24
		validation.CountableProperty("keywords", len(document.Keywords), it.HasCountBetween(2, 10)),
25
		validation.EachStringProperty("keywords", document.Keywords, it.IsNotBlank()),
26
	)
27
28
	violations := err.(validation.ViolationList)
29
	for _, violation := range violations {
30
		fmt.Println(violation.Error())
31
	}
32
	// Output:
33
	// violation at 'title': This value should not be blank.
34
	// violation at 'keywords': This collection should contain 2 elements or more.
35
	// violation at 'keywords[0]': This value should not be blank.
36
}
37