handlerutils

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetURLParamString

func GetURLParamString(r *http.Request, param types.URLParam) (string, apierrors.RequestError)

GetURLParamString returns a specific URL parameter as a string using chi.URLParam. It returns an internal server error if the URL parameter is not found.

func GetURLParamUint

func GetURLParamUint(r *http.Request, param types.URLParam) (uint, apierrors.RequestError)

GetURLParamUint returns a URL parameter as a uint. It returns an internal server error if the URL parameter is not found.

func NewErrFailedRequestValidation

func NewErrFailedRequestValidation(valErrors ...string) apierrors.RequestError

Types

type Decoder

type Decoder interface {
	// Decode accepts a target struct, a reader for the request body, and a URL
	// for the request endpoint
	Decode(s interface{}, r *http.Request) apierrors.RequestError

	// DecodeQueryOnly is Decode but only looks at the query parameters
	DecodeQueryOnly(s interface{}, r *http.Request) apierrors.RequestError
}

Decoder populates a request form from the request body and URL.

func NewDefaultDecoder

func NewDefaultDecoder() Decoder

NewDefaultDecoder returns an implementation of Decoder that uses `json` and `gorilla/schema`.

type DefaultDecoder

type DefaultDecoder struct {
	// contains filtered or unexported fields
}

DefaultDecoder decodes the request body with `json` and the URL query params with gorilla/schema,

func (*DefaultDecoder) Decode

func (d *DefaultDecoder) Decode(
	s interface{},
	r *http.Request,
) (reqErr apierrors.RequestError)

Decode reads the request and populates the target request object.

func (*DefaultDecoder) DecodeQueryOnly

func (d *DefaultDecoder) DecodeQueryOnly(
	s interface{},
	r *http.Request,
) (reqErr apierrors.RequestError)

Decode reads the request and populates the target request object.

type DefaultRequestDecoderValidator

type DefaultRequestDecoderValidator struct {
	// contains filtered or unexported fields
}

func (*DefaultRequestDecoderValidator) DecodeAndValidate

func (j *DefaultRequestDecoderValidator) DecodeAndValidate(
	w http.ResponseWriter,
	r *http.Request,
	v interface{},
) (ok bool)

func (*DefaultRequestDecoderValidator) DecodeAndValidateNoWrite

func (j *DefaultRequestDecoderValidator) DecodeAndValidateNoWrite(
	r *http.Request,
	v interface{},
) error

func (*DefaultRequestDecoderValidator) DecodeAndValidateQueryOnly

func (j *DefaultRequestDecoderValidator) DecodeAndValidateQueryOnly(
	w http.ResponseWriter,
	r *http.Request,
	v interface{},
) (ok bool)

type DefaultResultWriter

type DefaultResultWriter struct {
	// contains filtered or unexported fields
}

default generalizes response codes for common operations (http.StatusOK, http.StatusCreated, etc)

func (*DefaultResultWriter) WriteResult

func (j *DefaultResultWriter) WriteResult(w http.ResponseWriter, r *http.Request, v interface{})

type DefaultValidator

type DefaultValidator struct {
	// contains filtered or unexported fields
}

DefaultValidator uses the go-playground v10 validator for verifying that request objects are well-formed

func (*DefaultValidator) Validate

func (v *DefaultValidator) Validate(s interface{}) apierrors.RequestError

Validate uses the go-playground v10 validator and checks struct fields against a `form:"<validator>"` tag.

type RequestDecoderValidator

type RequestDecoderValidator interface {
	DecodeAndValidate(w http.ResponseWriter, r *http.Request, v interface{}) bool
	DecodeAndValidateQueryOnly(w http.ResponseWriter, r *http.Request, v interface{}) bool
	DecodeAndValidateNoWrite(r *http.Request, v interface{}) error
}

func NewDefaultRequestDecoderValidator

func NewDefaultRequestDecoderValidator(
	logger logger.Logger,
	alerter erroralerter.Alerter,
) RequestDecoderValidator

type ResultWriter

type ResultWriter interface {
	WriteResult(w http.ResponseWriter, r *http.Request, v interface{})
}

func NewDefaultResultWriter

func NewDefaultResultWriter(
	logger logger.Logger,
	alerter erroralerter.Alerter,
) ResultWriter

type ValidationErrObject

type ValidationErrObject struct {
	// Field is the request field that has a validation error.
	Field string

	// Condition is the condition that was not satisfied, resulting in the validation
	// error
	Condition string

	// Param is an optional field that shows a parameter that was not satisfied. For example,
	// the field value was not found in the set [ "value1", "value2" ], so "value1", "value2"
	// is the parameter in this case.
	Param string

	// ActualValue is the actual value of the field that failed validation.
	ActualValue interface{}
}

ValidationErrObject represents an error referencing a specific field in a struct that must match a specific condition. This object is modeled off of the go-playground v10 validator `FieldError` type, but can be used generically for any request validation issues that occur downstream.

func NewValidationErrObject

func NewValidationErrObject(fieldErr v10Validator.FieldError) *ValidationErrObject

NewValidationErrObject simply returns a ValidationErrObject from a go-playground v10 validator `FieldError`

func (*ValidationErrObject) SafeExternalError

func (obj *ValidationErrObject) SafeExternalError() string

SafeExternalError converts the ValidationErrObject to a string that is readable and safe to send externally. In this case, "safe" means that when the `ActualValue` field is cast to a string, it is type-checked so that only certain types are passed to the user. We don't want an upstream command accidentally setting a complex object in the request field that could leak sensitive information to the user. To limit this, we only support sending static `ActualValue` types: `string`, `int`, `[]string`, and `[]int`. Otherwise, we say that the actual value is "invalid type".

Note: the test cases split on "," to parse out the different errors. Don't add commas to the safe external error.

type Validator

type Validator interface {
	// Validate accepts a generic struct for validating. It returns a request
	// error that is meant to be shown to the end user as a readable string.
	Validate(s interface{}) apierrors.RequestError
}

Validator will validate the fields for a request object to ensure that the request is well-formed. For example, it searches for required fields or verifies that fields are of a semantic type (like email)

func NewDefaultValidator

func NewDefaultValidator() Validator

NewDefaultValidator returns a Validator constructed from the go-playground v10 validator

type ValidatorErr

type ValidatorErr string
const (
	EmailErr       ValidatorErr = "Invalid email address"
	PasswordErr    ValidatorErr = "" /* 140-byte string literal not displayed */
	UUIDErr        ValidatorErr = "Invalid UUID reference"
	HatchetNameErr ValidatorErr = "Hatchet names must match the regex ^[a-zA-Z0-9\\.\\-_]+$"
)

Jump to

Keyboard shortcuts

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