mirror of
https://github.com/astaxie/beego.git
synced 2025-07-10 02:50:19 +00:00
add validation funcs
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
package revel
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -10,9 +10,12 @@ import (
|
||||
type Validator interface {
|
||||
IsSatisfied(interface{}) bool
|
||||
DefaultMessage() string
|
||||
GetKey() string
|
||||
}
|
||||
|
||||
type Required struct{}
|
||||
type Required struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (r Required) IsSatisfied(obj interface{}) bool {
|
||||
if obj == nil {
|
||||
@ -42,11 +45,13 @@ func (r Required) DefaultMessage() string {
|
||||
return "Required"
|
||||
}
|
||||
|
||||
// TODO
|
||||
// type Unique struct {}
|
||||
func (r Required) GetKey() string {
|
||||
return r.Key
|
||||
}
|
||||
|
||||
type Min struct {
|
||||
Min int
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m Min) IsSatisfied(obj interface{}) bool {
|
||||
@ -61,8 +66,13 @@ func (m Min) DefaultMessage() string {
|
||||
return fmt.Sprintln("Minimum is", m.Min)
|
||||
}
|
||||
|
||||
func (m Min) GetKey() string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
type Max struct {
|
||||
Max int
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m Max) IsSatisfied(obj interface{}) bool {
|
||||
@ -77,10 +87,15 @@ func (m Max) DefaultMessage() string {
|
||||
return fmt.Sprintln("Maximum is", m.Max)
|
||||
}
|
||||
|
||||
func (m Max) GetKey() string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
// Requires an integer to be within Min, Max inclusive.
|
||||
type Range struct {
|
||||
Min
|
||||
Max
|
||||
Key string
|
||||
}
|
||||
|
||||
func (r Range) IsSatisfied(obj interface{}) bool {
|
||||
@ -91,9 +106,14 @@ func (r Range) DefaultMessage() string {
|
||||
return fmt.Sprintln("Range is", r.Min.Min, "to", r.Max.Max)
|
||||
}
|
||||
|
||||
func (r Range) GetKey() string {
|
||||
return r.Key
|
||||
}
|
||||
|
||||
// Requires an array or string to be at least a given length.
|
||||
type MinSize struct {
|
||||
Min int
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m MinSize) IsSatisfied(obj interface{}) bool {
|
||||
@ -111,9 +131,14 @@ func (m MinSize) DefaultMessage() string {
|
||||
return fmt.Sprintln("Minimum size is", m.Min)
|
||||
}
|
||||
|
||||
func (m MinSize) GetKey() string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
// Requires an array or string to be at most a given length.
|
||||
type MaxSize struct {
|
||||
Max int
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m MaxSize) IsSatisfied(obj interface{}) bool {
|
||||
@ -131,27 +156,38 @@ func (m MaxSize) DefaultMessage() string {
|
||||
return fmt.Sprintln("Maximum size is", m.Max)
|
||||
}
|
||||
|
||||
// Requires an array or string to be exactly a given length.
|
||||
type Length struct {
|
||||
N int
|
||||
func (m MaxSize) GetKey() string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
func (s Length) IsSatisfied(obj interface{}) bool {
|
||||
// Requires an array or string to be exactly a given length.
|
||||
type Length struct {
|
||||
N int
|
||||
Key string
|
||||
}
|
||||
|
||||
func (l Length) IsSatisfied(obj interface{}) bool {
|
||||
if str, ok := obj.(string); ok {
|
||||
return len(str) == s.N
|
||||
return len(str) == l.N
|
||||
}
|
||||
v := reflect.ValueOf(obj)
|
||||
if v.Kind() == reflect.Slice {
|
||||
return v.Len() == s.N
|
||||
return v.Len() == l.N
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Length) DefaultMessage() string {
|
||||
return fmt.Sprintln("Required length is", s.N)
|
||||
func (l Length) DefaultMessage() string {
|
||||
return fmt.Sprintln("Required length is", l.N)
|
||||
}
|
||||
|
||||
type Alpha struct{}
|
||||
func (l Length) GetKey() string {
|
||||
return l.Key
|
||||
}
|
||||
|
||||
type Alpha struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (a Alpha) IsSatisfied(obj interface{}) bool {
|
||||
if str, ok := obj.(string); ok {
|
||||
@ -168,7 +204,13 @@ func (a Alpha) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be valid alpha characters")
|
||||
}
|
||||
|
||||
type Numeric struct{}
|
||||
func (a Alpha) GetKey() string {
|
||||
return a.Key
|
||||
}
|
||||
|
||||
type Numeric struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (n Numeric) IsSatisfied(obj interface{}) bool {
|
||||
if str, ok := obj.(string); ok {
|
||||
@ -185,7 +227,13 @@ func (n Numeric) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be valid numeric characters")
|
||||
}
|
||||
|
||||
type AlphaNumeric struct{}
|
||||
func (n Numeric) GetKey() string {
|
||||
return n.Key
|
||||
}
|
||||
|
||||
type AlphaNumeric struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (a AlphaNumeric) IsSatisfied(obj interface{}) bool {
|
||||
if str, ok := obj.(string); ok {
|
||||
@ -202,9 +250,14 @@ func (a AlphaNumeric) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be valid alpha or numeric characters")
|
||||
}
|
||||
|
||||
func (a AlphaNumeric) GetKey() string {
|
||||
return a.Key
|
||||
}
|
||||
|
||||
// Requires a string to match a given regex.
|
||||
type Match struct {
|
||||
Regexp *regexp.Regexp
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m Match) IsSatisfied(obj interface{}) bool {
|
||||
@ -216,55 +269,84 @@ func (m Match) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must match", m.Regexp)
|
||||
}
|
||||
|
||||
func (m Match) GetKey() string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
// Requires a string to not match a given regex.
|
||||
type NoMatch struct {
|
||||
Match
|
||||
Key string
|
||||
}
|
||||
|
||||
func (m NoMatch) IsSatisfied(obj interface{}) bool {
|
||||
return !m.Match.IsSatisfied(obj)
|
||||
func (n NoMatch) IsSatisfied(obj interface{}) bool {
|
||||
return !n.Match.IsSatisfied(obj)
|
||||
}
|
||||
|
||||
func (m NoMatch) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must no match", m.Regexp)
|
||||
func (n NoMatch) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must no match", n.Regexp)
|
||||
}
|
||||
|
||||
func (n NoMatch) GetKey() string {
|
||||
return n.Key
|
||||
}
|
||||
|
||||
var alphaDashPattern = regexp.MustCompile("[^\\d\\w-_]")
|
||||
|
||||
type AlphaDash struct {
|
||||
NoMatch
|
||||
Key string
|
||||
}
|
||||
|
||||
func (a AlphaDash) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be valid characters")
|
||||
}
|
||||
|
||||
func (a AlphaDash) GetKey() string {
|
||||
return a.Key
|
||||
}
|
||||
|
||||
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
|
||||
|
||||
type Email struct {
|
||||
Match
|
||||
Key string
|
||||
}
|
||||
|
||||
func (e Email) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be a valid email address")
|
||||
}
|
||||
|
||||
func (e Email) GetKey() string {
|
||||
return e.Key
|
||||
}
|
||||
|
||||
var ipPattern = regexp.MustCompile("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)")
|
||||
|
||||
type IP struct {
|
||||
Match
|
||||
Key string
|
||||
}
|
||||
|
||||
func (i IP) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be a valid ip address")
|
||||
}
|
||||
|
||||
func (i IP) GetKey() string {
|
||||
return i.Key
|
||||
}
|
||||
|
||||
var base64Pattern = regexp.MustCompile("^(?:[A-Za-z0-99+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")
|
||||
|
||||
type Base64 struct {
|
||||
Match
|
||||
Key string
|
||||
}
|
||||
|
||||
func (b Base64) DefaultMessage() string {
|
||||
return fmt.Sprintln("Must be valid base64 characters")
|
||||
}
|
||||
|
||||
func (b Base64) GetKey() string {
|
||||
return b.Key
|
||||
}
|
||||
|
Reference in New Issue
Block a user