1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 19:00:50 +00:00

validation add more info to ValidationError, and put all messages tmpl to a map

This commit is contained in:
slene 2013-09-10 21:51:25 +08:00
parent 02a03cec33
commit ea9c2cebfd
2 changed files with 144 additions and 22 deletions

View File

@ -4,10 +4,13 @@ import (
"fmt"
"reflect"
"regexp"
"strings"
)
type ValidationError struct {
Message, Key string
Message, Key, Name, Field, Tmpl string
Value interface{}
LimitValue interface{}
}
// Returns the Message.
@ -170,9 +173,24 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
}
// Add the error to the validation context.
key := chk.GetKey()
Name := key
Field := ""
parts := strings.Split(key, ".")
if len(parts) == 2 {
Field = parts[0]
Name = parts[1]
}
err := &ValidationError{
Message: chk.DefaultMessage(),
Key: chk.GetKey(),
Message: chk.DefaultMessage(),
Key: key,
Name: Name,
Field: Field,
Value: obj,
Tmpl: MessageTmpls[Name],
LimitValue: chk.GetLimitValue(),
}
v.Errors = append(v.Errors, err)

View File

@ -7,10 +7,34 @@ import (
"time"
)
var MessageTmpls = map[string]string{
"Required": "Can not be empty",
"Min": "Minimum is %d",
"Max": "Maximum is %d",
"Range": "Range is %d to %d",
"MinSize": "Minimum size is %d",
"MaxSize": "Maximum size is %d",
"Length": "Required length is %d",
"Alpha": "Must be valid alpha characters",
"Numeric": "Must be valid numeric characters",
"AlphaNumeric": "Must be valid alpha or numeric characters",
"Match": "Must match %s",
"NoMatch": "Must not match %s",
"AlphaDash": "Must be valid alpha or numeric or dash(-_) characters",
"Email": "Must be a valid email address",
"IP": "Must be a valid ip address",
"Base64": "Must be valid base64 characters",
"Mobile": "Must be valid mobile number",
"Tel": "Must be valid telephone number",
"Phone": "Must be valid telephone or mobile phone number",
"ZipCode": "Must be valid zipcode",
}
type Validator interface {
IsSatisfied(interface{}) bool
DefaultMessage() string
GetKey() string
GetLimitValue() interface{}
}
type Required struct {
@ -49,6 +73,10 @@ func (r Required) GetKey() string {
return r.Key
}
func (r Required) GetLimitValue() interface{} {
return nil
}
type Min struct {
Min int
Key string
@ -63,13 +91,17 @@ func (m Min) IsSatisfied(obj interface{}) bool {
}
func (m Min) DefaultMessage() string {
return fmt.Sprint("Minimum is ", m.Min)
return fmt.Sprintf(MessageTmpls["Min"], m.Min)
}
func (m Min) GetKey() string {
return m.Key
}
func (m Min) GetLimitValue() interface{} {
return m.Min
}
type Max struct {
Max int
Key string
@ -84,13 +116,17 @@ func (m Max) IsSatisfied(obj interface{}) bool {
}
func (m Max) DefaultMessage() string {
return fmt.Sprint("Maximum is ", m.Max)
return fmt.Sprintf(MessageTmpls["Max"], m.Max)
}
func (m Max) GetKey() string {
return m.Key
}
func (m Max) GetLimitValue() interface{} {
return m.Max
}
// Requires an integer to be within Min, Max inclusive.
type Range struct {
Min
@ -103,13 +139,17 @@ func (r Range) IsSatisfied(obj interface{}) bool {
}
func (r Range) DefaultMessage() string {
return fmt.Sprint("Range is ", r.Min.Min, " to ", r.Max.Max)
return fmt.Sprintf(MessageTmpls["Range"], r.Min.Min, r.Max.Max)
}
func (r Range) GetKey() string {
return r.Key
}
func (r Range) GetLimitValue() interface{} {
return []int{r.Min.Min, r.Max.Max}
}
// Requires an array or string to be at least a given length.
type MinSize struct {
Min int
@ -128,13 +168,17 @@ func (m MinSize) IsSatisfied(obj interface{}) bool {
}
func (m MinSize) DefaultMessage() string {
return fmt.Sprint("Minimum size is ", m.Min)
return fmt.Sprintf(MessageTmpls["MinSize"], m.Min)
}
func (m MinSize) GetKey() string {
return m.Key
}
func (m MinSize) GetLimitValue() interface{} {
return m.Min
}
// Requires an array or string to be at most a given length.
type MaxSize struct {
Max int
@ -153,13 +197,17 @@ func (m MaxSize) IsSatisfied(obj interface{}) bool {
}
func (m MaxSize) DefaultMessage() string {
return fmt.Sprint("Maximum size is ", m.Max)
return fmt.Sprintf(MessageTmpls["MaxSize"], m.Max)
}
func (m MaxSize) GetKey() string {
return m.Key
}
func (m MaxSize) GetLimitValue() interface{} {
return m.Max
}
// Requires an array or string to be exactly a given length.
type Length struct {
N int
@ -178,13 +226,17 @@ func (l Length) IsSatisfied(obj interface{}) bool {
}
func (l Length) DefaultMessage() string {
return fmt.Sprint("Required length is ", l.N)
return fmt.Sprintf(MessageTmpls["Length"], l.N)
}
func (l Length) GetKey() string {
return l.Key
}
func (l Length) GetLimitValue() interface{} {
return l.N
}
type Alpha struct {
Key string
}
@ -202,13 +254,17 @@ func (a Alpha) IsSatisfied(obj interface{}) bool {
}
func (a Alpha) DefaultMessage() string {
return fmt.Sprint("Must be valid alpha characters")
return fmt.Sprint(MessageTmpls["Alpha"])
}
func (a Alpha) GetKey() string {
return a.Key
}
func (a Alpha) GetLimitValue() interface{} {
return nil
}
type Numeric struct {
Key string
}
@ -226,13 +282,17 @@ func (n Numeric) IsSatisfied(obj interface{}) bool {
}
func (n Numeric) DefaultMessage() string {
return fmt.Sprint("Must be valid numeric characters")
return fmt.Sprint(MessageTmpls["Numeric"])
}
func (n Numeric) GetKey() string {
return n.Key
}
func (n Numeric) GetLimitValue() interface{} {
return nil
}
type AlphaNumeric struct {
Key string
}
@ -250,13 +310,17 @@ func (a AlphaNumeric) IsSatisfied(obj interface{}) bool {
}
func (a AlphaNumeric) DefaultMessage() string {
return fmt.Sprint("Must be valid alpha or numeric characters")
return fmt.Sprint(MessageTmpls["AlphaNumeric"])
}
func (a AlphaNumeric) GetKey() string {
return a.Key
}
func (a AlphaNumeric) GetLimitValue() interface{} {
return nil
}
// Requires a string to match a given regex.
type Match struct {
Regexp *regexp.Regexp
@ -268,13 +332,17 @@ func (m Match) IsSatisfied(obj interface{}) bool {
}
func (m Match) DefaultMessage() string {
return fmt.Sprint("Must match ", m.Regexp)
return fmt.Sprintf(MessageTmpls["Match"], m.Regexp.String())
}
func (m Match) GetKey() string {
return m.Key
}
func (m Match) GetLimitValue() interface{} {
return m.Regexp.String()
}
// Requires a string to not match a given regex.
type NoMatch struct {
Match
@ -286,13 +354,17 @@ func (n NoMatch) IsSatisfied(obj interface{}) bool {
}
func (n NoMatch) DefaultMessage() string {
return fmt.Sprint("Must not match ", n.Regexp)
return fmt.Sprintf(MessageTmpls["NoMatch"], n.Regexp.String())
}
func (n NoMatch) GetKey() string {
return n.Key
}
func (n NoMatch) GetLimitValue() interface{} {
return n.Regexp.String()
}
var alphaDashPattern = regexp.MustCompile("[^\\d\\w-_]")
type AlphaDash struct {
@ -301,13 +373,17 @@ type AlphaDash struct {
}
func (a AlphaDash) DefaultMessage() string {
return fmt.Sprint("Must be valid alpha or numeric or dash(-_) characters")
return fmt.Sprint(MessageTmpls["AlphaDash"])
}
func (a AlphaDash) GetKey() string {
return a.Key
}
func (a AlphaDash) GetLimitValue() interface{} {
return nil
}
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
type Email struct {
@ -316,13 +392,17 @@ type Email struct {
}
func (e Email) DefaultMessage() string {
return fmt.Sprint("Must be a valid email address")
return fmt.Sprint(MessageTmpls["Email"])
}
func (e Email) GetKey() string {
return e.Key
}
func (e Email) GetLimitValue() interface{} {
return nil
}
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 {
@ -331,13 +411,17 @@ type IP struct {
}
func (i IP) DefaultMessage() string {
return fmt.Sprint("Must be a valid ip address")
return fmt.Sprint(MessageTmpls["IP"])
}
func (i IP) GetKey() string {
return i.Key
}
func (i IP) GetLimitValue() interface{} {
return nil
}
var base64Pattern = regexp.MustCompile("^(?:[A-Za-z0-99+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")
type Base64 struct {
@ -346,13 +430,17 @@ type Base64 struct {
}
func (b Base64) DefaultMessage() string {
return fmt.Sprint("Must be valid base64 characters")
return fmt.Sprint(MessageTmpls["Base64"])
}
func (b Base64) GetKey() string {
return b.Key
}
func (b Base64) GetLimitValue() interface{} {
return nil
}
// just for chinese mobile phone number
var mobilePattern = regexp.MustCompile("^((\\+86)|(86))?(1(([35][0-9])|(47)|[8][01236789]))\\d{8}$")
@ -362,13 +450,17 @@ type Mobile struct {
}
func (m Mobile) DefaultMessage() string {
return fmt.Sprint("Must be valid mobile number")
return fmt.Sprint(MessageTmpls["Mobile"])
}
func (m Mobile) GetKey() string {
return m.Key
}
func (m Mobile) GetLimitValue() interface{} {
return nil
}
// just for chinese telephone number
var telPattern = regexp.MustCompile("^(0\\d{2,3}(\\-)?)?\\d{7,8}$")
@ -378,13 +470,17 @@ type Tel struct {
}
func (t Tel) DefaultMessage() string {
return fmt.Sprint("Must be valid telephone number")
return fmt.Sprint(MessageTmpls["Tel"])
}
func (t Tel) GetKey() string {
return t.Key
}
func (t Tel) GetLimitValue() interface{} {
return nil
}
// just for chinese telephone or mobile phone number
type Phone struct {
Mobile
@ -397,13 +493,17 @@ func (p Phone) IsSatisfied(obj interface{}) bool {
}
func (p Phone) DefaultMessage() string {
return fmt.Sprint("Must be valid telephone or mobile phone number")
return fmt.Sprint(MessageTmpls["Phone"])
}
func (p Phone) GetKey() string {
return p.Key
}
func (p Phone) GetLimitValue() interface{} {
return nil
}
// just for chinese zipcode
var zipCodePattern = regexp.MustCompile("^[1-9]\\d{5}$")
@ -413,9 +513,13 @@ type ZipCode struct {
}
func (z ZipCode) DefaultMessage() string {
return fmt.Sprint("Must be valid zipcode")
return fmt.Sprint(MessageTmpls["ZipCode"])
}
func (z ZipCode) GetKey() string {
return z.Key
}
func (z ZipCode) GetLimitValue() interface{} {
return nil
}