validation

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	ErrorResponse *ErrorResponse
}

Error is an error who occured when trying to validate an entity It contains an ErrorResponse which can be used to write an http error message.

func GetUnmarshalError

func GetUnmarshalError(err *json.UnmarshalTypeError) *Error

GetUnmarshalError returns a validation Error based on given UnmarshalTypeError.

func NewError

func NewError(errorResponse *ErrorResponse) *Error

NewError returns an error based on a validation error response.

func (*Error) Error

func (validationError *Error) Error() string

type ErrorDetail

type ErrorDetail struct {
	Field  string `json:"field"`
	Reason string `json:"reason"`
}

ErrorDetail details an error.

func NewParameterErrorDetail

func NewParameterErrorDetail(field, reason string) ErrorDetail

NewParameterErrorDetail returns an error struct containing which field and the reason behind the error.

func (*ErrorDetail) MarshalJSON

func (errorDetail *ErrorDetail) MarshalJSON() ([]byte, error)

MarshalJSON marshals a JSON string from the ErrorResponse.

type ErrorResponse

type ErrorResponse struct {
	Title   string        `json:"title"`
	Detail  string        `json:"detail,omitempty"`
	Status  int           `json:"status"`
	Type    string        `json:"type"`
	Details []ErrorDetail `json:"details,omitempty"`
}

ErrorResponse is a generic response type for errors in HTTP requests.

func NewErrorResponse

func NewErrorResponse(statusCode int, details ...ErrorDetail) *ErrorResponse

NewErrorResponse returns a new ErrorResponse based on given status code and details.

func (*ErrorResponse) MarshalJSON

func (errorResponse *ErrorResponse) MarshalJSON() ([]byte, error)

MarshalJSON marshals a JSON string from the ErrorResponse.

func (*ErrorResponse) WriteHTTPError

func (errorResponse *ErrorResponse) WriteHTTPError(writer http.ResponseWriter)

WriteHTTPError writes error to a writer as JSON.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL