validations

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

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

Go to latest
Published: Nov 9, 2014 License: BSD-2-Clause Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

validations

Go validations

Install

go get github.com/ernestoalejo/validations

API reference

http://godoc.org/github.com/ernestoalejo/validations

Examples

Validate strings, emails and integers
package main

import (
  vals "github.com/ernestoalejo/validations"
)

type requestData struct {
  Foo string
  FooEmail string
  Bar int
}

func (req *requestData) Validate() error {
  return vals.Run([]vals.Func{
    vals.Str(req.Foo, []vals.StrFunc{
      vals.Required(),
    }),

    vals.Str(req.FooEmail, []vals.StrFunc{
      vals.Required(),
      vals.Email(),
    }),

    vals.Int(req.Bar, []vals.IntFunc{
      vals.Range(10, 20),
    }),
  })
}
Validate slices
package main

import (
  vals "github.com/ernestoalejo/validations"
)

type requestData struct {
  Foo []*sliceData
}

type sliceData struct {
  Foo string
}

func (req *requestData) Validate() error {
  return vals.Run([]vals.Func{
    vals.Slice(req.Rooms, []vals.SliceFunc{
      vals.SliceMinLen(1),

      vals.Each(func(item *sliceData) error {
        return vals.Run([]vals.Func{
          vals.Str(item.Foo, []vals.StrFunc{
            vals.Required(),
          }),
        })
      }),
    }),
  })
}
Validate times
package main

import (
  "time"

  vals "github.com/ernestoalejo/validations"
)

type requestData struct {
  EntryRaw, DepartureRaw string

  entry, departure time.Time
}

func (req *requestData) Validate() error {
  return vals.Run([]vals.Func{
    vals.Time(&req.entry, []vals.TimeFunc{
      vals.ParseTime(req.EntryRaw, "2006-01-02"),
      vals.NotBefore(time.Now()),
    }),

    vals.Time(&req.departure, []vals.TimeFunc{
      vals.ParseTime(req.DepartureRaw, "2006-01-02"),
      vals.NotBefore(req.entry),
    }),
  })
}

Documentation

Overview

Provides easy declarative validation of structs of data

These structs will probably be obtained from a XML / JSON deserialization and should be validated before using them in the web application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(validations []Func) error

Types

type FloatFunc

type FloatFunc func(s float64) error

func FloatPositive

func FloatPositive() FloatFunc

func FloatPositiveZero

func FloatPositiveZero() FloatFunc

func FloatRange

func FloatRange(minvalue, maxvalue float64) FloatFunc

type Func

type Func func() error

func Float

func Float(fieldName string, field float64, validations []FloatFunc) Func

func If

func If(condition bool, validations []Func) Func

If only runs the validations if the condition it's true.

func Int

func Int(fieldName string, field int, validations []IntFunc) Func

func Int64

func Int64(fieldName string, field int64, validations []Int64Func) Func

func Slice

func Slice(fieldName string, field interface{}, validations []SliceFunc) Func

func Str

func Str(fieldName, field string, validations []StrFunc) Func

func Struct

func Struct(fieldName string, field interface{}, validations []StructFunc) Func

func Time

func Time(fieldName string, field *time.Time, validations []TimeFunc) Func

type Int64Func

type Int64Func func(s int64) error

func MaxValue64

func MaxValue64(max int64) Int64Func

func MinValue64

func MinValue64(min int64) Int64Func

func Positive64

func Positive64() Int64Func

func PositiveZero64

func PositiveZero64() Int64Func

func Range64

func Range64(minvalue, maxvalue int64) Int64Func

type IntFunc

type IntFunc func(s int) error

func MaxValue

func MaxValue(max int) IntFunc

func Positive

func Positive() IntFunc

func PositiveZero

func PositiveZero() IntFunc

func Range

func Range(minvalue, maxvalue int) IntFunc

type SliceFunc

type SliceFunc func(s interface{}) error

func Each

func Each(fn interface{}) SliceFunc

func SliceMinLen

func SliceMinLen(length int) SliceFunc

type StrFunc

type StrFunc func(s string) error

func Email

func Email() StrFunc

func In

func In(allowed ...string) StrFunc

func Length

func Length(minlength, maxlength int) StrFunc

func MaxLength

func MaxLength(length int) StrFunc

func MinLength

func MinLength(length int) StrFunc

func RegExp

func RegExp(expression string) StrFunc

func Required

func Required() StrFunc

type StructFunc

type StructFunc func(s interface{}) error

func NotNil

func NotNil() StructFunc

type TimeFunc

type TimeFunc func(s time.Time) (time.Time, error)

func After

func After(otherTime time.Time) TimeFunc

func NotAfter

func NotAfter(otherTime time.Time) TimeFunc

func NotBefore

func NotBefore(otherTime time.Time) TimeFunc

func ParseTime

func ParseTime(source, format string) TimeFunc

func ParseTimeUnix

func ParseTimeUnix(seconds int64) TimeFunc

Jump to

Keyboard shortcuts

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