From 07d66c7ca86cb5ed756e23147ec0aff17f5511c1 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Thu, 2 Nov 2023 17:03:43 +0300 Subject: [PATCH] feat(validate): add min/max errors for handling in app --- validate/array.go | 4 ++-- validate/error.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/validate/array.go b/validate/array.go index d50304b9a..4904ff36b 100644 --- a/validate/array.go +++ b/validate/array.go @@ -38,10 +38,10 @@ func (t Array) Set() bool { // ValidateLength returns error if array length v is invalid. func (t Array) ValidateLength(v int) error { if t.MaxLengthSet && v > t.MaxLength { - return errors.Errorf("len %d greater than maximum %d", v, t.MaxLength) + return &MaxLengthError{Len: v, MaxLength: t.MaxLength} } if t.MinLengthSet && v < t.MinLength { - return errors.Errorf("len %d less than minimum %d", v, t.MinLength) + return &MinLengthError{Len: v, MinLength: t.MinLength} } return nil diff --git a/validate/error.go b/validate/error.go index c7833bd95..273336599 100644 --- a/validate/error.go +++ b/validate/error.go @@ -48,8 +48,8 @@ type InvalidContentTypeError struct { } // InvalidContentTypeError implements error. -func (i *InvalidContentTypeError) Error() string { - return fmt.Sprintf("unexpected Content-Type: %s", i.ContentType) +func (e *InvalidContentTypeError) Error() string { + return fmt.Sprintf("unexpected Content-Type: %s", e.ContentType) } // InvalidContentType creates new InvalidContentTypeError. @@ -72,9 +72,31 @@ func UnexpectedStatusCode(statusCode int) error { } // UnexpectedStatusCodeError implements error. -func (i *UnexpectedStatusCodeError) Error() string { - return fmt.Sprintf("unexpected status code: %d", i.StatusCode) +func (e *UnexpectedStatusCodeError) Error() string { + return fmt.Sprintf("unexpected status code: %d", e.StatusCode) } // ErrNilPointer reports that use Validate, but receiver pointer is nil. var ErrNilPointer = errors.New("nil pointer") + +// MinLengthError reports that len less than minimum. +type MinLengthError struct { + Len int + MinLength int +} + +// MinLengthError implements error. +func (e *MinLengthError) Error() string { + return fmt.Sprintf("len %d less than minimum %d", e.Len, e.MinLength) +} + +// MaxLengthError reports that len greater than maximum. +type MaxLengthError struct { + Len int + MaxLength int +} + +// MaxLengthError implements error. +func (e *MaxLengthError) Error() string { + return fmt.Sprintf("len %d greater than maximum %d", e.Len, e.MaxLength) +}