Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
nqv committed Oct 27, 2016
1 parent 5a08c83 commit 0152e4e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 39 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,70 @@ The package was a fork of go-validator but was rewritten to:
- Reduce unneccesary memory allocations
- Support Slice/Array, Map, Interface,
- Simplify source code.

## Download
```
go get -u github.com/goburrow/validator
```

## Example
```go
package main

import (
"errors"
"fmt"

"github.com/goburrow/validator"
)

type User struct {
Name string `valid:"notempty"`
Age int `valid:"min=13"`
Addresses []*Address `valid:"min=1,max=2"`
}

type Address struct {
Line1 string
Line2 string
PostCode int `valid:"min=1"`
Country string `valid:"notempty,max=2"`
}

func (a *Address) Validate() error {
if a.Line1 == "" && a.Line2 == "" {
return errors.New("Either address Line1 or Line2 must be set")
}
return nil
}

func main() {
u := &User{
Addresses: []*Address{
&Address{
Line1: "Somewhere",
PostCode: 1000,
Country: "AU",
},
&Address{
PostCode: -1,
Country: "US",
},
&Address{
Line2: "Here",
PostCode: 1,
Country: "USA",
},
},
}
v := validator.Default()
fmt.Println(v.Validate(u))
// Output:
// Name must not be empty,
// Age must not be less than 13 (was 0),
// Addresses must have length not greater than 2 (was 3),
// Either address Line1 or Line2 must be set,
// PostCode must not be less than 1 (was -1),
// Country must have length not greater than 2 (was 3)
}
```
87 changes: 49 additions & 38 deletions default_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
package validator
package validator_test

import "fmt"
import (
"errors"
"fmt"

func ExampleValidator() {
type data struct {
A string `valid:"notempty"`
B int `valid:"min=1,max=10"`
C map[int]bool `valid:"notempty,max=2"`
D []data
"github.com/goburrow/validator"
)

type User struct {
Name string `valid:"notempty"`
Age int `valid:"min=13"`
Addresses []*Address `valid:"min=1,max=2"`
}

type Address struct {
Line1 string
Line2 string
PostCode int `valid:"min=1"`
Country string `valid:"notempty,max=2"`
}

func (a *Address) Validate() error {
if a.Line1 == "" && a.Line2 == "" {
return errors.New("Either address Line1 or Line2 must be set")
}
d := &data{
A: "",
B: 11,
C: map[int]bool{
1: false,
2: true,
3: false,
},
D: []data{
data{
A: "ab",
B: 2,
return nil
}

func ExampleValidator() {
u := User{
Addresses: []*Address{
&Address{
Line1: "Somewhere",
PostCode: 1000,
Country: "AU",
},
&Address{
PostCode: -1,
Country: "US",
},
data{
A: "cd",
B: 0,
C: map[int]bool{
0: false,
},
&Address{
Line2: "Here",
PostCode: 1,
Country: "USA",
},
},
}
v := Default()
err := v.Validate(d)
if err != nil {
for _, e := range err.(Errors) {
fmt.Println(e)
}
}
v := validator.Default()
fmt.Println(v.Validate(&u))
// Output:
// A must not be empty
// B must not be greater than 10 (was 11)
// C must have length not greater than 2 (was 3)
// C must not be empty
// B must not be less than 1 (was 0)
// Name must not be empty,
// Age must not be less than 13 (was 0),
// Addresses must have length not greater than 2 (was 3),
// Either address Line1 or Line2 must be set,
// PostCode must not be less than 1 (was -1),
// Country must have length not greater than 2 (was 3)
}
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e Errors) Error() string {
var buf bytes.Buffer
for i, err := range e {
if i > 0 {
buf.WriteString("; ")
buf.WriteString(",\n")
}
buf.WriteString(err.Error())
}
Expand Down

0 comments on commit 0152e4e

Please sign in to comment.