From ba4ebbcb84cd1215ea258ad65eac998eecabe67a Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Tue, 21 May 2024 21:08:43 +0330 Subject: [PATCH] feat(validator): add validation for kindbox request refer date --- validator/benefactor/kind_box_req/add.go | 2 +- validator/benefactor/kind_box_req/validator.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index b40385d..aa94a51 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -2,7 +2,6 @@ package benefactorkindboxreqvalidator import ( "errors" - 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" @@ -30,6 +29,7 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) *Validator validation.Field(&req.DeliverReferDate, validation.Required, + validation.By(v.isDateValid), ), ); err != nil { diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index d07cbf7..4a99066 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -3,12 +3,12 @@ package benefactorkindboxreqvalidator import ( "context" "fmt" - "git.gocasts.ir/ebhomengo/niki/entity" addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" + "time" ) const ( @@ -94,6 +94,22 @@ func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc { } } +func (v Validator) isDateValid(value interface{}) error { + date, ok := value.(string) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + t, err := time.Parse(time.DateTime, date) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgInvalidInput) + } + if t.Before(time.Now()) { + return fmt.Errorf(errmsg.ErrorMsgInvalidInput) + } + + return nil +} + // func (v Validator) doesKindBoxRequestExist(value interface{}) error { // kindBoxReqID, ok := value.(uint) // if !ok {