forked from ebhomengo/niki
feat(validation): update validation section
This commit is contained in:
parent
d49f1277cc
commit
4697645e64
|
@ -0,0 +1,46 @@
|
|||
package adminkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[string]string, error) {
|
||||
const op = "adminkindbox.KindBoxAddRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.SerialNumber, validation.Required),
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.SenderID,
|
||||
validation.Required,
|
||||
validation.By(v.doesEmployeeExist(&req.SenderID))),
|
||||
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -1,25 +1,23 @@
|
|||
package kindbox
|
||||
package adminkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
|
||||
const op = "kindbox.ValidateUpdateRequest"
|
||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
|
||||
const op = "adminkindbox.ValidateDeleteRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.KindBox.SerialNumber, validation.Required),
|
||||
|
||||
validation.Field(&req.KindBox.ReceiverID,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesReceiverUserExist)),
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBox.SenderID,
|
||||
validation.Field(&req.KindBoxID,
|
||||
validation.Required,
|
||||
validation.By(v.doesSenderUserExist)),
|
||||
validation.By(v.doesKindBoxExist(&req.KindBoxID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package adminkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) {
|
||||
const op = "adminkindbox.ValidateGetRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxExist(&req.KindBoxID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package adminkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
|
||||
const op = "adminkindbox.ValidateUpdateRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))),
|
||||
|
||||
validation.Field(&req.SerialNumber, validation.EnsureString),
|
||||
|
||||
validation.Field(&req.TotalAmount, validation.Min(0)),
|
||||
|
||||
validation.Field(&req.SenderID,
|
||||
validation.By(v.doesEmployeeExist(req.SenderID))),
|
||||
|
||||
validation.Field(&req.ReceiverID,
|
||||
validation.By(v.doesEmployeeExist(req.ReceiverID))),
|
||||
|
||||
validation.Field(&req.Status,
|
||||
validation.By(v.hasCorrectStatus(req.Status.String))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package adminkindbox
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
KindBoxRequestExist(id uint) (bool, error)
|
||||
EmployeeExist(id uint) (bool, error)
|
||||
BenefactorExist(id uint) (bool, error)
|
||||
KindBoxExist(id uint) (bool, error)
|
||||
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
|
||||
CheckStatus(status string) (bool, error)
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func New(repo Repository) Validator {
|
||||
return Validator{repo: repo}
|
||||
}
|
||||
|
||||
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
|
||||
receiverID := value.(uint)
|
||||
_, err := v.repo.KindBoxRequestExist(receiverID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesEmployeeExist(value interface{}) error {
|
||||
senderID := value.(uint)
|
||||
_, err := v.repo.EmployeeExist(senderID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesBenefactorExist(value interface{}) error {
|
||||
benefactorID := value.(uint)
|
||||
_, err := v.repo.BenefactorExist(benefactorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesKindBoxExist(value interface{}) error {
|
||||
kindboxID := value.(uint)
|
||||
_, err := v.repo.KindBoxExist(kindboxID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) hasCorrectStatus(value interface{}) error {
|
||||
status := value.(string)
|
||||
_, err := v.repo.CheckStatus(status)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,14 +1,14 @@
|
|||
package kindboxreq
|
||||
package adminkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
|
||||
const op = "kindboxreq.ValidateAddRequest"
|
||||
const op = "adminkindboxreq.ValidateAddRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
|
||||
|
@ -16,7 +16,7 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
|
|||
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBeneFactorExist)),
|
||||
validation.By(v.doesBenefactorExist)),
|
||||
|
||||
validation.Field(&req.TypeID,
|
||||
validation.Required,
|
|
@ -1,16 +1,24 @@
|
|||
package kindboxreq
|
||||
package adminkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
|
||||
const op = "kindboxreq.ValidateDeleteRequest"
|
||||
const op = "adminkindboxreq.ValidateDeleteRequest"
|
||||
|
||||
if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil {
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
|
@ -1,16 +1,24 @@
|
|||
package kindboxreq
|
||||
package adminkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetByIDRequest) (map[string]string, error) {
|
||||
const op = "kindboxreq.ValidateGetByIDRequest"
|
||||
func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[string]string, error) {
|
||||
const op = "adminkindboxreq.ValidateGetRequest"
|
||||
|
||||
if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil {
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
|
@ -1,25 +1,28 @@
|
|||
package kindboxreq
|
||||
package adminkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
|
||||
const op = "kindboxreq.ValidateUpdateRequest"
|
||||
const op = "adminkindboxreq.ValidateUpdateRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
|
||||
validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
|
||||
|
||||
validation.Field(&req.KindBoxReq.BenefactorID,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBeneFactorExist)),
|
||||
validation.By(v.doesBenefactorExist(req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxReq.TypeID,
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesTypeExist)),
|
||||
validation.By(v.doesKindBoxBelongToBenefactor(req.KindBoxReqID, req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.TypeID,
|
||||
validation.By(v.doesTypeExist(req.TypeID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package adminkindboxreq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
)
|
||||
|
||||
const (
|
||||
MinKindBoxReq = 1
|
||||
MaxKindBoxReq = 100
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
BenefactorExist(id int) (bool, error)
|
||||
KindBoxRequestExist(id int) (bool, error)
|
||||
TypeExist(id int) (bool, error)
|
||||
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func New(repo Repository) Validator {
|
||||
return Validator{repo: repo}
|
||||
}
|
||||
|
||||
func (v Validator) doesBenefactorExist(value interface{}) error {
|
||||
benefactorID := value.(int)
|
||||
_, err := v.repo.BenefactorExist(benefactorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
|
||||
kindboxreqID := value.(int)
|
||||
_, err := v.repo.KindBoxRequestExist(kindboxreqID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesTypeExist(value interface{}) error {
|
||||
typeID := value.(int)
|
||||
_, err := v.repo.TypeExist(typeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package userkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[string]string, error) {
|
||||
const op = "userkindbox.ValidateGetRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxExist(&req.KindBoxID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -1,16 +1,20 @@
|
|||
package kindbox
|
||||
package userkindbox
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
|
||||
const op = "kindbox.ValidateDeleteRequest"
|
||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[string]string, error) {
|
||||
const op = "userkindbox.ValidateGetAllRequest"
|
||||
|
||||
if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
|
@ -1,4 +1,4 @@
|
|||
package kindbox
|
||||
package userkindbox
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -7,8 +7,8 @@ import (
|
|||
)
|
||||
|
||||
type Repository interface {
|
||||
ReceiverUserExist(id int) (bool, error)
|
||||
SenderUserExist(id int) (bool, error)
|
||||
BenefactorExist(id int) (bool, error)
|
||||
KindBoxExist(id int) (bool, error)
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
|
@ -19,9 +19,9 @@ func New(repo Repository) Validator {
|
|||
return Validator{repo: repo}
|
||||
}
|
||||
|
||||
func (v Validator) doesReceiverUserExist(value interface{}) error {
|
||||
receiverID := value.(int)
|
||||
_, err := v.repo.ReceiverUserExist(receiverID)
|
||||
func (v Validator) doesBenefactorExist(value interface{}) error {
|
||||
benefactorID := value.(int)
|
||||
_, err := v.repo.BenefactorExist(benefactorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
@ -29,9 +29,9 @@ func (v Validator) doesReceiverUserExist(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesSenderUserExist(value interface{}) error {
|
||||
senderID := value.(int)
|
||||
_, err := v.repo.SenderUserExist(senderID)
|
||||
func (v Validator) doesKindBoxExist(value interface{}) error {
|
||||
kindBoxID := value.(int)
|
||||
_, err := v.repo.KindBoxExist(kindBoxID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package userkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
|
||||
const op = "userkindboxreq.ValidateAddRequest"
|
||||
|
||||
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.Field(&req.TypeID,
|
||||
validation.Required,
|
||||
validation.By(v.doesTypeExist(&req.TypeID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package userkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
|
||||
const op = "userkindboxreq.ValidateDeleteRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package userkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) {
|
||||
const op = "userkindboxreq.ValidateGetRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
|
||||
validation.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -1,16 +1,20 @@
|
|||
package kindbox
|
||||
package userkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) {
|
||||
const op = "kindbox.ValidateGetByIDRequest"
|
||||
func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map[string]string, error) {
|
||||
const op = "userkindboxreq.ValidateGetAllRequest"
|
||||
|
||||
if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.BenefactorID,
|
||||
validation.Required,
|
||||
validation.By(v.doesBenefactorExist(&req.BenefactorID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
|
@ -0,0 +1,47 @@
|
|||
package userkindboxreq
|
||||
|
||||
import (
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
|
||||
const op = "userkindboxreq.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.Field(&req.KindBoxReqID,
|
||||
validation.Required,
|
||||
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))),
|
||||
|
||||
validation.Field(&req.TypeID,
|
||||
validation.Required,
|
||||
validation.By(v.doesTypeExist(&req.TypeID))),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
errV, ok := err.(validation.Errors)
|
||||
if ok {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package kindboxreq
|
||||
package userkindboxreq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -12,7 +12,8 @@ const (
|
|||
)
|
||||
|
||||
type Repository interface {
|
||||
BeneFactorExist(id int) (bool, error)
|
||||
BenefactorExist(id int) (bool, error)
|
||||
KindBoxReqExist(id int) (bool, error)
|
||||
TypeExist(id int) (bool, error)
|
||||
}
|
||||
|
||||
|
@ -24,9 +25,9 @@ func New(repo Repository) Validator {
|
|||
return Validator{repo: repo}
|
||||
}
|
||||
|
||||
func (v Validator) doesBeneFactorExist(value interface{}) error {
|
||||
func (v Validator) doesBenefactorExist(value interface{}) error {
|
||||
benefactorID := value.(int)
|
||||
_, err := v.repo.BeneFactorExist(benefactorID)
|
||||
_, err := v.repo.BenefactorExist(benefactorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
@ -43,3 +44,13 @@ func (v Validator) doesTypeExist(value interface{}) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
|
||||
kindBoxReqID := value.(int)
|
||||
_, err := v.repo.KindBoxReqExist(kindBoxReqID)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue