feat(validation): fix Custom Rules function

This commit is contained in:
miaad shahrokhi 2023-12-30 23:21:22 +03:30
parent f7fb18a501
commit 0b48cb4971
19 changed files with 119 additions and 111 deletions

View File

@ -1,4 +1,4 @@
package adminkindbox package adminkindboxvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" 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) { func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[string]string, error) {
const op = "adminkindbox.KindBoxAddRequest" const op = "adminkindboxvalidator.KindBoxAddRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric),
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.SenderID, validation.Field(&req.SenderID,
validation.Required, validation.Required,
validation.By(v.doesEmployeeExist(req.SenderID))), validation.By(v.doesEmployeeExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindbox package adminkindboxvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" 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) { func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
const op = "adminkindbox.ValidateDeleteRequest" const op = "adminkindboxvalidator.ValidateDeleteRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.hasPendingStatus(req.KindBoxID)), validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxExist(req.KindBoxID)), validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindbox package adminkindboxvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" 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) { func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) {
const op = "adminkindbox.ValidateGetRequest" const op = "adminkindboxvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID)), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)),
validation.By(v.doesKindBoxExist(&req.KindBoxID))), validation.By(v.doesKindBoxExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindbox package adminkindboxvalidator
import ( import (
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
@ -10,17 +10,17 @@ import (
) )
func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) { func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
const op = "adminkindbox.ValidateUpdateRequest" const op = "adminkindboxvalidator.ValidateUpdateRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist(req.BenefactorID)), validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.SerialNumber, is.Alphanumeric), validation.Field(&req.SerialNumber, is.Alphanumeric),

View File

@ -1,9 +1,10 @@
package adminkindbox package adminkindboxvalidator
import ( import (
"fmt" "fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
) )
type Repository interface { type Repository interface {
@ -64,15 +65,16 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
kbId := kindBoxID.(uint) return func(value interface{}) error {
bId := benefactorID.(uint) kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
return nil return nil
}
} }
func (v Validator) hasCorrectStatus(value interface{}) error { func (v Validator) hasCorrectStatus(value interface{}) error {

View File

@ -1,4 +1,4 @@
package adminkindboxreq package adminkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" 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) { func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
const op = "adminkindboxreq.ValidateAddRequest" const op = "adminkindboxreqvalidator.ValidateAddRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindboxreq package adminkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" 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) { func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
const op = "adminkindboxreq.ValidateDeleteRequest" const op = "adminkindboxreqvalidator.ValidateDeleteRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.hasPendingStatus(req.KindBoxReqID)), validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindboxreq package adminkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" 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) { func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[string]string, error) {
const op = "adminkindboxreq.ValidateGetRequest" const op = "adminkindboxreqvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package adminkindboxreq package adminkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" 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) { func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
const op = "adminkindboxreq.ValidateUpdateRequest" const op = "adminkindboxreqvalidator.ValidateUpdateRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.hasPendingStatus(req.KindBoxReqID)), validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist(req.TypeID))),

View File

@ -1,9 +1,10 @@
package adminkindboxreq package adminkindboxreqvalidator
import ( import (
"fmt" "fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
) )
const ( const (
@ -37,15 +38,16 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
kbId := kindBoxID.(uint) return func(value interface{}) error {
bId := benefactorID.(uint) kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
return nil return nil
}
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {

View File

@ -1,4 +1,4 @@
package userkindbox package userkindboxvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" 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) { func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) {
const op = "userkindbox.ValidateGetRequest" const op = "userkindboxvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist(req.KindBoxID)), validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package userkindbox package userkindboxvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" 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) { func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[string]string, error) {
const op = "userkindbox.ValidateGetAllRequest" const op = "userkindboxvalidator.ValidateGetAllRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,9 +1,10 @@
package userkindbox package userkindboxvalidator
import ( import (
"fmt" "fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
) )
type Repository interface { type Repository interface {
@ -40,13 +41,14 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
kbId := kindBoxID.(uint) return func(value interface{}) error {
bId := benefactorID.(uint) kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
return nil return nil
}
} }

View File

@ -1,4 +1,4 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" 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) { func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
const op = "userkindboxreq.ValidateAddRequest" const op = "userkindboxreqvalidator.ValidateAddRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" 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) { func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
const op = "userkindboxreq.ValidateDeleteRequest" const op = "userkindboxreqvalidator.ValidateDeleteRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.hasPendingStatus(req.KindBoxReqID)), validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" 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) { func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) {
const op = "userkindboxreq.ValidateGetRequest" const op = "userkindboxreqvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" 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) { func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map[string]string, error) {
const op = "userkindboxreq.ValidateGetAllRequest" const op = "userkindboxreqvalidator.ValidateGetAllRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,4 +1,4 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" 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) { func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
const op = "userkindboxreq.ValidateUpdateRequest" const op = "userkindboxreqvalidator.ValidateUpdateRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), validation.By(v.doesKindBoxRequestExist),
validation.By(v.hasPendingStatus(req.KindBoxReqID)), validation.By(v.hasPendingStatus),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,

View File

@ -1,9 +1,10 @@
package userkindboxreq package userkindboxreqvalidator
import ( import (
"fmt" "fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
) )
const ( const (
@ -57,15 +58,16 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
kbId := kindBoxID.(uint) return func(value interface{}) error {
bId := benefactorID.(uint) kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
return nil return nil
}
} }
func (v Validator) hasPendingStatus(value interface{}) error { func (v Validator) hasPendingStatus(value interface{}) error {