validation

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

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 13 Imported by: 0

README

validation from revel

Documentation

Index

Constants

View Source
const (
	None               = 0
	IPAny              = 1
	IPv4               = 32 // IPv4 (32 chars)
	IPv6               = 39 // IPv6(39 chars)
	IPv4MappedIPv6     = 45 // IP4-mapped IPv6 (45 chars) , Ex) ::FFFF:129.144.52.38
	IPv4CIDR           = IPv4 + 3
	IPv6CIDR           = IPv6 + 3
	IPv4MappedIPv6CIDR = IPv4MappedIPv6 + 3
)
View Source
const (
	NORMAL = 0
	STRICT = 4
)

NORMAL BenchmarkRegex-8 2000000000 0.24 ns/op STRICT BenchmarkLoop-8 2000000000 0.01 ns/op

View Source
const (
	ONLY_FILENAME       = 0
	ALLOW_RELATIVE_PATH = 1
)

Variables

View Source
var DefaultValidationKeys map[string]map[int]string

DefaultValidationKeys register default validation keys for all calls to Controller.Validation.Func(). Map from (package).func => (line => name of first arg to Validation func) E.g. "myapp/controllers.helper" or "myapp/controllers.(*Application).Action" This is set on initialization in the generated main.go file.

Functions

This section is empty.

Types

type Domain

type Domain struct {
	Regexp *regexp.Regexp
}

Requires a Domain string to be exactly

func ValidDomain

func ValidDomain() Domain

func (Domain) DefaultMessage

func (d Domain) DefaultMessage() string

func (Domain) IsSatisfied

func (d Domain) IsSatisfied(obj interface{}) bool

type Email

type Email struct {
	Match
}

func ValidEmail

func ValidEmail() Email

func (Email) DefaultMessage

func (e Email) DefaultMessage() string

type FilePath

type FilePath struct {
	Mode int
}

Requires an string to be sanitary file path

func ValidFilePath

func ValidFilePath(m int) FilePath

func (FilePath) DefaultMessage

func (f FilePath) DefaultMessage() string

func (FilePath) IsSatisfied

func (f FilePath) IsSatisfied(obj interface{}) bool

type IPAddr

type IPAddr struct {
	Vaildtypes []int
}

Requires a string(IP Address) to be within IP Pattern type inclusive.

func ValidIPAddr

func ValidIPAddr(cktypes ...int) IPAddr

Requires an IP Address string to be exactly a given validation type (IPv4, IPv6, IPv4MappedIPv6, IPv4CIDR, IPv6CIDR, IPv4MappedIPv6CIDR OR IPAny)

func (IPAddr) DefaultMessage

func (i IPAddr) DefaultMessage() string

func (IPAddr) IsSatisfied

func (i IPAddr) IsSatisfied(obj interface{}) bool

type Length

type Length struct {
	N int
}

Length requires an array or string to be exactly a given length.

func ValidLength

func ValidLength(n int) Length

func (Length) DefaultMessage

func (s Length) DefaultMessage() string

func (Length) IsSatisfied

func (s Length) IsSatisfied(obj interface{}) bool

type MacAddr

type MacAddr struct{}

Requires a MAC Address string to be exactly

func ValidMacAddr

func ValidMacAddr() MacAddr

func (MacAddr) DefaultMessage

func (m MacAddr) DefaultMessage() string

func (MacAddr) IsSatisfied

func (m MacAddr) IsSatisfied(obj interface{}) bool

type Match

type Match struct {
	Regexp *regexp.Regexp
}

Match requires a string to match a given regex.

func ValidMatch

func ValidMatch(regex *regexp.Regexp) Match

func (Match) DefaultMessage

func (m Match) DefaultMessage() string

func (Match) IsSatisfied

func (m Match) IsSatisfied(obj interface{}) bool

type Max

type Max struct {
	Max float64
}

func ValidMax

func ValidMax(max int) Max

func ValidMaxFloat

func ValidMaxFloat(max float64) Max

func (Max) DefaultMessage

func (m Max) DefaultMessage() string

func (Max) IsSatisfied

func (m Max) IsSatisfied(obj interface{}) bool

type MaxSize

type MaxSize struct {
	Max int
}

MaxSize requires an array or string to be at most a given length.

func ValidMaxSize

func ValidMaxSize(max int) MaxSize

func (MaxSize) DefaultMessage

func (m MaxSize) DefaultMessage() string

func (MaxSize) IsSatisfied

func (m MaxSize) IsSatisfied(obj interface{}) bool

type Min

type Min struct {
	Min float64
}

func ValidMin

func ValidMin(min int) Min

func ValidMinFloat

func ValidMinFloat(min float64) Min

func (Min) DefaultMessage

func (m Min) DefaultMessage() string

func (Min) IsSatisfied

func (m Min) IsSatisfied(obj interface{}) bool

type MinSize

type MinSize struct {
	Min int
}

MinSize requires an array or string to be at least a given length.

func ValidMinSize

func ValidMinSize(min int) MinSize

func (MinSize) DefaultMessage

func (m MinSize) DefaultMessage() string

func (MinSize) IsSatisfied

func (m MinSize) IsSatisfied(obj interface{}) bool

type PureText

type PureText struct {
	Mode int
}

Requires a string to be without invisible characters

func ValidPureText

func ValidPureText(m int) PureText

func (PureText) DefaultMessage

func (p PureText) DefaultMessage() string

func (PureText) IsSatisfied

func (p PureText) IsSatisfied(obj interface{}) bool

