legit

package module
v0.0.0-...-ebe60f9 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2017 License: BSD-2-Clause Imports: 11 Imported by: 0

README

LEGIT

GoDoc

Legit is an input validation framework for Go. Legit differs from existing frameworks by constructing validation from types and interfaces, preferring custom validators to complex struct tags.

go get -u github.com/jamescun/legit

Included validators:

  • Email
  • UUID
  • UUID3
  • UUID4
  • UUID5
  • Credit Card
  • Lowercase
  • Uppercase
  • No whitespace
  • Printable characters
  • Alpha
  • Alphanumeric
  • Numeric
  • ASCII
  • Positive number
  • Negative number

Example

package main

import (
	"fmt"
	"net/http"

	"github.com/jamescun/legit"
)

type User struct {
	Email legit.Email    `json:"email"`
	Age   legit.Positive `json:"age"`
}

func Handler(w http.ResponseWriter, r *http.Request) {
	var user User
	err := legit.ParseRequestAndValidate(r, &user)
	if err != nil {
		fmt.Fprintln(w, "invalid user:", err)
		return
	}
}

Custom Example

package main

import (
	"fmt"
	"regexp"
	"errors"
	"encoding/json"

	"github.com/jamescun/legit"
)

type Name string

// very simplistic regexp for human name validation
var expName = regexp.MustCompile(`[a-zA-Z\ ]{1,64}`)

// attach Validate() method to our custom name type, satisfying the legit.Object interface,
// defining our custom name validation.
func (n Name) Validate() error {
	if !expName.MatchString(string(n)) {
		return errors.New("invalid name")
	}

	return nil
}

type User struct {
	Email legit.Email `json:"email"`
	Name  Name        `json:"name"`
}

func main() {
	body := []byte(`{"email": "test@example.org", "name": "John Doe"}`)
	
	var user User
	json.Unmarshal(body, &user)

	err := legit.Validate(user)
	if err != nil {
		fmt.Println("invalid user!", err)
	}
}

Documentation

Overview

Legit is an input validation framework for Go. Legit differs from existing frameworks by constructing validation from types and interfaces, preferring custom validators to complex struct tags.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotStruct is returned when a struct validation method is not given
	// a struct.
	ErrNotStruct = errors.New("object is not a struct")

	// ErrNotSlice is returned when a slice validation method is not given
	// a slice.
	ErrNotSlice = errors.New("object is not a slice")

	// ErrStrict is returned when strict validation mode is enabled and a
	// field does not satisfy the Validator interface.
	ErrStrict = errors.New("field is not a validator")
)
View Source
var (
	// ErrEncoding is returned when a matching decoder is not found for
	// the encoding.
	ErrEncoding = errors.New("unknown encoding")
)

Functions

func ParseAndValidate

func ParseAndValidate(r io.Reader, mime string, dst interface{}) error

ParseAndValidate first decodes a reader using the first decoder matching the given mime type, then applies validation to the collected input

func ParseRequestAndValidate

func ParseRequestAndValidate(r *http.Request, dst interface{}) error

ParseRequestAndValidate is the same as ParseAndValidate accepting a HTTP request for the reader and using the "Content-Type" header for the MIME type

func Validate

func Validate(src interface{}) error
Example
package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Email Email
}

func main() {
	body := []byte(`{"email": "not an email"}`)

	var user User
	json.Unmarshal(body, &user)

	err := Validate(user)
	if err != nil {
		fmt.Println("invalid user!", err)
	}
}
Output:

func ValidateSlice

func ValidateSlice(src interface{}) error

func ValidateStruct

func ValidateStruct(src interface{}) error

Types

type ASCII

type ASCII string

ASCII validates any string containing only ASCII characters.

func (*ASCII) Scan

func (a *ASCII) Scan(src interface{}) error

func (ASCII) Validate

func (a ASCII) Validate() error

func (ASCII) Value

func (a ASCII) Value() (driver.Value, error)

type Alpha

type Alpha string

Alpha validates any string containing only letters.

func (*Alpha) Scan

func (a *Alpha) Scan(src interface{}) error

func (Alpha) Validate

func (a Alpha) Validate() error

func (Alpha) Value

func (a Alpha) Value() (driver.Value, error)

type Alphanumeric

type Alphanumeric string

Alphanumeric validates any string containing only letters or numbers.

func (*Alphanumeric) Scan

func (a *Alphanumeric) Scan(src interface{}) error

func (Alphanumeric) Validate

func (a Alphanumeric) Validate() error

func (Alphanumeric) Value

func (a Alphanumeric) Value() (driver.Value, error)

type CreditCard

type CreditCard string

CreditCard validates any string matching a credit card number

func (CreditCard) Validate

func (c CreditCard) Validate() error

type Decoder

type Decoder interface {
	// return true if decoder can understand the given MIME type
	Match(mime string) bool

	// return nil if data from reader was unmarshaled into dst
	Decode(r io.Reader, dst interface{}) error
}

Decoder is used to decode a request body to a type

type Decoders

type Decoders []Decoder

Decoders contains multiple decoders for matching

func (Decoders) Match

func (d Decoders) Match(mime string) Decoder

return the first matching decoder for a MIME type, or nil if no match

type Email

type Email string

Email validates any string matching a RFC 5322 email address

func (Email) Validate

