Skip to content

Commit cf8445c

Browse files
authored
Add alpha_space and fix typo (#42)
* 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
1 parent 34b7b54 commit cf8445c

6 files changed

+68
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Send request to the server using curl or postman: `curl GET "http://localhost:90
127127
### Validation Rules
128128
* `alpha` The field under validation must be entirely alphabetic characters.
129129
* `alpha_dash` The field under validation may have alpha-numeric characters, as well as dashes and underscores.
130+
* `alpha_space` The field under validation may have alpha-numeric characters, as well as dashes, underscores and space.
130131
* `alpha_num` The field under validation must be entirely alpha-numeric characters.
131132
* `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.
132133
* `numeric` The field under validation must be entirely numeric characters.
@@ -145,7 +146,7 @@ Send request to the server using curl or postman: `curl GET "http://localhost:90
145146
* `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)
146147
* `email` The field under validation must have a valid email.
147148
* `float` The field under validation must have a valid float number.
148-
* `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.
149+
* `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.
149150
e.g: `min:3` may contains characters minimum length of 3 like `"john", "jane", "jane321"` but not `"mr", "xy"`
150151
* `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.
151152
e.g: `max:6` may contains characters maximum length of 6 like `"john doe", "jane doe"` but not `"john", "jane"`

doc/SIMPLE_STRUCT_VALIDATION.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
4242
e := v.ValidateJSON()
4343
fmt.Println(user) // your incoming JSON data in Go data struct
4444
err := map[string]interface{}{"validationError": e}
45-
w.Header().Set("Content-type", "applciation/json")
45+
w.Header().Set("Content-type", "application/json")
4646
json.NewEncoder(w).Encode(err)
4747
}
4848

@@ -53,7 +53,7 @@ func main() {
5353
}
5454

5555
```
56-
***Resposne***
56+
***Response***
5757
```json
5858
{
5959
"validationError": {

helper.go

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ func isAlphaDash(str string) bool {
1515
return regexAlphaDash.MatchString(str)
1616
}
1717

18+
// isAlphaSpace check the input is letters, number with dash and underscore
19+
func isAlphaSpace(str string) bool {
20+
return regexAlphaSpace.MatchString(str)
21+
}
22+
1823
// isAlphaNumeric check the input is alpha numeric or not
1924
func isAlphaNumeric(str string) bool {
2025
return regexAlphaNumeric.MatchString(str)

regex_patterns.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
)
66

77
const (
8-
// Alpha represents regular expression for alpha chartacters
8+
// Alpha represents regular expression for alpha characters
99
Alpha string = "^[a-zA-Z]+$"
10-
// AlphaDash represents regular expression for alpha chartacters with underscore and ash
11-
AlphaDash string = "^[a-zA-Z0-9_-]+$"
12-
// AlphaNumeric represents regular expression for alpha numeric chartacters
10+
// AlphaDash represents regular expression for alpha characters with underscore and dash
11+
AlphaDash string = "^[a-zA-Z0-9_-]+$"
12+
AlphaSpace string = "^[-a-zA-Z0-9_ ]+$"
13+
// AlphaNumeric represents regular expression for alpha numeric characters
1314
AlphaNumeric string = "^[a-zA-Z0-9]+$"
1415
// 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
1516
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})$"
@@ -23,7 +24,7 @@ const (
2324
DateDDMMYY string = "^(0?[1-9]|[12][0-9]|3[01])[\\/\\-](0?[1-9]|1[012])[\\/\\-]\\d{4}$"
2425
// Email represents regular expression for email
2526
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])?)+$"
26-
// Float represents regular expression for finding fload number
27+
// Float represents regular expression for finding float number
2728
Float string = "^[+-]?([0-9]*[.])?[0-9]+$"
2829
// IP represents regular expression for ip address
2930
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])$"
@@ -52,6 +53,7 @@ const (
5253
var (
5354
regexAlpha = regexp.MustCompile(Alpha)
5455
regexAlphaDash = regexp.MustCompile(AlphaDash)
56+
regexAlphaSpace = regexp.MustCompile(AlphaSpace)
5557
regexAlphaNumeric = regexp.MustCompile(AlphaNumeric)
5658
regexCreditCard = regexp.MustCompile(CreditCard)
5759
regexCoordinate = regexp.MustCompile(Coordinate)

rules.go

+13
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ func init() {
194194
return nil
195195
})
196196

197+
// AlphaDash check if provided field contains valid letters, numbers, underscore and dash
198+
AddCustomRule("alpha_space", func(field string, vlaue string, message string, value interface{}) error {
199+
str := toString(value)
200+
err := fmt.Errorf("The %s may only contain letters, numbers, dashes, space", field)
201+
if message != "" {
202+
err = errors.New(message)
203+
}
204+
if !isAlphaSpace(str) {
205+
return err
206+
}
207+
return nil
208+
})
209+
197210
// AlphaNumeric check if provided field contains valid letters and numbers
198211
AddCustomRule("alpha_num", func(field string, vlaue string, message string, value interface{}) error {
199212
str := toString(value)

rules_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,45 @@ func Test_AlphaDash(t *testing.T) {
232232
}
233233
}
234234

235+
func Test_AlphaSpace(t *testing.T) {
236+
type user struct {
237+
Name string `json:"name"`
238+
}
239+
240+
postUser := user{Name: "999$"}
241+
var userObj user
242+
243+
body, _ := json.Marshal(postUser)
244+
req, _ := http.NewRequest("POST", "http://www.example.com", bytes.NewReader(body))
245+
246+
messages := MapData{
247+
"name": []string{"alpha_space:custom_message"},
248+
}
249+
250+
rules := MapData{
251+
"name": []string{"alpha_space"},
252+
}
253+
254+
opts := Options{
255+
Request: req,
256+
Data: &userObj,
257+
Rules: rules,
258+
Messages: messages,
259+
}
260+
261+
vd := New(opts)
262+
validationErr := vd.ValidateJSON()
263+
t.Log(len(validationErr))
264+
if len(validationErr) != 1 {
265+
t.Log(validationErr)
266+
t.Error("alpha_space validation failed!")
267+
}
268+
269+
if validationErr.Get("name") != "custom_message" {
270+
t.Error("alpha space custom message failed!")
271+
}
272+
}
273+
235274
func Test_AlphaNumeric(t *testing.T) {
236275
type user struct {
237276
Name string `json:"name"`

0 commit comments

Comments
 (0)