github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/profiles/domain/validators/validators.go (about)

     1  package validators
     2  
     3  import (
     4  	"github.com/k0marov/go-socnet/core/general/client_errors"
     5  	"github.com/k0marov/go-socnet/core/general/image_decoder"
     6  	"github.com/k0marov/go-socnet/features/profiles/domain/values"
     7  )
     8  
     9  type ProfileUpdateValidator func(values.ProfileUpdateData) (client_errors.ClientError, bool)
    10  type AvatarValidator func(values.AvatarData) (client_errors.ClientError, bool)
    11  
    12  const MaxAboutLength = 255
    13  
    14  func NewProfileUpdateValidator() ProfileUpdateValidator {
    15  	return func(profileUpdate values.ProfileUpdateData) (client_errors.ClientError, bool) {
    16  		if len(profileUpdate.About) > MaxAboutLength {
    17  			return client_errors.AboutTooLong, false
    18  		}
    19  		return client_errors.ClientError{}, true
    20  	}
    21  }
    22  
    23  func NewAvatarValidator(imageDecoder image_decoder.ImageDecoder) AvatarValidator {
    24  	return func(avatar values.AvatarData) (client_errors.ClientError, bool) {
    25  		imageDimensions, err := imageDecoder(avatar.Data.Value())
    26  		if err != nil {
    27  			return client_errors.InvalidImage, false
    28  		}
    29  		if imageDimensions.Height != imageDimensions.Width {
    30  			return client_errors.NonSquareAvatar, false
    31  		}
    32  		return client_errors.ClientError{}, true
    33  	}
    34  }