validate

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2016 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validate provides a simple interface for validating JSON user input

Example:

	type Item struct {
		Name        string `json:"name"`
		Description string `json:"description"`
	}

    func (i *Item) Validate(ctx context.Context) error {
		if RuneCountInString(i.Name) == 0 {
			return fmt.Errorf("the field: name must be provided and not empty")
		}
		return nil
	}

	func CreateItem(w http.ResponseWriter, r *http.Request) {
		item := &Item{}
		if err := validate.JSONRequest(ctx, r, item); err != nil {
			w.WriteHeader(400)
			return
		}
	}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONRequest

func JSONRequest(ctx context.Context, r *http.Request, v Validatable) error

JSONRequest takes an http request, decodes the json and validates the input against a Validatable output the Validatable variable will get populated with the contents of the body provided by *http.Request

The base set of error types returned from this method are:

	validate.IOError
 *json.SyntaxError
	*json.UnmarshalFieldError
	*json.UnmarshalTypeError

Usage:

type Item struct {
	Name string `json:"name"`
	Description string `json:"description"`
}

func (i *Item) Validate(ctx context.Context) error {
	if RuneCountInString(i.Name) == 0 {
		return fmt.Errorf("the field: name must be provided and not empty")
	}
	return nil
}

func CreateItem(w http.ResponseWriter, r *http.Request) {
	item := &Item{}
	if err := validate.JSONRequest(ctx, r, item); err != nil {
		w.WriteHeader(400)
		return
	}
}

func Reader

func Reader(ctx context.Context, r io.Reader, unmarshaller func(data []byte, v interface{}) error, v Validatable) error

Reader takes a generic io.Reader, an unmarshaller and validates the input against a Validatable item the Validatable variable will get populated with the contents of the body provided by *http.Request

Usage:

	type ApiInput struct {
		Name string `json:"name"`
		Description string `json:"description"`
	}

 func (i *ApiInput) Validate(ctx context.Context) error {
		if utf8.RuneCountInString(i.Name) == 0 {
			return fmt.Errorf("the field: name must be provided and not empty")
		}
		return nil
	}

	func main() {
		input := &ApiInput{}
		if err := validate.Reader(ctx, reader, json.Unmarshal, input); err != nil {
			log.Panic(err)
		}
	}

func XMLRequest

func XMLRequest(ctx context.Context, r *http.Request, v Validatable) error

XMLRequest takes an http request docodes the xml into an item and validates the provided item

The base set of error types returned from this method are:

	validate.IOError
 *xml.SyntaxError
	*xml.TagPathError
	*xml.UnmarshalError

Types

type IOError

type IOError struct {
	// contains filtered or unexported fields
}

IOError for when we fail to read the stream

type Validatable

type Validatable interface {
	Validate(ctx context.Context) error
}

Validatable items can self validate

Jump to

Keyboard shortcuts

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