add ProfessionInput sanitization
This commit is contained in:
parent
cc93a57af9
commit
75253f2c02
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
@ -44,6 +45,19 @@ func (input *ProfessionInput) IsEmpty() bool {
|
|||
return input == nil && input.Name == nil && input.Description == nil
|
||||
}
|
||||
|
||||
func (input *ProfessionInput) Sanitize() *ProfessionInput {
|
||||
if input.Name != nil {
|
||||
trimmed := strings.TrimSpace(*input.Name)
|
||||
input.Name = &trimmed
|
||||
}
|
||||
if input.Description != nil {
|
||||
trimmed := strings.TrimSpace(*input.Description)
|
||||
input.Description = &trimmed
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
func (input *ProfessionInput) ToProfession() *Profession {
|
||||
p := &Profession{}
|
||||
if input.Name != nil {
|
||||
|
|
|
@ -27,7 +27,7 @@ func New(cfg *Config) (profession.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *models.ProfessionInput) (*models.Profession, error) {
|
||||
if err := ucase.validateInput(input, validateOptions{false}); err != nil {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.professionRepository.Store(ctx, input)
|
||||
|
@ -37,7 +37,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.P
|
|||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
}
|
||||
if err := ucase.validateInput(input, validateOptions{true}); err != nil {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := ucase.professionRepository.UpdateMany(ctx,
|
||||
|
|
Reference in New Issue