Skip to content

Commit

Permalink
feat: added FieldError struct and modified NewFieldErrorsBodyData fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
ralvarezdev committed Jan 13, 2025
1 parent 83a7834 commit ae4e8b2
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions http/response/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package response

import (
goflagsmode "github.com/ralvarezdev/go-flags/mode"
gostringsconvert "github.com/ralvarezdev/go-strings/convert"
)

type (
Expand All @@ -20,6 +19,12 @@ type (
Message *string `json:"message,omitempty"`
Code *int `json:"code,omitempty"`
}

// FieldError struct
FieldError struct {
Field string
Err error
}
)

// NewJSendSuccessBody creates a new success response body
Expand Down Expand Up @@ -154,22 +159,43 @@ func NewErrorResponse(
return NewDebugErrorResponse(err, err, data, errorCode, httpStatus)
}

// NewFieldBodyData creates a new field body data
func NewFieldBodyData(
fieldName string,
fieldValue ...interface{},
) *map[string]interface{} {
return &map[string]interface{}{
fieldName: &[]interface{}{fieldValue},
// NewFieldError creates a new field error
func NewFieldError(
field string,
err error,
) *FieldError {
return &FieldError{
Field: field,
Err: err,
}
}

// NewFieldErrorsBodyData creates a new single field errors body data
// NewFieldErrorsBodyData creates a new field errors body data
func NewFieldErrorsBodyData(
fieldName string,
fieldValue ...error,
fieldErrors ...FieldError,
) *map[string]*[]string {
return &map[string]*[]string{
fieldName: gostringsconvert.ErrorArrayToStringArray(&fieldValue),
// Check if there are field errors
if len(fieldErrors) == 0 {
return nil
}

// Initialize the field errors map
fieldErrorsMap := make(map[string]*[]string)

// Iterate over the field errors
for _, fieldError := range fieldErrors {
// Check if the field name exists in the map
if _, ok := fieldErrorsMap[fieldError.Field]; !ok {
// Initialize the field errors slice
fieldErrorsMap[fieldError.Field] = &[]string{fieldError.Err.Error()}
} else {
// Append the error to the field errors slice
*fieldErrorsMap[fieldError.Field] = append(
*fieldErrorsMap[fieldError.Field],
fieldError.Err.Error(),
)
}
}

return &fieldErrorsMap
}

0 comments on commit ae4e8b2

Please sign in to comment.