From d284844be56529a4be7e64b9ce7f24de67d3cf8c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 20 Jul 2025 15:24:45 +0200 Subject: [PATCH] feat(validation/notblank): improve validation and messages --- validation/notblank.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/validation/notblank.go b/validation/notblank.go index 45460b9..38463fc 100644 --- a/validation/notblank.go +++ b/validation/notblank.go @@ -7,15 +7,25 @@ import ( ) type NotBlank struct { + Message string + TypeErrorMessage string +} + +func NewNotBlank() NotBlank { + return NotBlank{ + Message: "This value should not be blank.", + TypeErrorMessage: "This value can not be processed.", + } } func (c NotBlank) Validate(data any) []Error { isValid := true - label := "This value should not be blank." errors := []Error{} - if data == nil { - errors = append(errors, Error(label)) + v := reflect.ValueOf(data) + + if v.IsZero() { + errors = append(errors, Error(c.Message)) return errors } @@ -48,11 +58,11 @@ func (c NotBlank) Validate(data any) []Error { case reflect.Uint8: isValid = cast.ToFloat64(data.(string)) == float64(0) default: - errors = append(errors, Error("This value can not be processed.")) + errors = append(errors, Error(c.TypeErrorMessage)) } if !isValid { - errors = append(errors, Error(label)) + errors = append(errors, Error(c.Message)) } return errors