Skip to content

Commit dd79518

Browse files
Jos-Jerusnirarg
authored andcommitted
NO-JIRA | fix: improve name validation error messages
- Updated ValidateName() error message to include valid format guidance - Added explicit name validation in assessment handler before struct validation - Both RVTools and assessment flows now return consistent descriptive errors Signed-off-by: yjacobi <yjacobi@redhat.com>
1 parent 13027a4 commit dd79518

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

internal/handlers/v1alpha1/assessment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ func (h *ServiceHandler) CreateAssessment(ctx context.Context, request server.Cr
6565
}
6666

6767
form := v1alpha1.AssessmentForm(*request.Body)
68+
69+
// Validate name first for better error message
70+
if err := validator.ValidateName(string(form.Name)); err != nil {
71+
logger.Error(err).WithString("step", "validation").Log()
72+
return server.CreateAssessment400JSONResponse{Message: err.Error()}, nil
73+
}
74+
6875
if err := validateAssessmentData(form); err != nil {
6976
logger.Error(err).WithString("step", "validation").Log()
7077
return server.CreateAssessment400JSONResponse{Message: err.Error()}, nil

internal/handlers/validator/custom.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func nameValidator(fl validator.FieldLevel) bool {
3939
func ValidateName(name string) error {
4040
ok := nameValidRegex.MatchString(name)
4141
if !ok {
42-
return NewErrInvalidName("The provided name: %s is invalid.", name)
42+
return NewErrInvalidName(
43+
"The provided name %q is invalid. Name must be 1-100 characters and contain only letters, numbers, underscores, periods, or hyphens (no spaces).",
44+
name,
45+
)
4346
}
4447

4548
return nil

0 commit comments

Comments
 (0)