validator

package
v0.0.0-...-58d3f0e Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2017 License: MIT Imports: 8 Imported by: 0

README

Requests Validator

Automatically validates your requests. Helps to incapsulate requests validation logic.

Install

go get github.com/mlanin/iris-middlewares/requests-validator

About

By Default it highly utilizes go-apierr package, so for the comfort usage better be used in link with the API Errors Handler middleware from this repo.

Validation logic and errors handling can be overridden.

  • Populates your requests from the preferred source.
  • Validates requests data by ozzo-validation(v3).
  • Saves valid request to the context to use its data further.
JSON API validation

On invalid request will panic with apierr.ValidationFailed that can be converted in 422 response with JSON like:

{
	"error": {
		"id": "validation_failed",
		"message": "Validation failed.",
	},
	"meta": {
		"errors": [
      {
				"field": "body",
				"message": "Cannot be blank",
			}
    ]
	}
}
Common web forms validation

On invalid requests old input and validation errors will be saved to session flash and redirected back to the referer url or previous visited page or /.

Usage

package main

import (
  "github.com/kataras/iris"
  "github.com/mlanin/go-apierr"
  handler "github.com/mlanin/iris-middlewares/apierr-handler"
  validator "github.com/mlanin/iris-middlewares/requests-validator"
)

// PostNewsJSON request:
// - if name ends with JSON, request will be populated from JSON;
// - if name ends with XML, request will be populated with XML;
// - if name ends with Form, request will be populated from form data;
// - if name ends with Query, request will be populated from URL query
type PostNewsJSON struct {
	// By default fields in request will be searched by their name, but you can override it by tag:
	// - json
	// - xml
	// - form
	// - query
	Text string `json:"text"`
}

// Validate request.
func (r *PostNews) Validate() error {
	// Register validation rules.
	return validation.ValidateStruct(r,
		validation.Field(&r.Text, validation.Required),
	)
}

func main() {

	// Make RequestsValidator middleware.
	rv := validator.New(validator.Config{})

  // Make APIErrors handler middleware.
  errorsHandler := handler.New(handler.Config{
		EnvGetter: func() string {
			return "production"
		},
		DebugGetter: func() bool {
			return false
		},
	})

	// Import middleware.
  iris.Use(errorsHandler)
  iris.Use(rv)

  // Place rv.ValidateRequest with you request struct right before main handler.
	iris.Post("/news", rv.ValidateRequest(&PostNewsJSON{}), func(ctx *iris.Context) {
    // If request is valid, it will be stored by request full name key in the IRIS context.
    request := ctx.Get("main.PostNewsJSON").(*PostNewsJSON)

		ctx.Text(200, request.Text)
	})

}

Override handler

You can override error handing logic by passing your own handlers to the validator's constructor.

func main() {

	// Make RequestsValidator middleware and override default JSON API requests errors.
	rv := validator.New(validator.Config{
		APIHandler: func(context *validator.Context, ctx *iris.Context) {
			panic(apierr.BadRequest)
		},
		WebHandler: func(context *validator.Context, ctx *iris.Context) {
			ctx.Text(200, "Error")
		},
		BadRequestHandler: func(context *validator.Context, ctx *iris.Context) {
			panic(apierr.BadRequest)
		},
	})

}

Working example

Full working example can be found in API Boilerplate

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UcFirst

func UcFirst(str string) string

UcFirst convert first letter into upper.

Types

type Config

type Config struct {
	// Handle error for common http request.
	WebHandler func(context *Context, ctx *iris.Context)
	// Handle error for API calls.
	APIHandler func(context *Context, ctx *iris.Context)
	// Handle unmarshal request error.
	BadRequestHandler func(context *Context, ctx *iris.Context)
}

Config for the middleware.

type Context

type Context struct {
	Request HTTPRequest
	Name    string
	Type    string
	Errors  error
}

Context to store request data.

func NewContext

func NewContext(request HTTPRequest) *Context

NewContext constructor.

type HTTPRequest

type HTTPRequest interface {
	// Validate request.
	Validate() error
}

HTTPRequest interface.

type RequestsValidator

type RequestsValidator struct {
	Config
}

RequestsValidator for http requests.

func New

func New(config Config) *RequestsValidator

New middleware constructor.

func (*RequestsValidator) Serve

func (rv *RequestsValidator) Serve(ctx *iris.Context)

Serve the middleware.

func (*RequestsValidator) ValidateRequest

func (rv *RequestsValidator) ValidateRequest(request HTTPRequest) iris.HandlerFunc

ValidateRequest helper function to make validator.

Jump to

Keyboard shortcuts

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