Skip to content

Commit

Permalink
feat(validate): add min/max errors for handling in app
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamelentyev committed Nov 2, 2023
1 parent c6fcb33 commit 07d66c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions validate/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 26 additions & 4 deletions validate/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

0 comments on commit 07d66c7

Please sign in to comment.