refactor: разнести пакеты и обновить валидацию
This commit is contained in:
35
protocol/validation.go
Normal file
35
protocol/validation.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
var validate = validator.New(validator.WithRequiredStructEnabled())
|
||||
|
||||
func Validate(cfg Config, state ContentState) error {
|
||||
cfg = NormalizeConfig(cfg)
|
||||
|
||||
route := strings.TrimSpace(state.Route)
|
||||
template := strings.TrimSpace(state.Template)
|
||||
|
||||
if err := validate.Var(route, "required"); err != nil {
|
||||
return fmt.Errorf("%w: route is required", ErrInvalidData)
|
||||
}
|
||||
if err := validate.Var([]rune(route), fmt.Sprintf("max=%d", cfg.MaxRouteLen)); err != nil {
|
||||
return fmt.Errorf("%w: route must be at most %d characters", ErrInvalidData, cfg.MaxRouteLen)
|
||||
}
|
||||
if err := validate.Var([]rune(state.Text), fmt.Sprintf("max=%d", cfg.MaxTextLen)); err != nil {
|
||||
return fmt.Errorf("%w: str must be at most %d characters", ErrInvalidData, cfg.MaxTextLen)
|
||||
}
|
||||
if err := validate.Var(template, "required"); err != nil {
|
||||
return fmt.Errorf("%w: tpl_name is required", ErrInvalidData)
|
||||
}
|
||||
if err := validate.Var([]rune(template), fmt.Sprintf("max=%d", cfg.MaxTemplateLen)); err != nil {
|
||||
return fmt.Errorf("%w: tpl_name must be at most %d characters", ErrInvalidData, cfg.MaxTemplateLen)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user