46 lines
737 B
Go
46 lines
737 B
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type LenError struct {
|
|
Min int
|
|
Max int
|
|
Current int
|
|
}
|
|
|
|
var _ ErrorWithParams = LenError{}
|
|
|
|
func (e LenError) Error() string {
|
|
return fmt.Sprintf("sort length must be between %d and %d (current length: %d)", e.Min, e.Max, e.Current)
|
|
}
|
|
|
|
func (e LenError) Code() ErrorCode {
|
|
return ErrorCodeIncorrectInput
|
|
}
|
|
|
|
func (e LenError) Slug() string {
|
|
return "length"
|
|
}
|
|
|
|
func (e LenError) Params() map[string]any {
|
|
return map[string]any{
|
|
"Min": e.Min,
|
|
"Max": e.Max,
|
|
"Current": e.Current,
|
|
}
|
|
}
|
|
|
|
func validateSliceLen[S ~[]E, E any](s S, min, max int) error {
|
|
if l := len(s); l > max || l < min {
|
|
return LenError{
|
|
Min: min,
|
|
Max: max,
|
|
Current: l,
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|