forked from ebhomengo/niki
29 lines
874 B
Go
29 lines
874 B
Go
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
"slices"
|
|
)
|
|
|
|
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)
|
|
}
|
|
return nil
|
|
}
|
|
}
|