Total Lines | 30 |
Duplicated Lines | 0 % |
Changes | 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 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 | } |
||
37 |