From 0b48cb49712d83d2421ef03c8ed5e97713319d8a Mon Sep 17 00:00:00 2001 From: miaad shahrokhi Date: Sat, 30 Dec 2023 23:21:22 +0330 Subject: [PATCH] feat(validation): fix Custom Rules function --- validator/admin/kind_box/add.go | 10 +++++----- validator/admin/kind_box/delete.go | 12 +++++------ validator/admin/kind_box/get.go | 10 +++++----- validator/admin/kind_box/update.go | 12 +++++------ validator/admin/kind_box/validator.go | 20 ++++++++++--------- validator/admin/kind_box_req/add.go | 8 ++++---- validator/admin/kind_box_req/delete.go | 12 +++++------ validator/admin/kind_box_req/get.go | 10 +++++----- validator/admin/kind_box_req/update.go | 12 +++++------ validator/admin/kind_box_req/validator.go | 20 ++++++++++--------- validator/benefactor/kind_box/get.go | 10 +++++----- validator/benefactor/kind_box/get_all.go | 6 +++--- validator/benefactor/kind_box/validator.go | 20 ++++++++++--------- validator/benefactor/kind_box_req/add.go | 8 ++++---- validator/benefactor/kind_box_req/delete.go | 12 +++++------ validator/benefactor/kind_box_req/get.go | 10 +++++----- validator/benefactor/kind_box_req/get_all.go | 6 +++--- validator/benefactor/kind_box_req/update.go | 12 +++++------ .../benefactor/kind_box_req/validator.go | 20 ++++++++++--------- 19 files changed, 119 insertions(+), 111 deletions(-) diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go index 9b3909a..5efab62 100644 --- a/validator/admin/kind_box/add.go +++ b/validator/admin/kind_box/add.go @@ -1,4 +1,4 @@ -package adminkindbox +package adminkindboxvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" @@ -9,21 +9,21 @@ import ( ) func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[string]string, error) { - const op = "adminkindbox.KindBoxAddRequest" + const op = "adminkindboxvalidator.KindBoxAddRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.SenderID, validation.Required, - validation.By(v.doesEmployeeExist(req.SenderID))), + validation.By(v.doesEmployeeExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/delete.go b/validator/admin/kind_box/delete.go index 8878b67..fc37e80 100644 --- a/validator/admin/kind_box/delete.go +++ b/validator/admin/kind_box/delete.go @@ -1,4 +1,4 @@ -package adminkindbox +package adminkindboxvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" @@ -8,18 +8,18 @@ import ( ) func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) { - const op = "adminkindbox.ValidateDeleteRequest" + const op = "adminkindboxvalidator.ValidateDeleteRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.hasPendingStatus(req.KindBoxID)), - validation.By(v.doesKindBoxExist(req.KindBoxID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), + validation.By(v.hasPendingStatus), + validation.By(v.doesKindBoxExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/get.go b/validator/admin/kind_box/get.go index 78ca1f9..c50c6fc 100644 --- a/validator/admin/kind_box/get.go +++ b/validator/admin/kind_box/get.go @@ -1,4 +1,4 @@ -package adminkindbox +package adminkindboxvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" @@ -8,17 +8,17 @@ import ( ) func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) { - const op = "adminkindbox.ValidateGetRequest" + const op = "adminkindboxvalidator.ValidateGetRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID)), - validation.By(v.doesKindBoxExist(&req.KindBoxID))), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), + validation.By(v.doesKindBoxExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/update.go b/validator/admin/kind_box/update.go index 80d7956..5813bcd 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -1,4 +1,4 @@ -package adminkindbox +package adminkindboxvalidator import ( "git.gocasts.ir/ebhomengo/niki/entity" @@ -10,18 +10,18 @@ import ( ) func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) { - const op = "adminkindbox.ValidateUpdateRequest" + const op = "adminkindboxvalidator.ValidateUpdateRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(req.BenefactorID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), - + validation.By(v.doesKindBoxExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), + validation.Field(&req.SerialNumber, is.Alphanumeric), validation.Field(&req.TotalAmount, validation.Min(0)), diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index de7d8d3..692de58 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -1,9 +1,10 @@ -package adminkindbox +package adminkindboxvalidator import ( "fmt" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" ) type Repository interface { @@ -64,15 +65,16 @@ func (v Validator) doesKindBoxExist(value interface{}) error { return nil } -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { - kbId := kindBoxID.(uint) - bId := benefactorID.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } +func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { + return func(value interface{}) error { + kbId := value.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } - return nil + return nil + } } func (v Validator) hasCorrectStatus(value interface{}) error { diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index d20ad22..7ea2da2 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -1,4 +1,4 @@ -package adminkindboxreq +package adminkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" @@ -8,7 +8,7 @@ import ( ) func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { - const op = "adminkindboxreq.ValidateAddRequest" + const op = "adminkindboxreqvalidator.ValidateAddRequest" if err := validation.ValidateStruct(&req, @@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist(req.TypeID))), + validation.By(v.doesTypeExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go index b47a556..9555eaf 100644 --- a/validator/admin/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -1,4 +1,4 @@ -package adminkindboxreq +package adminkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" @@ -8,18 +8,18 @@ import ( ) func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) { - const op = "adminkindboxreq.ValidateDeleteRequest" + const op = "adminkindboxreqvalidator.ValidateDeleteRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.hasPendingStatus(req.KindBoxReqID)), - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.hasPendingStatus), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go index debff13..068e227 100644 --- a/validator/admin/kind_box_req/get.go +++ b/validator/admin/kind_box_req/get.go @@ -1,4 +1,4 @@ -package adminkindboxreq +package adminkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" @@ -8,17 +8,17 @@ import ( ) func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { - const op = "adminkindboxreq.ValidateGetRequest" + const op = "adminkindboxreqvalidator.ValidateGetRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index baf6c34..89ac9ea 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -1,4 +1,4 @@ -package adminkindboxreq +package adminkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" @@ -8,20 +8,20 @@ import ( ) func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { - const op = "adminkindboxreq.ValidateUpdateRequest" + const op = "adminkindboxreqvalidator.ValidateUpdateRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.hasPendingStatus(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.hasPendingStatus), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.Field(&req.TypeID, validation.By(v.doesTypeExist(req.TypeID))), diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 1729a15..17a978b 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -1,9 +1,10 @@ -package adminkindboxreq +package adminkindboxreqvalidator import ( "fmt" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" ) const ( @@ -37,15 +38,16 @@ func (v Validator) doesBenefactorExist(value interface{}) error { return nil } -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { - kbId := kindBoxID.(uint) - bId := benefactorID.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } +func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { + return func(value interface{}) error { + kbId := value.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } - return nil + return nil + } } func (v Validator) doesKindBoxRequestExist(value interface{}) error { diff --git a/validator/benefactor/kind_box/get.go b/validator/benefactor/kind_box/get.go index 35e9978..d8fa4cd 100644 --- a/validator/benefactor/kind_box/get.go +++ b/validator/benefactor/kind_box/get.go @@ -1,4 +1,4 @@ -package userkindbox +package userkindboxvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" @@ -8,17 +8,17 @@ import ( ) func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) { - const op = "userkindbox.ValidateGetRequest" + const op = "userkindboxvalidator.ValidateGetRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(req.KindBoxID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), + validation.By(v.doesKindBoxExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box/get_all.go b/validator/benefactor/kind_box/get_all.go index b49593e..46a74b0 100644 --- a/validator/benefactor/kind_box/get_all.go +++ b/validator/benefactor/kind_box/get_all.go @@ -1,4 +1,4 @@ -package userkindbox +package userkindboxvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" @@ -8,12 +8,12 @@ import ( ) func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[string]string, error) { - const op = "userkindbox.ValidateGetAllRequest" + const op = "userkindboxvalidator.ValidateGetAllRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box/validator.go b/validator/benefactor/kind_box/validator.go index 6fede4f..40fb16c 100644 --- a/validator/benefactor/kind_box/validator.go +++ b/validator/benefactor/kind_box/validator.go @@ -1,9 +1,10 @@ -package userkindbox +package userkindboxvalidator import ( "fmt" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" ) type Repository interface { @@ -40,13 +41,14 @@ func (v Validator) doesKindBoxExist(value interface{}) error { return nil } -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { - kbId := kindBoxID.(uint) - bId := benefactorID.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } +func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { + return func(value interface{}) error { + kbId := value.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } - return nil + return nil + } } diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index 0e08c26..6aa19e1 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -1,4 +1,4 @@ -package userkindboxreq +package userkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -8,7 +8,7 @@ import ( ) func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { - const op = "userkindboxreq.ValidateAddRequest" + const op = "userkindboxreqvalidator.ValidateAddRequest" if err := validation.ValidateStruct(&req, @@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist(req.TypeID))), + validation.By(v.doesTypeExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go index bae2a01..021ecec 100644 --- a/validator/benefactor/kind_box_req/delete.go +++ b/validator/benefactor/kind_box_req/delete.go @@ -1,4 +1,4 @@ -package userkindboxreq +package userkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -8,18 +8,18 @@ import ( ) func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) { - const op = "userkindboxreq.ValidateDeleteRequest" + const op = "userkindboxreqvalidator.ValidateDeleteRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.hasPendingStatus(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.hasPendingStatus), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/get.go b/validator/benefactor/kind_box_req/get.go index c8e2aa2..bf87f15 100644 --- a/validator/benefactor/kind_box_req/get.go +++ b/validator/benefactor/kind_box_req/get.go @@ -1,4 +1,4 @@ -package userkindboxreq +package userkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -8,17 +8,17 @@ import ( ) func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { - const op = "userkindboxreq.ValidateGetRequest" + const op = "userkindboxreqvalidator.ValidateGetRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/get_all.go b/validator/benefactor/kind_box_req/get_all.go index 15de7ca..58fd30a 100644 --- a/validator/benefactor/kind_box_req/get_all.go +++ b/validator/benefactor/kind_box_req/get_all.go @@ -1,4 +1,4 @@ -package userkindboxreq +package userkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -8,12 +8,12 @@ import ( ) func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map[string]string, error) { - const op = "userkindboxreq.ValidateGetAllRequest" + const op = "userkindboxreqvalidator.ValidateGetAllRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 603f5ec..4b42110 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -1,4 +1,4 @@ -package userkindboxreq +package userkindboxreqvalidator import ( param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -8,20 +8,20 @@ import ( ) func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { - const op = "userkindboxreq.ValidateUpdateRequest" + const op = "userkindboxreqvalidator.ValidateUpdateRequest" if err := validation.ValidateStruct(&req, validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(req.BenefactorID))), + validation.By(v.doesBenefactorExist)), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), - validation.By(v.hasPendingStatus(req.KindBoxReqID)), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist), + validation.By(v.hasPendingStatus), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.Field(&req.TypeID, validation.Required, diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index be7e6f1..bde8aac 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -1,9 +1,10 @@ -package userkindboxreq +package userkindboxreqvalidator import ( "fmt" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" ) const ( @@ -57,15 +58,16 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { return nil } -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { - kbId := kindBoxID.(uint) - bId := benefactorID.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } +func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { + return func(value interface{}) error { + kbId := value.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } - return nil + return nil + } } func (v Validator) hasPendingStatus(value interface{}) error {