type Range

type Range struct {
	Min
	Max
}

Range requires an integer to be within Min, Max inclusive.

func ValidRange

func ValidRange(min, max int) Range

func ValidRangeFloat

func ValidRangeFloat(min, max float64) Range

func (Range) DefaultMessage

func (r Range) DefaultMessage() string

func (Range) IsSatisfied

func (r Range) IsSatisfied(obj interface{}) bool

type Required

type Required struct{}

func ValidRequired

func ValidRequired() Required

func (Required) DefaultMessage

func (r Required) DefaultMessage() string

func (Required) IsSatisfied

func (r Required) IsSatisfied(obj interface{}) bool

type URL

type URL struct {
	Domain
}

func ValidURL

func ValidURL() URL

func (URL) DefaultMessage

func (u URL) DefaultMessage() string

func (URL) IsSatisfied

func (u URL) IsSatisfied(obj interface{}) bool

type Validation

type Validation struct {
	Errors     []*ValidationError
	Request    *http.Request
	Translator func(locale, message string, args ...interface{}) string
	// contains filtered or unexported fields
}

Validation context manages data validation and error messages.

func (*Validation) Check

func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult

Check applies a group of validators to a field, in order, and return the ValidationResult from the first one that fails, or the last one that succeeds.

func (*Validation) Clear

func (v *Validation) Clear()

Clear *all* ValidationErrors

func (*Validation) Domain

func (v *Validation) Domain(str string) *ValidationResult

func (*Validation) Email

func (v *Validation) Email(str string) *ValidationResult

func (*Validation) Error

func (v *Validation) Error(message string, args ...interface{}) *ValidationResult

Error adds an error to the validation context.

func (*Validation) ErrorKey

func (v *Validation) ErrorKey(message string, args ...interface{}) *ValidationResult

Error adds an error to the validation context.

func (*Validation) ErrorMap

func (v *Validation) ErrorMap() map[string]*ValidationError

ErrorMap returns the errors mapped by key. If there are multiple validation errors associated with a single key, the first one "wins". (Typically the first validation will be the more basic).

func (*Validation) FilePath

func (v *Validation) FilePath(str string, m int) *ValidationResult

func (*Validation) HasErrors

func (v *Validation) HasErrors() bool

HasErrors returns true if there are any (ie > 0) errors. False otherwise.

func (*Validation) IPAddr

func (v *Validation) IPAddr(str string, cktype ...int) *ValidationResult

func (*Validation) Keep

func (v *Validation) Keep()

Keep tells revel to set a flash cookie on the client to make the validation errors available for the next request. This is helpful when redirecting the client after the validation failed. It is good practice to always redirect upon a HTTP POST request. Thus one should use this method when HTTP POST validation failed and redirect the user back to the form.

func (*Validation) Length

func (v *Validation) Length(obj interface{}, n int) *ValidationResult

func (*Validation) MacAddr

func (v *Validation) MacAddr(str string) *ValidationResult

func (*Validation) Match

func (v *Validation) Match(str string, regex *regexp.Regexp) *ValidationResult

func (*Validation) Max

func (v *Validation) Max(n int, max int) *ValidationResult

func (*Validation) MaxFloat

func (v *Validation) MaxFloat(n float64, max float64) *ValidationResult

func (*Validation) MaxSize

func (v *Validation) MaxSize(obj interface{}, max int) *ValidationResult

func (*Validation) Min

func (v *Validation) Min(n int, min int) *ValidationResult

func (*Validation) MinFloat

func (v *Validation) MinFloat(n float64, min float64) *ValidationResult

func (*Validation) MinSize

func (v *Validation) MinSize(obj interface{}, min int) *ValidationResult

func (*Validation) PureText

func (v *Validation) PureText(str string, m int) *ValidationResult

func (*Validation) Range

func (v *Validation) Range(n, min, max int) *ValidationResult

func (*Validation) RangeFloat

func (v *Validation) RangeFloat(n, min, max float64) *ValidationResult

func (*Validation) Required

func (v *Validation) Required(obj interface{}) *ValidationResult

Required tests that the argument is non-nil and non-empty (if string or list)

func (*Validation) URL

func (v *Validation) URL(str string) *ValidationResult

func (*Validation) ValidationResult

func (v *Validation) ValidationResult(ok bool) *ValidationResult

Error adds an error to the validation context.

type ValidationError

type ValidationError struct {
	Message, Key string
}

ValidationError simple struct to store the Message & Key of a validation error

func (*ValidationError) String

func (e *ValidationError) String() string

String returns the Message field of the ValidationError struct.

type ValidationResult

type ValidationResult struct {
	Error      *ValidationError
	Ok         bool
	Locale     string
	Translator func(locale, message string, args ...interface{}) string
}

ValidationResult is returned from every validation method. It provides an indication of success, and a pointer to the Error (if any).

func (*ValidationResult) Key

Key sets the ValidationResult's Error "key" and returns itself for chaining

func (*ValidationResult) Message

func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult

Message sets the error message for a ValidationResult. Returns itself to allow chaining. Allows Sprintf() type calling with multiple parameters

func (*ValidationResult) MessageKey

func (r *ValidationResult) MessageKey(message string, args ...interface{}) *ValidationResult

Allow a message key to be passed into the validation result. The Validation has already setup the translator to translate the message key

type Validator

type Validator interface {
	IsSatisfied(interface{}) bool
	DefaultMessage() string
}

Jump to

Keyboard shortcuts

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