Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
guiferpa authored Apr 14, 2020
1 parent 72ce1ca commit 4940e41
Showing 1 changed file with 54 additions and 23 deletions.
77 changes: 54 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,74 @@

### Installation
```bash
go get github.com/guiferpa/gody@v1.1.0
go get github.com/guiferpa/gody@v2.0.0
```

### Usage

```go
package main

import (
"log"

"github.com/guiferpa/gody"
"github.com/guiferpa/gody/rule"
"github.com/guiferpa/gody"
"github.com/guiferpa/gody/rule"
)

type Body struct {
Text string `json:"text" validate:"not_empty"`
type RequestBody struct {
Name string `json:"name" validate:"not_empty"`
Age int `json:"age" validate:"min=21"`
}

func main() {
b := Body{}
...

validator := gody.NewValidator()

if validated, err := gody.Validate(b, nil); err != nil {
if !validated {
log.Println("body didn't validate:", err)
}
rules := []gody.Rule{rule.NotEmpty, rule.Min}
validator.AddRules(rules...)

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println(err)
}
func HTTPHandler(vtr *gody.Validator) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body RequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
...
}
defer r.Body.Close()

if validated, err := vtr.Validate(body); err != nil {
...
}
}
}

port := /* port */
http.ListenAndServe(port, HTTPHandler(validator))

...
```

### Others ways for validation

There are others ways to valid a struct, take a look on functions below:

- **RawValidate** - It's a function that make validate with no rule, it's necessary put the struct for validation, some rule(s) and tag name.

```go
gody.RawValidate(interface{}, string, []gody.Rule) (bool, error)
```

- **Validate** - It's a function that make validate with no rule, it's necessary put the struct for validation and some rule(s).
```go
gody.Validate(interface{}, []gody.Rule) (bool, error)
```

- **RawDefaultValidate** - It's a function that already have [built-in rules](https://github.com/guiferpa/gody/blob/72ce1caecc5fdacf40ee282716ec1b5abe6f7adf/validate.go#L15-L23) configured, it's necessary put the struct for validation, tag name and optional custom rule(s).
```go
gody.RawDefaultValidate(interface{}, string, []gody.Rule) (bool, error)
```

- **DefaultValidate** - It's a function that already have [built-in rules](https://github.com/guiferpa/gody/blob/72ce1caecc5fdacf40ee282716ec1b5abe6f7adf/validate.go#L15-L23) configured, it's necessary put the struct for validation and optional custom rule(s).
```go
gody.DefaultValidate(interface{}, []gody.Rule) (bool, error)
```

### Kinds of validation
### Contribution policies

- [Simple](https://github.com/guiferpa/gody/blob/6fd8753bedc85053e4f91307edb6d1cda8159e79/example/validate.go#L11-L29)
- [Deep](https://github.com/guiferpa/gody/blob/6fd8753bedc85053e4f91307edb6d1cda8159e79/example/validate.go#L84-L115)
- [Custom](https://github.com/guiferpa/gody/blob/6fd8753bedc85053e4f91307edb6d1cda8159e79/example/validate.go#L31-L82)
1. At this time the only policy is don't create a Pull Request directly, it's necessary some discussions for some implementation then please open a issue before any thing to dicussion about the subject.

0 comments on commit 4940e41

Please sign in to comment.