-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from muonsoft/next
Next
- Loading branch information
Showing
16 changed files
with
1,115 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package validation_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/muonsoft/validation" | ||
"github.com/muonsoft/validation/it" | ||
"github.com/muonsoft/validation/validator" | ||
) | ||
|
||
type Vehicle struct { | ||
Model string | ||
MaxSpeed int | ||
} | ||
|
||
func (v Vehicle) Validate(ctx context.Context, validator *validation.Validator) error { | ||
return validator.Validate(ctx, | ||
validation.StringProperty("model", v.Model, it.IsNotBlank(), it.HasMaxLength(100)), | ||
validation.NumberProperty[int]("maxSpeed", v.MaxSpeed, it.IsBetween(50, 200)), | ||
) | ||
} | ||
|
||
type Car struct { | ||
Vehicle | ||
PassengerSeats int | ||
} | ||
|
||
func (c Car) Validate(ctx context.Context, validator *validation.Validator) error { | ||
return validator.Validate(ctx, | ||
validation.CheckNoViolations(c.Vehicle.Validate(ctx, validator)), | ||
validation.NumberProperty[int]("passengerSeats", c.PassengerSeats, it.IsBetween(2, 6)), | ||
) | ||
} | ||
|
||
type Truck struct { | ||
Vehicle | ||
LoadCapacity float64 | ||
} | ||
|
||
func (t Truck) Validate(ctx context.Context, validator *validation.Validator) error { | ||
return validator.Validate(ctx, | ||
validation.CheckNoViolations(t.Vehicle.Validate(ctx, validator)), | ||
validation.NumberProperty[float64]("loadCapacity", t.LoadCapacity, it.IsBetween(10.0, 200.0)), | ||
) | ||
} | ||
|
||
func ExampleCheckNoViolations() { | ||
vehicles := []validation.Validatable{ | ||
Car{ | ||
Vehicle: Vehicle{ | ||
Model: "Audi", | ||
MaxSpeed: 10, | ||
}, | ||
PassengerSeats: 1, | ||
}, | ||
Truck{ | ||
Vehicle: Vehicle{ | ||
Model: "Benz", | ||
MaxSpeed: 20, | ||
}, | ||
LoadCapacity: 5, | ||
}, | ||
} | ||
|
||
for i, vehicle := range vehicles { | ||
err := validator.ValidateIt(context.Background(), vehicle) | ||
if violations, ok := validation.UnwrapViolationList(err); ok { | ||
fmt.Println("vehicle", i, "is not valid:") | ||
for violation := violations.First(); violation != nil; violation = violation.Next() { | ||
fmt.Println(violation) | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// vehicle 0 is not valid: | ||
// violation at 'maxSpeed': This value should be between 50 and 200. | ||
// violation at 'passengerSeats': This value should be between 2 and 6. | ||
// vehicle 1 is not valid: | ||
// violation at 'maxSpeed': This value should be between 50 and 200. | ||
// violation at 'loadCapacity': This value should be between 10 and 200. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package validation_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/muonsoft/validation" | ||
"github.com/muonsoft/validation/it" | ||
"github.com/muonsoft/validation/validator" | ||
) | ||
|
||
type Person struct { | ||
Name string | ||
Surname string | ||
Age int | ||
} | ||
|
||
func (p Person) Validate(ctx context.Context, validator *validation.Validator) error { | ||
return validator.Validate(ctx, | ||
validation.StringProperty("name", p.Name, it.IsNotBlank(), it.HasMaxLength(50)), | ||
validation.StringProperty("surname", p.Surname, it.IsNotBlank(), it.HasMaxLength(100)), | ||
validation.NumberProperty[int]("age", p.Age, it.IsBetween(18, 100)), | ||
) | ||
} | ||
|
||
func ExampleValidator_ValidateIt() { | ||
persons := []Person{ | ||
{ | ||
Name: "John", | ||
Surname: "Doe", | ||
Age: 23, | ||
}, | ||
{ | ||
Name: "", | ||
Surname: "", | ||
Age: 0, | ||
}, | ||
} | ||
|
||
for i, person := range persons { | ||
err := validator.ValidateIt(context.Background(), person) | ||
if violations, ok := validation.UnwrapViolationList(err); ok { | ||
fmt.Println("person", i, "is not valid:") | ||
for violation := violations.First(); violation != nil; violation = violation.Next() { | ||
fmt.Println(violation) | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// person 1 is not valid: | ||
// violation at 'name': This value should not be blank. | ||
// violation at 'surname': This value should not be blank. | ||
// violation at 'age': This value should be between 18 and 100. | ||
} |
Oops, something went wrong.