Documentation
¶
Index ¶
- Variables
- func GetPathsFromMask(fieldMask ...string) map[string]struct{}
- func IsPathInMask(path string, paths map[string]struct{}) bool
- func NormalizePath(path string) string
- type AddFieldValidationErrOption
- type Arrangement
- func (s *Arrangement[T, U]) Exec(ctx context.Context, message T) (U, *Error)
- func (r *Arrangement[T, U]) WithAuth(act Auther[T]) *Arrangement[T, U]
- func (r *Arrangement[T, U]) WithServe(act Server[T, U]) *Arrangement[T, U]
- func (r *Arrangement[T, U]) WithValidate(fv MessageValidator[T]) *Arrangement[T, U]
- type AuthError
- type Auther
- type Condition
- type DefaultMessageValidator
- func (s *DefaultMessageValidator[T]) AssertEqualTo(path string, value any, equalTo any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) AssertEqualToWhenInMask(path string, value any, equalTo any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) AssertNonZero(path string, value any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) AssertNonZeroWhenInMask(path string, value any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) AssertNotEqualTo(path string, value any, notEqualTo any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) AssertNotEqualToWhenInMask(path string, value any, notEqualTo any) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) CustomValidation(act Validator[T]) *DefaultMessageValidator[T]
- func (s *DefaultMessageValidator[T]) Exec(ctx context.Context, message T) *ValidationErrors
- type Error
- func (e *Error) Error() string
- func (e *Error) GetAuthError() *AuthError
- func (e *Error) GetServeError() *ServeErr
- func (e *Error) GetValidationErrors() *ValidationErrors
- func (e *Error) SetAuthError(err error)
- func (e *Error) SetServeError(err error)
- func (e *Error) SetValidationErrors(err error)
- func (e *Error) ToGrpcStatus() *status.Status
- func (e *Error) Unwrap() error
- type Field
- type FieldError
- type MessageValidator
- type Policy
- type Request
- type Response
- type ServeErr
- type Server
- type ValidationErrors
- func (v *ValidationErrors) AddFieldErr(path string, err error, options ...AddFieldValidationErrOption)
- func (v *ValidationErrors) AsMap() map[string]*FieldError
- func (v *ValidationErrors) Error() string
- func (v ValidationErrors) HasErrors() bool
- func (v ValidationErrors) Paths() []string
- func (v *ValidationErrors) SetCustomValidationErr(err error)
- func (v *ValidationErrors) Unwrap() error
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFieldComparisonFailedNotComparable returned when an equality policy is applied (e.g. AssertNotEqualTo) to an incompatible type ErrFieldComparisonFailedNotComparable = errors.New("equality check failed, types not comparable") // ErrFieldMustEqualFailed returned when the supplied value does not match the target value ErrFieldMustEqualFailed = errors.New("expected values to be equal") // ErrFieldMustEqualFailed returned when the supplied value matches a forbidden value ErrFieldMustNotEqualFailed = errors.New("field set to forbidden value") // ErrFieldMustNotBeZeroFailed returned when the supplied value matches its type's zero-value ErrFieldMustNotBeZeroFailed = errors.New("field set to zero value") )
Functions ¶
func GetPathsFromMask ¶
func IsPathInMask ¶
func NormalizePath ¶
Types ¶
type AddFieldValidationErrOption ¶
type AddFieldValidationErrOption func(*FieldError)
func WithExpectedValue ¶
func WithExpectedValue(value any) AddFieldValidationErrOption
func WithValue ¶
func WithValue(value any) AddFieldValidationErrOption
type Arrangement ¶
type Arrangement[T proto.Message, U any] struct { // action to run before running field validations Auth Auther[T] // validator to validate the incoming message Validate MessageValidator[T] // logic to run if all validations completed successfully -- // typically some business logic Serve Server[T, U] }
Arrangement represents different actions to take during the execution of serving some request
func Arrange ¶
func Arrange[T proto.Message, U any]() *Arrangement[T, U]
Instantiate a new Arrangement to build
func (*Arrangement[T, U]) Exec ¶
func (s *Arrangement[T, U]) Exec(ctx context.Context, message T) (U, *Error)
Exec runs in the following order: 1. Auth 2. Validate 3. Serve The function exits if any error is encountered at any stage
func (*Arrangement[T, U]) WithAuth ¶
func (r *Arrangement[T, U]) WithAuth(act Auther[T]) *Arrangement[T, U]
Add an Auth behavior
func (*Arrangement[T, U]) WithServe ¶
func (r *Arrangement[T, U]) WithServe(act Server[T, U]) *Arrangement[T, U]
Add a Serve behavior
func (*Arrangement[T, U]) WithValidate ¶
func (r *Arrangement[T, U]) WithValidate(fv MessageValidator[T]) *Arrangement[T, U]
Add a Validate behavior
type AuthError ¶
type AuthError struct {
Err error
}
AuthError wraps when an error occurs in the auth stage
func NewAuthError ¶
type DefaultMessageValidator ¶
func ForMessage ¶
func ForMessage[T proto.Message](fieldMask ...string) *DefaultMessageValidator[T]
ForMessage creates a new DefaultMessageValidator Accepts paths from a field mask if available
func (*DefaultMessageValidator[T]) AssertEqualTo ¶
func (s *DefaultMessageValidator[T]) AssertEqualTo(path string, value any, equalTo any) *DefaultMessageValidator[T]
AssertEqualTo assert that the value for the supplied fialed path is equal to the supplied target value
func (*DefaultMessageValidator[T]) AssertEqualToWhenInMask ¶
func (s *DefaultMessageValidator[T]) AssertEqualToWhenInMask(path string, value any, equalTo any) *DefaultMessageValidator[T]
AssertEqualToWhenInMask same as AssertEqualTo, but only executes if the supplied path is in the field mask
func (*DefaultMessageValidator[T]) AssertNonZero ¶
func (s *DefaultMessageValidator[T]) AssertNonZero(path string, value any) *DefaultMessageValidator[T]
AssertNonZero assert that the value for the supplied field path is not a zero-value
func (*DefaultMessageValidator[T]) AssertNonZeroWhenInMask ¶
func (s *DefaultMessageValidator[T]) AssertNonZeroWhenInMask(path string, value any) *DefaultMessageValidator[T]
AssertNonZeroWhenInMask same as AssertNonZero, but only executes if the supplied path is in the field mask
func (*DefaultMessageValidator[T]) AssertNotEqualTo ¶
func (s *DefaultMessageValidator[T]) AssertNotEqualTo(path string, value any, notEqualTo any) *DefaultMessageValidator[T]
AssertNotEqualTo assert that the value for the supplied field path is not equal to the supplied target value
func (*DefaultMessageValidator[T]) AssertNotEqualToWhenInMask ¶
func (s *DefaultMessageValidator[T]) AssertNotEqualToWhenInMask(path string, value any, notEqualTo any) *DefaultMessageValidator[T]
AssertNotEqualToWhenInMask same as AssertNotEqualTo, but only executes if the supplied path is in the field mask
func (*DefaultMessageValidator[T]) CustomValidation ¶
func (s *DefaultMessageValidator[T]) CustomValidation(act Validator[T]) *DefaultMessageValidator[T]
CustomValidation is a custom validation function. There can only be one per-validator instance. To add field-level errors to the existing list of field validation errors (in the case regular Assertxxx functions are used), add the errors to the ValidationErrors object and return nil.
In the case that a non-field level error occurs, return the err
func (*DefaultMessageValidator[T]) Exec ¶
func (s *DefaultMessageValidator[T]) Exec(ctx context.Context, message T) *ValidationErrors
Exec executes in the following order: 1. Custom validation function if it exists 2. Field-level assertion functions
type Error ¶
type Error struct {
AuthError *AuthError
ValidationErrs *ValidationErrors
ServeError *ServeErr
}
Error holds errors for each stage of a request
func (*Error) GetAuthError ¶
func (*Error) GetServeError ¶
func (*Error) GetValidationErrors ¶
func (e *Error) GetValidationErrors() *ValidationErrors
func (*Error) SetAuthError ¶
func (*Error) SetServeError ¶
func (*Error) SetValidationErrors ¶
func (*Error) ToGrpcStatus ¶
type FieldError ¶
func FieldErrorFromField ¶
func FieldErrorFromField(f *Field, err error) *FieldError
func (*FieldError) Error ¶
func (f *FieldError) Error() string
func (*FieldError) Unwrap ¶
func (f *FieldError) Unwrap() error
type MessageValidator ¶
type MessageValidator[T proto.Message] interface { Exec(context.Context, T) *ValidationErrors }
MessageValidator responsible for validating supplied fields and returning the results in a ValidationErrors object
type Request ¶
type Request[T proto.Message] struct { Msg T Headers map[string]string Claims map[string]any Extras map[string]any }
TODO(signal426)
type ServeErr ¶
type ServeErr struct {
Err error
}
ServeErr wraps when an error occurs during the handle stage
func NewServeError ¶
type ValidationErrors ¶
type ValidationErrors struct {
FieldErrors []*FieldError
CustomValidationError error
// contains filtered or unexported fields
}
ValidationErrors holds faults for each field evaluated
func NewValidationErrors ¶
func NewValidationErrors() *ValidationErrors
func ValidationErrorsFromErr ¶
func ValidationErrorsFromErr(err error) *ValidationErrors
func (*ValidationErrors) AddFieldErr ¶
func (v *ValidationErrors) AddFieldErr(path string, err error, options ...AddFieldValidationErrOption)
AddFieldErr adds an error for a field from a custom eval function
func (*ValidationErrors) AsMap ¶
func (v *ValidationErrors) AsMap() map[string]*FieldError
func (*ValidationErrors) Error ¶
func (v *ValidationErrors) Error() string
func (ValidationErrors) HasErrors ¶
func (v ValidationErrors) HasErrors() bool
func (ValidationErrors) Paths ¶
func (v ValidationErrors) Paths() []string
func (*ValidationErrors) SetCustomValidationErr ¶
func (v *ValidationErrors) SetCustomValidationErr(err error)
func (*ValidationErrors) Unwrap ¶
func (v *ValidationErrors) Unwrap() error