Skip to content

Commit

Permalink
Add alpha_space and fix typo (#42)
Browse files Browse the repository at this point in the history
* Changed from max to min in the README.md. Seems to have been a typo. (#38)

*  Fixed a couple of typos. (#39)

* Changed from max to min in the README.md. Seems to have been a typo.

* Fixed a couple of typos.

*  Add new rule alpha_space (#41)

* Add new rule alpha_space

* use gofmt

* Fix typo
  • Loading branch information
thedevsaddam authored Sep 23, 2018
1 parent 34b7b54 commit cf8445c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Send request to the server using curl or postman: `curl GET "http://localhost:90
### Validation Rules
* `alpha` The field under validation must be entirely alphabetic characters.
* `alpha_dash` The field under validation may have alpha-numeric characters, as well as dashes and underscores.
* `alpha_space` The field under validation may have alpha-numeric characters, as well as dashes, underscores and space.
* `alpha_num` The field under validation must be entirely alpha-numeric characters.
* `between:numeric,numeric` The field under validation check the length of characters/ length of array, slice, map/ range between two integer or float number etc.
* `numeric` The field under validation must be entirely numeric characters.
Expand All @@ -145,7 +146,7 @@ Send request to the server using curl or postman: `curl GET "http://localhost:90
* `not_in:foo,bar` The field under validation must have one value except foo,bar. e.g: `not_in:admin,manager,user` must not contain the values (admin or manager or user)
* `email` The field under validation must have a valid email.
* `float` The field under validation must have a valid float number.
* `max:numeric` The field under validation must have a min length of characters for string, items length for slice/map, value for integer or float.
* `min:numeric` The field under validation must have a min length of characters for string, items length for slice/map, value for integer or float.
e.g: `min:3` may contains characters minimum length of 3 like `"john", "jane", "jane321"` but not `"mr", "xy"`
* `max:numeric` The field under validation must have a max length of characters for string, items length for slice/map, value for integer or float.
e.g: `max:6` may contains characters maximum length of 6 like `"john doe", "jane doe"` but not `"john", "jane"`
Expand Down
4 changes: 2 additions & 2 deletions doc/SIMPLE_STRUCT_VALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
e := v.ValidateJSON()
fmt.Println(user) // your incoming JSON data in Go data struct
err := map[string]interface{}{"validationError": e}
w.Header().Set("Content-type", "applciation/json")
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(err)
}

Expand All @@ -53,7 +53,7 @@ func main() {
}

```
***Resposne***
***Response***
```json
{
"validationError": {
Expand Down
5 changes: 5 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func isAlphaDash(str string) bool {
return regexAlphaDash.MatchString(str)
}

// isAlphaSpace check the input is letters, number with dash and underscore
func isAlphaSpace(str string) bool {
return regexAlphaSpace.MatchString(str)
}

// isAlphaNumeric check the input is alpha numeric or not
func isAlphaNumeric(str string) bool {
return regexAlphaNumeric.MatchString(str)
Expand Down
12 changes: 7 additions & 5 deletions regex_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
)

const (
// Alpha represents regular expression for alpha chartacters
// Alpha represents regular expression for alpha characters
Alpha string = "^[a-zA-Z]+$"
// AlphaDash represents regular expression for alpha chartacters with underscore and ash
AlphaDash string = "^[a-zA-Z0-9_-]+$"
// AlphaNumeric represents regular expression for alpha numeric chartacters
// AlphaDash represents regular expression for alpha characters with underscore and dash
AlphaDash string = "^[a-zA-Z0-9_-]+$"
AlphaSpace string = "^[-a-zA-Z0-9_ ]+$"
// AlphaNumeric represents regular expression for alpha numeric characters
AlphaNumeric string = "^[a-zA-Z0-9]+$"
// CreditCard represents regular expression for credit cards like (Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards). Ref: https://stackoverflow.com/questions/9315647/regex-credit-card-number-tests
CreditCard string = "^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$"
Expand All @@ -23,7 +24,7 @@ const (
DateDDMMYY string = "^(0?[1-9]|[12][0-9]|3[01])[\\/\\-](0?[1-9]|1[012])[\\/\\-]\\d{4}$"
// Email represents regular expression for email
Email string = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$"
// Float represents regular expression for finding fload number
// Float represents regular expression for finding float number
Float string = "^[+-]?([0-9]*[.])?[0-9]+$"
// IP represents regular expression for ip address
IP string = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
Expand Down Expand Up @@ -52,6 +53,7 @@ const (
var (
regexAlpha = regexp.MustCompile(Alpha)
regexAlphaDash = regexp.MustCompile(AlphaDash)
regexAlphaSpace = regexp.MustCompile(AlphaSpace)
regexAlphaNumeric = regexp.MustCompile(AlphaNumeric)
regexCreditCard = regexp.MustCompile(CreditCard)
regexCoordinate = regexp.MustCompile(Coordinate)
Expand Down
13 changes: 13 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ func init() {
return nil
})

// AlphaDash check if provided field contains valid letters, numbers, underscore and dash
AddCustomRule("alpha_space", func(field string, vlaue string, message string, value interface{}) error {
str := toString(value)
err := fmt.Errorf("The %s may only contain letters, numbers, dashes, space", field)
if message != "" {
err = errors.New(message)
}
if !isAlphaSpace(str) {
return err
}
return nil
})

// AlphaNumeric check if provided field contains valid letters and numbers
AddCustomRule("alpha_num", func(field string, vlaue string, message string, value interface{}) error {
str := toString(value)
Expand Down
39 changes: 39 additions & 0 deletions rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,45 @@ func Test_AlphaDash(t *testing.T) {
}
}

func Test_AlphaSpace(t *testing.T) {
type user struct {
Name string `json:"name"`
}

postUser := user{Name: "999$"}
var userObj user

body, _ := json.Marshal(postUser)
req, _ := http.NewRequest("POST", "http://www.example.com", bytes.NewReader(body))

messages := MapData{
"name": []string{"alpha_space:custom_message"},
}

rules := MapData{
"name": []string{"alpha_space"},
}

opts := Options{
Request: req,
Data: &userObj,
Rules: rules,
Messages: messages,
}

vd := New(opts)
validationErr := vd.ValidateJSON()
t.Log(len(validationErr))
if len(validationErr) != 1 {
t.Log(validationErr)
t.Error("alpha_space validation failed!")
}

if validationErr.Get("name") != "custom_message" {
t.Error("alpha space custom message failed!")
}
}

func Test_AlphaNumeric(t *testing.T) {
type user struct {
Name string `json:"name"`
Expand Down

0 comments on commit cf8445c

Please sign in to comment.