Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
)

Expand Down Expand Up @@ -291,6 +292,8 @@ func FromStruct(s interface{}) (*StructData, error) {
return data, nil
}

var jsonContent = regexp.MustCompile(`(?i)application/((\w|\.|-)+\+)?json(-seq)?`)

// FromRequest collect data from request instance
func FromRequest(r *http.Request, maxMemoryLimit ...int64) (DataFace, error) {
// no body. like GET DELETE ....
Expand Down Expand Up @@ -334,7 +337,7 @@ func FromRequest(r *http.Request, maxMemoryLimit ...int64) (DataFace, error) {
}

// JSON body request
if strings.Contains(cType, "application/json") {
if jsonContent.MatchString(cType) {
bs, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
Expand Down
143 changes: 108 additions & 35 deletions validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,43 +452,116 @@ func TestFromRequest_FileForm(t *testing.T) {
}

func TestFromRequest_JSON(t *testing.T) {
is := assert.New(t)

// =================== POST: JSON body ===================
body := strings.NewReader(`{
"name": " inhere ",
"age": 100
}`)
r, _ := http.NewRequest("POST", "/users", body)
r.Header.Set("Content-Type", "application/json")
// - create data
d, err := FromRequest(r)
is.Nil(err)
user := &struct {
Age int
Name string
}{}
md, ok := d.(*MapData)
is.True(ok)
err = md.BindJSON(user)
is.Nil(err)
is.Equal(100, user.Age)
is.Equal(" inhere ", user.Name)

// - create validation
v := d.Create()
v.StringRule("name", "-", "trim|upper")
v.Validate() // validate
is.True(v.IsOK())
err = v.BindSafeData(user)
is.NoError(err)
is.Equal("INHERE", user.Name)
body := `{
"name": " inhere ",
"age": 100
}`

tests := []struct {
name string
header string
body string
failure bool
}{
{
name: "valid JSON content type #1",
header: "application/json",
body: body,
},
{
name: "valid JSON content type #2",
header: "application/activity+json",
body: body,
},
{
name: "valid JSON content type #3",
header: "application/geo+json-seq",
body: body,
},
{
name: "valid JSON content type #4",
header: "application/json-patch+json",
body: body,
},
{
name: "valid JSON content type #5",
header: "application/vnd.api+json",
body: body,
},
{
name: "valid JSON content type #6",
header: "application/vnd.capasystems-pg+json",
body: body,
},
{
name: "valid JSON content type #7",
header: "application/vnd.ims.lti.v2.toolconsumerprofile+json",
body: body,
},
{
name: "invalid JSON content type #1",
header: "foo/bar+json-seq",
body: "",
},
{
name: "invalid JSON content type #2",
header: "application/xml",
body: "",
},
{
name: "invalid JSON content type #3",
header: "application/invalidjson",
body: "",
},
{
name: "invalid JSON content type #4",
header: "application/invalid-json-seq",
body: "",
},
{
name: "invalid JSON content type #5",
header: "application/+json",
body: "",
},
}

// error content type
r, _ = http.NewRequest("POST", "/users", nil)
d, err = FromRequest(r)
is.Nil(d)
is.Error(err)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
is := assert.New(t)

r, _ := http.NewRequest("POST", "/users", strings.NewReader(test.body))
r.Header.Set("Content-Type", test.header)

// - create data
d, err := FromRequest(r)

if test.body != "" {
user := &struct {
Age int
Name string
}{}
md, ok := d.(*MapData)
is.True(ok)
err = md.BindJSON(user)
is.Nil(err)
is.Equal(100, user.Age)
is.Equal(" inhere ", user.Name)

// - create validation
v := d.Create()
v.StringRule("name", "-", "trim|upper")
v.Validate() // validate
is.True(v.IsOK())
err = v.BindSafeData(user)
is.NoError(err)
is.Equal("INHERE", user.Name)
} else {
is.Nil(d)
is.Error(err)
}
})
}
}

func TestFieldCompare(t *testing.T) {
Expand Down