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 ¶
- Variables
- func ParseAndValidate(r io.Reader, mime string, dst interface{}) error
- func ParseRequestAndValidate(r *http.Request, dst interface{}) error
- func Validate(src interface{}) error
- func ValidateSlice(src interface{}) error
- func ValidateStruct(src interface{}) error
- type ASCII
- type Alpha
- type Alphanumeric
- type CreditCard
- type Decoder
- type Decoders
- type Email
- type Errors
- type Float
- type Form
- type JSON
- type Legit
- type Lower
- type Negative
- type NoSpace
- type Number
- type Positive
- type Printable
- type Required
- type SliceError
- type StructError
- type UUID
- type UUID3
- type UUID4
- type UUID5
- type Upper
- type Validator
- type XML
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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") )
var ( // ErrEncoding is returned when a matching decoder is not found for // the encoding. ErrEncoding = errors.New("unknown encoding") )
Functions ¶
func ParseAndValidate ¶
ParseAndValidate first decodes a reader using the first decoder matching the given mime type, then applies validation to the collected input
func ParseRequestAndValidate ¶
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 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
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 Float ¶
type Float string
Float validates any string containing numbers, including an initial minus and a single decimal point.
type Form ¶
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 ¶
ParseAndValidate first decodes a reader using the first decoder matching the given mime type, then applies validation to the collected input
type JSON ¶
type JSON struct{}
JSON decoder can decode any JSON body with the MIME type "application/json"
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 (Legit) ValidateSlice ¶
func (Legit) ValidateStruct ¶
type NoSpace ¶
type NoSpace string
NoSpace validates any string not containing any whitespace characters.
type Positive ¶
type Positive int
Positive validates any integer that contains a value above (and including) zero.
type Printable ¶
type Printable string
Printable validates any string not containing any non-printing characters.
type SliceError ¶
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 ¶
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