Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added request validation and automatic default injection #31

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MoritzHeld
Copy link
Collaborator

No description provided.

@MarvinJWendt MarvinJWendt marked this pull request as draft February 24, 2025 15:26
@MarvinJWendt MarvinJWendt changed the title Feat/validation and defaults feat: validation and defaults Feb 24, 2025
@MarvinJWendt MarvinJWendt changed the title feat: validation and defaults feat: added request validation and automatic default injection Feb 24, 2025
Comment on lines +13 to +39
if err := s.validator.Struct(data); err != nil {
var invalidValidationError *validator.InvalidValidationError
if errors.As(err, &invalidValidationError) {
sendJSON(w, http.StatusBadRequest, api.ValidationError{
Message: "Validation failed: illegal argument was passed",
StatusCode: http.StatusBadRequest,
})
}

var result api.ValidationMessages
var errorsMap validator.ValidationErrors
errors.As(err, &errorsMap)
translatedErrors := errorsMap.Translate(s.translator)
for k, v := range translatedErrors {
x := strings.Split(k, ".")
result = append(result, api.ValidationMessage{
Field: strings.ToLower(x[len(x)-1]),
Message: v,
})
}
sendJSON(w, http.StatusBadRequest, api.ValidationError{
Message: "Validation failed",
StatusCode: http.StatusBadRequest,
Errors: &result,
})
return false
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little too complex for a short statement if clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants