forked from ebhomengo/niki
1
0
Fork 0

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 (
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)

View File

@ -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)

View File

@ -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)

View File

@ -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)),

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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))),

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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,

View File

@ -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 {