Skip to content

Commit

Permalink
fix param validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn committed Nov 15, 2023
1 parent f173f1f commit dc357d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions internal/handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type LocationRequest struct {
Lng float64 `query:"lng"`
Lat float64 `query:"lat"`
Lng float64 `query:"lng" vd:"$>=-180 && $<=180"`
Lat float64 `query:"lat" vd:"$>=-90 && $<=90"`
}

type GetTimezoneNameResponse struct {
Expand All @@ -21,9 +21,9 @@ type GetTimezoneNameResponse struct {

func GetTimezoneName(ctx context.Context, c *app.RequestContext) {
req := &LocationRequest{}
err := c.Bind(req)
err := c.BindAndValidate(req)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI})
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())})
return
}
timezone := finder.GetTimezoneName(req.Lng, req.Lat)
Expand All @@ -44,9 +44,9 @@ type GetTimezoneNamesResponse struct {

func GetTimezoneNames(ctx context.Context, c *app.RequestContext) {
req := &LocationRequest{}
err := c.Bind(req)
err := c.BindAndValidate(req)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI})
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())})
return
}
timezones, err := finder.GetTimezoneNames(req.Lng, req.Lat)
Expand Down Expand Up @@ -85,7 +85,7 @@ type GetTimezoneInfoRequest struct {

func GetTimezoneShape(ctx context.Context, c *app.RequestContext) {
req := &GetTimezoneInfoRequest{}
err := c.Bind(req)
err := c.BindAndValidate(req)
if err != nil {
c.JSON(http.StatusBadRequest, utils.H{"err": err.Error()})
return
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type GetTimezoneInfoPageResponse struct {

func GetTimezoneInfoPage(ctx context.Context, c *app.RequestContext) {
req := &GetTimezoneInfoRequest{}
if err := c.Bind(req); err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI})
if err := c.BindAndValidate(req); err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())})
return
}

Expand All @@ -47,7 +47,7 @@ func GetTimezoneInfoPage(ctx context.Context, c *app.RequestContext) {

func GetTimezonesInfoPage(ctx context.Context, c *app.RequestContext) {
req := &LocationRequest{}
err := c.Bind(req)
err := c.BindAndValidate(req)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())})
return
Expand Down Expand Up @@ -97,7 +97,7 @@ func GetAllSupportTimezoneNamesPage(ctx context.Context, c *app.RequestContext)
// Render GeoJSON on web
func GetGeoJSONViewerForTimezone(ctx context.Context, c *app.RequestContext) {
req := &GetTimezoneInfoRequest{}
if err := c.Bind(req); err != nil {
if err := c.BindAndValidate(req); err != nil {
c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())})
return
}
Expand Down

0 comments on commit dc357d8

Please sign in to comment.