func (e Email) Validate() error

type Errors

type Errors []error

Errors contains one or more validate errors

func (Errors) Error

func (e Errors) Error() string

returns string representation of the first validation error encountered

type Float

type Float string

Float validates any string containing numbers, including an initial minus and a single decimal point.

func (*Float) Scan

func (f *Float) Scan(src interface{}) error

func (Float) Validate

func (f Float) Validate() error

func (Float) Value

func (f Float) Value() (driver.Value, error)

type Form

type Form struct {
	Legit    Legit
	Decoders Decoders
}

Form implements the decoding and validation of user data from readers and HTTP requests

func NewForm

func NewForm() Form

NewForm returns a Form assignment with the default Legit configuration and a JSON decoder

func (Form) ParseAndValidate

func (f Form) ParseAndValidate(r io.Reader, mime string, dst interface{}) error

ParseAndValidate first decodes a reader using the first decoder matching the given mime type, then applies validation to the collected input

func (Form) ParseRequestAndValidate

func (f Form) ParseRequestAndValidate(r *http.Request, dst interface{}) error

ParseRequestAndValidate is the same as ParseAndValidate accepting a HTTP request for the reader and using the "Content-Type" header for the MIME type

type JSON

type JSON struct{}

JSON decoder can decode any JSON body with the MIME type "application/json"

func (JSON) Decode

func (j JSON) Decode(r io.Reader, dst interface{}) error

func (JSON) Match

func (j JSON) Match(mime string) bool

type Legit

type Legit struct {
	// Strict mode requires that all fields in a struct be validatable
	Strict bool
}

Legit implements validation of types implementing the Validator interface, structs and slices.

func New

func New() Legit

New return a Legit assignment without strict validation

func (Legit) Validate

func (l Legit) Validate(src interface{}) error

func (Legit) ValidateSlice

func (l Legit) ValidateSlice(src interface{}) error

func (Legit) ValidateStruct

func (l Legit) ValidateStruct(src interface{}) error

type Lower

type Lower string

Lower validates any string not containing any uppercase characters.

func (*Lower) Scan

func (l *Lower) Scan(src interface{}) error

func (Lower) Validate

func (l Lower) Validate() error

func (Lower) Value

func (l Lower) Value() (driver.Value, error)

type Negative

type Negative int

Negative validates any integer that contains a value below zero.

func (Negative) Validate

func (n Negative) Validate() error

type NoSpace

type NoSpace string

NoSpace validates any string not containing any whitespace characters.

func (*NoSpace) Scan

func (ns *NoSpace) Scan(src interface{}) error

func (NoSpace) Validate

func (ns NoSpace) Validate() error

func (NoSpace) Value

func (ns NoSpace) Value() (driver.Value, error)

type Number

type Number string

Number validates any string containing only numeric characters.

func (*Number) Scan

func (n *Number) Scan(src interface{}) error

func (Number) Validate

func (n Number) Validate() error

func (Number) Value

func (n Number) Value() (driver.Value, error)

type Positive

type Positive int

Positive validates any integer that contains a value above (and including) zero.

func (Positive) Validate

func (p Positive) Validate() error

type Printable

type Printable string

Printable validates any string not containing any non-printing characters.

func (*Printable) Scan

func (p *Printable) Scan(src interface{}) error

func (Printable) Validate

func (p Printable) Validate() error

func (Printable) Value

func (p Printable) Value() (driver.Value, error)

type Required

type Required string

Required validates any string that is not empty

func (*Required) Scan

func (r *Required) Scan(src interface{}) error

func (Required) Validate

func (r Required) Validate() error

func (Required) Value

func (r Required) Value() (driver.Value, error)

type SliceError

type SliceError struct {
	Index   int   `json:"index"`
	Message error `json:"message"`
}

SliceError contains the index and message of a failed validation

func (SliceError) Error

func (se SliceError) Error() string

returns the string representation of the index and failed validation

type StructError

type StructError struct {
	Field   string `json:"field"`
	Message error  `json:"message"`
}

StructError contains the field name and message of a failed validation

func (StructError) Error

func (se StructError) Error() string

returns the string representation of the field and failed validation

type UUID

type UUID string

UUID validates any string matching a UUID of any version

func (UUID) Validate

func (u UUID) Validate() error

type UUID3

type UUID3 string

UUID3 validates any string matching a version 3 UUID

func (UUID3) Validate

func (u UUID3) Validate() error

type UUID4

type UUID4 string

UUID4 validates any string matching a version 4 UUID

func (UUID4) Validate

func (u UUID4) Validate() error

type UUID5

type UUID5 string

UUID5 validates any string matching a version 5 UUID

func (UUID5) Validate

func (u UUID5) Validate() error

type Upper

type Upper string

Upper validates any string not containing any lowercase characters.

func (*Upper) Scan

func (u *Upper) Scan(src interface{}) error

func (Upper) Validate

func (u Upper) Validate() error

func (Upper) Value

func (u Upper) Value() (driver.Value, error)

type Validator

type Validator interface {
	// returns nil if object is valid
	Validate() error
}

Validator is a type that can be validated

type XML

type XML struct{}

func (XML) Decode

func (x XML) Decode(r io.Reader, dst interface{}) error

func (XML) Match

func (x XML) Match(mime string) bool

Jump to

Keyboard shortcuts

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