Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage.out
coverage.out
.idea
6 changes: 6 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func (v *Validator) Require(field string) *ValidationResult {
}
}

func (v *Validator) RequireAll(fields ...string) {
for _, field := range fields {
v.Require(field)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like we would want to have a return value for this since Require has one... would it make sense to have this signature be:

func (v *Validator) RequireAll(fields ...string) ([]*ValidationResult, bool) {
  // ...
}

where the boolean return value indicates validity of the set, while the slice would provide access to the results?

}

// RequireFile will add an error to the Validator if data.Files[field]
// does not exist or is an empty file
func (v *Validator) RequireFile(field string) *ValidationResult {
Expand Down
18 changes: 18 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ func TestRequire(t *testing.T) {
}
}

func TestRequireAll(t *testing.T) {
data := newData()
data.Add("name", "Bob")
data.Add("age", "25")
data.Add("color", "")

val := data.Validator()
val.RequireAll("age", "name")
if val.HasErrors() {
t.Errorf("Expected no errors but got errors: %v", val.Messages())
}

val.RequireAll("color", "a")
if len(val.Messages()) != 2 {
t.Errorf("Expected 2 validation errors but got %d.", len(val.Messages()))
}
}

func TestRequireFile(t *testing.T) {
data := newData()
val := data.Validator()
Expand Down