validationerrors

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2018 License: Apache-2.0 Imports: 8 Imported by: 4

README

Validation Errors

import "github.com/infobloxopen/atlas-app-toolkit/errors/mappers/validationerrors"

validationerrors is a request contents validator server-side middleware for gRPC.

Request Validator Middleware

This middleware checks for the existence of a Validate method on each of the messages of a gRPC request. In case of a validation failure, an InvalidArgument gRPC status is returned, along with the error that caused the validation failure.

It is intended to be used with plugins like https://github.com/lyft/protoc-gen-validate, Go protocol buffers codegen plugins that create the Validate methods (including nested messages) based on declarative options in the .proto files themselves.

Usage

func UnaryServerInterceptor
func UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that validates incoming messages and returns a ValidationError.

Invalid messages will be rejected with InvalidArgument and the error before reaching any userspace handlers.

func DefaultMapping
func DefaultMapping() errors.MapFunc

DefaultMapping returns a mapper that parses through the lyft protoc-gen-validate errors and only returns a user friendly error.

Example Usage:

  1. Add validationerrors and errors interceptors to your application:

    errors.UnaryServerInterceptor(ErrorMappings...),
    validationerrors.UnaryServerInterceptor(),
    
  2. Create an ErrorMapping variable with all your mappings.

  3. Add DefaultMapping as part of your ErrorMapping variable

    var ErrorMappings = []errors.MapFunc{
       // Adding Default Validations Mapping
       validationerrors.DefaultMapping(), 
    
    }
    

    Example return after DefaultMapping on a invalid email:

    {
        "error": {
            "status": 400,
            "code": "INVALID_ARGUMENT",
            "message": "Invalid primary_email: value must be a valid email address"
        },
        "fields": {
            "primary_email": [
                "value must be a valid email address"
            ]
        }
    }
    
  4. You can also add custom validation mappings:

    var ErrorMappings = []errors.MapFunc{
        // Adding custom Validation Mapping based on the known field and reason from lyft
       errors.NewMapping(
    		errors.CondAnd(
    			validationerrors.CondValidation(),
                validationerrors.CondFieldEq("primary_email"),
    			validationerrors.CondReasonEq("value must be a valid email address"),
    		),
    		errors.MapFunc(func(ctx context.Context, err error) (error, bool) {
    			vErr, _ := err.(validationerrors.ValidationError)
    			return errors.NewContainer(codes.InvalidArgument, "Custom error message for field: %v reason: %v", vErr.Field, vErr.Reason), true
            }),
       ),
    }
    
    

For actual example usage look at: https://github.com/infobloxopen/atlas-contacts-app

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CondFieldEq

func CondFieldEq(theField string) errors.MapCond

CondFieldEq function returns a condition function that checks if the field matches the validation error field.

func CondReasonEq

func CondReasonEq(theReason string) errors.MapCond

CondReasonEq function returns a condition function that checks if the reason matches the validation error reason.

func CondValidation

func CondValidation() errors.MapCond

CondValidation function returns a condition function that matches standard Validation error and ensures that the error contains a field and a reason.

func DefaultMapping

func DefaultMapping() errors.MapFunc

DefaultMapping the default behavior for validation error mapping.

func GetValidationError

func GetValidationError(err error) error

GetValidationError function returns a validation error from an error.

func ToMapFunc

func ToMapFunc(f func(context.Context, ValidationError) (error, bool)) errors.MapFunc

ToMapFunc function converts mapping function for *validationerrors.Error to a conventional MapFunc from atlas-app-toolkit/errors package.

func UnaryServerInterceptor

func UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that validates incoming messages.

Invalid messages will be rejected with `InvalidArgument` before reaching any userspace handlers.

Types

type ValidationError

type ValidationError struct {
	Field         string
	Reason        string
	Key           bool
	Cause         error
	ErrorTypeName string // Error Name (“ABValidationError”)
}

ValidationError represents the validation error that contains which field failed and the reasoning behind it.

func (ValidationError) Error

func (e ValidationError) Error() string

Error satisfies the builtin error interface.

Jump to

Keyboard shortcuts

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