requestutils

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 12 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(valError 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
}

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.

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 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 validator.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

Jump to

Keyboard shortcuts

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