Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Jul 6, 2017
1 parent c7523b3 commit 09f01a4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Package [check](https://godoc.org/github.com/gowww/check) provides form validati

## Usage

1. Make a [Checker](https://godoc.org/github.com/gowww/check#Checker) with rules for keys:
1. Make a [Checker](https://godoc.org/github.com/gowww/check#Checker) with [rules](#rules) (separated by comma) for keys:

```Go
checker := check.Checker{
Expand All @@ -28,9 +28,9 @@ Package [check](https://godoc.org/github.com/gowww/check) provides form validati
}
```

2. Check you data:
2. Check data:

- Using a map:
- From a map:

```Go
errs := checker.Check(map[string][]string{
Expand All @@ -46,10 +46,37 @@ Package [check](https://godoc.org/github.com/gowww/check) provides form validati
errs := checker.CheckRequest(r)
```

3. Use errors like you want:
3. Handle errors:

```Go
if errs.NotEmpty() {
// Handle errors.
fmt.Println(errs)
}
```

If errors must be JSON formatted (for an HTTP API response, by example), use [Errors.JSON](https://godoc.org/github.com/gowww/check#Errors.JSON):

```Go
if errs.NotEmpty() {
errsjs, _ := json.Marshal(errs)
w.Write(errsjs)
}
```

### Rules

Function | Description | Usage | Possible errors
------------|-------------------------------------|--------------| -----------------------------
`alpha` | Contains alpha characters only. | `alpha` | `notAlpha`
`email` | Represents an email. | `email` | `notEmail`
`integer` | Represents an integer. | `integer` | `notInteger`
`latitude` | Represents a latitude. | `latitude` | `notLatitude`, `notNumber`
`longitude` | Represents a longitude. | `longitude` | `notLongitude`, `notNumber`
`max` | Is below or equals max. | `max:1` | `max:1`, `notNumber`
`maxlen` | Length is below or equals max. | `maxlen:1` | `maxLen:1`, `notNumber`
`min` | Is over or equals min. | `min:1` | `min:1`, `notNumber`
`minlen` | Length is over or equals min. | `minlen:1` | `minLen:1`, `notNumber`
`number` | Represents a number. | `number` | `notNumber`
`phone` | Represents a phone number. | `phone` | `notPhone`
`range` | Represents a number inside a range. | `range:1,10` | `max:1`, `min:1`, `notNumber`
`required` | Value is not empry. | `required` | `required`
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (e Errors) Merge(e2 Errors) {
}
}

// JSON returns the errors map ready to be encoded.
// JSON returns the errors map under the "errors" key, ready to be encoded.
func (e Errors) JSON() map[string]interface{} {
return map[string]interface{}{"errors": e}
}
8 changes: 6 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package check_test

import "github.com/gowww/check"
import (
"fmt"

"github.com/gowww/check"
)

func Example() {
checker := check.Checker{
Expand All @@ -16,6 +20,6 @@ func Example() {
})

if errs.NotEmpty() {
// Handle errors.
fmt.Println(errs)
}
}
30 changes: 21 additions & 9 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
)

// IsAlpha checks if v contains alpha characters only.
// If check pass, nil is returned.
func IsAlpha(v string) []error {
for i := 0; i < len(v); i++ {
if v[i] < 65 || v[i] > 90 && v[i] < 97 || v[i] > 122 {
Expand All @@ -22,27 +23,31 @@ func IsAlpha(v string) []error {
}

// IsEmail checks if v represents an email.
// If check pass, nil is returned.
func IsEmail(v string) []error {
if reEmail.MatchString(v) {
return nil
}
return []error{ErrNotEmail}
}

// IsInRange check if v represents a number inside a range.
// IsInRange checks if v represents a number inside a range.
// If check pass, nil is returned.
func IsInRange(v string, min, max float64) []error {
return append(IsMax(v, max), IsMin(v, min)...)
}

// IsInteger check if v represents an integer.
// IsInteger checks if v represents an integer.
// If check pass, nil is returned.
func IsInteger(v string) []error {
if _, err := strconv.Atoi(v); err != nil {
return []error{ErrNotInteger}
}
return nil
}

// IsLatitude check if v represents a latitude.
// IsLatitude checks if v represents a latitude.
// If check pass, nil is returned.
func IsLatitude(v string) []error {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -54,7 +59,8 @@ func IsLatitude(v string) []error {
return nil
}

// IsLongitude check if v represents a longitude.
// IsLongitude checks if v represents a longitude.
// If check pass, nil is returned.
func IsLongitude(v string) []error {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -66,7 +72,8 @@ func IsLongitude(v string) []error {
return nil
}

// IsMax check if v is below or equals max.
// IsMax checks if v is below or equals max.
// If check pass, nil is returned.
func IsMax(v string, max float64) []error {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -78,15 +85,17 @@ func IsMax(v string, max float64) []error {
return nil
}

// IsMaxLen check if v length is below or equals max.
// IsMaxLen checks if v length is below or equals max.
// If check pass, nil is returned.
func IsMaxLen(v string, max int) []error {
if len(v) > max {
return []error{fmt.Errorf("%v:%v", ErrMaxLen, max)}
}
return nil
}

// IsMin check if v is over or equals min.
// IsMin checks if v is over or equals min.
// If check pass, nil is returned.
func IsMin(v string, min float64) []error {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -98,15 +107,17 @@ func IsMin(v string, min float64) []error {
return nil
}

// IsMinLen check if v length is over or equals min.
// IsMinLen checks if v length is over or equals min.
// If check pass, nil is returned.
func IsMinLen(v string, min int) []error {
if len(v) < min {
return []error{fmt.Errorf("%v:%v", ErrMinLen, min)}
}
return nil
}

// IsNumber check if v represents a number.
// IsNumber checks if v represents a number.
// If check pass, nil is returned.
func IsNumber(v string) []error {
_, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -116,6 +127,7 @@ func IsNumber(v string) []error {
}

// IsPhone checks if v represents a phone number.
// If check pass, nil is returned.
func IsPhone(v string) []error {
if rePhone.MatchString(v) {
return nil
Expand Down

0 comments on commit 09f01a4

Please sign in to comment.