diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-03-16 11:32:54 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-03-16 11:33:26 +0530 |
commit | 61265f790e3c32accb3ce844422bb6b23db7fe8a (patch) | |
tree | b44c1a7c4725f4aed418a178aad0a63c54a8822b /api | |
parent | fb387fe290d907dd8a1b0e3600c5468043071628 (diff) |
validator: init
Diffstat (limited to 'api')
-rw-r--r-- | api/login.go | 15 |
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 |