| Total Lines | 39 |
| 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 File struct { |
||
| 12 | IsDocument bool |
||
| 13 | DocumentName string |
||
| 14 | Name string |
||
| 15 | } |
||
| 16 | |||
| 17 | func ExampleConditionalConstraint_Then_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 | } |
||
| 44 |