Documentation
¶
Index ¶
- type Error
- type ErrorDetail
- type ErrorResponse
- type Rule
- func Custom[T any](fn func(T) bool, message string) Rule[T]
- func Email() Rule[string]
- func Max(maximum int) Rule[int]
- func MaxLength(maximum int) Rule[string]
- func Min(minimum int) Rule[int]
- func MinLength(minimum int) Rule[string]
- func Range(minimum, maximum int) Rule[int]
- func Required() Rule[string]
- type StructValidator
- type Validator
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 ValidateStringAsInt ¶ added in v0.2.4
ValidateStringAsInt validates a string can be converted to int and applies int validation.
type ErrorDetail ¶
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.
type Rule ¶ added in v0.2.4
Rule represents a single validation rule.
type StructValidator ¶ added in v0.2.4
type StructValidator struct {
// contains filtered or unexported fields
}
StructValidator provides validation for struct fields.
func NewStructValidator ¶ added in v0.2.4
func NewStructValidator() *StructValidator
NewStructValidator creates a new struct validator.
func ValidateStruct ¶ added in v0.2.4
func ValidateStruct() *StructValidator
ValidateStruct creates a new struct validator Example usage:
validator := validation.ValidateStruct().Field("Name", func(v interface{}) *validation.Error {
return validation.Validate[string]().Rule(validation.Required()).Validate(v.(string), "Name")
})
func (*StructValidator) Field ¶ added in v0.2.4
func (sv *StructValidator) Field(fieldName string, validator func(interface{}) *Error) *StructValidator
Field adds a field validator.
func (*StructValidator) Validate ¶ added in v0.2.4
func (sv *StructValidator) Validate(data interface{}) *Error
Validate validates a struct.
type Validator ¶ added in v0.2.4
type Validator[T any] struct { // contains filtered or unexported fields }
Validator provides type-safe validation for various data types.
func NewValidator ¶ added in v0.2.4
NewValidator creates a new type-safe validator.
func Validate ¶ added in v0.2.4
Validate creates a new type-safe validator for the specified type T Example usage:
validator := validation.Validate[string]().Rule(validation.Required()).Rule(validation.MinLength(3))
if err := validator.Validate(userInput, "username"); err != nil { ... }