validator

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EmailErr       = "Invalid email address"
	PasswordErr    = "" /* 140-byte string literal not displayed */
	UUIDErr        = "Invalid UUID reference"
	HatchetNameErr = "Hatchet names must match the regex ^[a-zA-Z0-9\\.\\-_]+$"
	ActionIDErr    = "Invalid action ID. Action IDs must be in the format <integrationId>:<verb>"
	CronErr        = "Invalid cron expression"
)

Variables

View Source
var CronRegex = regexp.MustCompile(`(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`) //nolint:gosimple
View Source
var NameRegex = regexp.MustCompile("^[a-zA-Z0-9\\.\\-_]+$") //nolint:gosimple

Functions

func IsValidUUID

func IsValidUUID(u string) bool

func NewErrFailedRequestValidation

func NewErrFailedRequestValidation(valErrors ...string) error

Types

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{}) error

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

func (*DefaultValidator) ValidateAPI

func (v *DefaultValidator) ValidateAPI(s interface{}) (*gen.APIErrors, error)

type ValidationErrObject

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

	// Namespace contains a path to the field which has a validation error
	Namespace 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(suffix string) 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{}) error

	ValidateAPI(s interface{}) (*gen.APIErrors, error)
}

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