Skip to content

Commit f1f49ab

Browse files
committed
feat: ⚡ add request body validate
add request body validate add request body validate
1 parent e37d5af commit f1f49ab

File tree

4 files changed

+140
-2
lines changed

4 files changed

+140
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
golang.org/x/crypto v0.24.0 // indirect
3737
golang.org/x/net v0.26.0 // indirect
3838
google.golang.org/protobuf v1.34.2 // indirect
39+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3940
)
4041

4142
require (

go.sum

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
4343
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
4444
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
4545
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
46+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
4647
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
4748
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
49+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
50+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4851
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4952
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
5053
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -129,8 +132,8 @@ golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
129132
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
130133
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
131134
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
132-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
133-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
135+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
136+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
134137
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
135138
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
136139
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

interanl/services/news/model.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package news
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/url"
7+
"time"
8+
)
9+
10+
type NewsPostReqBody struct {
11+
Author string `json:"author"`
12+
Title string `json:"title"`
13+
Summary string `json:"summary"`
14+
CreatedAt string `json:"created_at"`
15+
Content string `json:"content"`
16+
Source string `json:"source"`
17+
Tags []string `json:"tags"`
18+
}
19+
20+
func (n NewsPostReqBody) Validate() (errs error) {
21+
if n.Author == "" {
22+
errs = errors.Join(errs, fmt.Errorf("author is empty: %s", n.Author))
23+
}
24+
25+
if n.Title == "" {
26+
errs = errors.Join(errs, fmt.Errorf("title is empty: %s", n.Title))
27+
}
28+
29+
if n.Summary == "" {
30+
errs = errors.Join(errs, fmt.Errorf("summary is empty: %s", n.Summary))
31+
}
32+
33+
if _, err := time.Parse(time.RFC3339, n.CreatedAt); err != nil {
34+
errs = errors.Join(errs, err)
35+
}
36+
37+
if _, err := url.Parse(n.Source); err != nil {
38+
errs = errors.Join(errs, err)
39+
}
40+
41+
if len(n.Tags) == 0 {
42+
errs = errors.Join(errs, errors.New("tag cannot be empty"))
43+
}
44+
return errs
45+
}

interanl/services/news/model_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package news_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/leetcode-golang-classroom/golang-rest-api-sample/interanl/services/news"
7+
)
8+
9+
func TestNewsPostBody_Validate(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
req news.NewsPostReqBody
13+
expectedErr bool
14+
}{
15+
{
16+
name: "author empty",
17+
req: news.NewsPostReqBody{},
18+
expectedErr: true,
19+
},
20+
{
21+
name: "title empty",
22+
req: news.NewsPostReqBody{
23+
Author: "test-author",
24+
},
25+
expectedErr: true,
26+
},
27+
{
28+
name: "summary invalid",
29+
req: news.NewsPostReqBody{
30+
Author: "test-author",
31+
Title: "test-title",
32+
},
33+
expectedErr: true,
34+
},
35+
{
36+
name: "time invalid",
37+
req: news.NewsPostReqBody{
38+
Author: "test-author",
39+
Title: "test-title",
40+
Summary: "test-summary",
41+
CreatedAt: "Invalid",
42+
},
43+
expectedErr: true,
44+
},
45+
{
46+
name: "source invalid",
47+
req: news.NewsPostReqBody{
48+
Author: "test-author",
49+
Title: "test-title",
50+
Summary: "test-summary",
51+
CreatedAt: "2024-04-07T05:13:27+00:00",
52+
},
53+
expectedErr: true,
54+
},
55+
{
56+
name: "tags is empty",
57+
req: news.NewsPostReqBody{
58+
Author: "test-author",
59+
Title: "test-title",
60+
Summary: "test-summary",
61+
CreatedAt: "2024-04-07T05:13:27+00:00",
62+
Source: "https://tests-news.com",
63+
},
64+
expectedErr: true,
65+
},
66+
{
67+
name: "validate",
68+
req: news.NewsPostReqBody{
69+
Author: "test-author",
70+
Title: "test-title",
71+
Summary: "test-summary",
72+
CreatedAt: "2024-04-07T05:13:27+00:00",
73+
Source: "https://tests-news.com",
74+
Tags: []string{"test-tag"},
75+
},
76+
},
77+
}
78+
for _, tc := range testCases {
79+
t.Run(tc.name, func(t *testing.T) {
80+
err := tc.req.Validate()
81+
if tc.expectedErr && err == nil {
82+
t.Fatal("expected error but got nil")
83+
}
84+
if !tc.expectedErr && err != nil {
85+
t.Fatal("expected nil but got error")
86+
}
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)