summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-03-16 11:32:54 +0530
committersinanmohd <sinan@sinanmohd.com>2024-03-16 11:33:26 +0530
commit61265f790e3c32accb3ce844422bb6b23db7fe8a (patch)
treeb44c1a7c4725f4aed418a178aad0a63c54a8822b /api
parentfb387fe290d907dd8a1b0e3600c5468043071628 (diff)
validator: init
Diffstat (limited to 'api')
-rw-r--r--api/login.go15
1 files changed, 11 insertions, 4 deletions
diff --git a/api/login.go b/api/login.go
index ef2195e..ef43304 100644
--- a/api/login.go
+++ b/api/login.go
@@ -4,17 +4,19 @@ import (
"encoding/json"
"net/http"
+ "github.com/go-playground/validator/v10"
redqdb "sinanmohd.com/redq/db"
)
type loginAPI struct {
- db *redqdb.SafeDB
- req *RequestLogin
- resp *ResponseLogin
+ db *redqdb.SafeDB
+ validate *validator.Validate
+ req *RequestLogin
+ resp *ResponseLogin
}
type RequestLogin struct {
- Account *redqdb.Account
+ Account *redqdb.Account `validate:"required"`
}
type ResponseLogin struct {
@@ -24,6 +26,7 @@ type ResponseLogin struct {
func newLogin(db *redqdb.SafeDB) *loginAPI {
a := &loginAPI{}
a.db = db
+ a.validate = validator.New(validator.WithRequiredStructEnabled())
return a
}
@@ -31,7 +34,11 @@ func newLogin(db *redqdb.SafeDB) *loginAPI {
func (a *loginAPI) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
a.req = &RequestLogin{}
a.resp = &ResponseLogin{}
+
err := unmarshal(r.Body, a.req)
+ if err == nil {
+ err = a.validate.Struct(a.req)
+ }
if err != nil {
handleError(err, rw, http.StatusUnprocessableEntity)
return