2024-07-21 13:10:22 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-24 23:45:04 +00:00
|
|
|
"slices"
|
|
|
|
|
2024-07-21 13:10:22 +00:00
|
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AreSortFieldsValid(validSortFields []string) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
sort, ok := value.(params.SortRequest)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if sort.Field == "" && sort.Direction != "" {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired)
|
|
|
|
}
|
|
|
|
if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc)
|
|
|
|
}
|
|
|
|
if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-07-21 13:10:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|