Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
278f8eb1
Commit
278f8eb1
authored
Oct 09, 2013
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add ValidFormer interface, can custom valid
parent
658a671b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
34 deletions
+56
-34
validation.go
validation/validation.go
+56
-34
No files found.
validation/validation.go
View file @
278f8eb1
...
@@ -7,6 +7,10 @@ import (
...
@@ -7,6 +7,10 @@ import (
"strings"
"strings"
)
)
type
ValidFormer
interface
{
Valid
(
*
Validation
)
}
type
ValidationError
struct
{
type
ValidationError
struct
{
Message
,
Key
,
Name
,
Field
,
Tmpl
string
Message
,
Key
,
Name
,
Field
,
Tmpl
string
Value
interface
{}
Value
interface
{}
...
@@ -21,9 +25,35 @@ func (e *ValidationError) String() string {
...
@@ -21,9 +25,35 @@ func (e *ValidationError) String() string {
return
e
.
Message
return
e
.
Message
}
}
// A ValidationResult is returned from every validation method.
// It provides an indication of success, and a pointer to the Error (if any).
type
ValidationResult
struct
{
Error
*
ValidationError
Ok
bool
}
func
(
r
*
ValidationResult
)
Key
(
key
string
)
*
ValidationResult
{
if
r
.
Error
!=
nil
{
r
.
Error
.
Key
=
key
}
return
r
}
func
(
r
*
ValidationResult
)
Message
(
message
string
,
args
...
interface
{})
*
ValidationResult
{
if
r
.
Error
!=
nil
{
if
len
(
args
)
==
0
{
r
.
Error
.
Message
=
message
}
else
{
r
.
Error
.
Message
=
fmt
.
Sprintf
(
message
,
args
...
)
}
}
return
r
}
// A Validation context manages data validation and error messages.
// A Validation context manages data validation and error messages.
type
Validation
struct
{
type
Validation
struct
{
Errors
[]
*
ValidationError
Errors
[]
*
ValidationError
ErrorsMap
map
[
string
]
*
ValidationError
}
}
func
(
v
*
Validation
)
Clear
()
{
func
(
v
*
Validation
)
Clear
()
{
...
@@ -38,13 +68,7 @@ func (v *Validation) HasErrors() bool {
...
@@ -38,13 +68,7 @@ func (v *Validation) HasErrors() bool {
// If there are multiple validation errors associated with a single key, the
// If there are multiple validation errors associated with a single key, the
// first one "wins". (Typically the first validation will be the more basic).
// first one "wins". (Typically the first validation will be the more basic).
func
(
v
*
Validation
)
ErrorMap
()
map
[
string
]
*
ValidationError
{
func
(
v
*
Validation
)
ErrorMap
()
map
[
string
]
*
ValidationError
{
m
:=
map
[
string
]
*
ValidationError
{}
return
v
.
ErrorsMap
for
_
,
e
:=
range
v
.
Errors
{
if
_
,
ok
:=
m
[
e
.
Key
];
!
ok
{
m
[
e
.
Key
]
=
e
}
}
return
m
}
}
// Add an error to the validation context.
// Add an error to the validation context.
...
@@ -57,31 +81,6 @@ func (v *Validation) Error(message string, args ...interface{}) *ValidationResul
...
@@ -57,31 +81,6 @@ func (v *Validation) Error(message string, args ...interface{}) *ValidationResul
return
result
return
result
}
}
// A ValidationResult is returned from every validation method.
// It provides an indication of success, and a pointer to the Error (if any).
type
ValidationResult
struct
{
Error
*
ValidationError
Ok
bool
}
func
(
r
*
ValidationResult
)
Key
(
key
string
)
*
ValidationResult
{
if
r
.
Error
!=
nil
{
r
.
Error
.
Key
=
key
}
return
r
}
func
(
r
*
ValidationResult
)
Message
(
message
string
,
args
...
interface
{})
*
ValidationResult
{
if
r
.
Error
!=
nil
{
if
len
(
args
)
==
0
{
r
.
Error
.
Message
=
message
}
else
{
r
.
Error
.
Message
=
fmt
.
Sprintf
(
message
,
args
...
)
}
}
return
r
}
// Test that the argument is non-nil and non-empty (if string or list)
// Test that the argument is non-nil and non-empty (if string or list)
func
(
v
*
Validation
)
Required
(
obj
interface
{},
key
string
)
*
ValidationResult
{
func
(
v
*
Validation
)
Required
(
obj
interface
{},
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Required
{
key
},
obj
)
return
v
.
apply
(
Required
{
key
},
obj
)
...
@@ -192,7 +191,7 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
...
@@ -192,7 +191,7 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
Tmpl
:
MessageTmpls
[
Name
],
Tmpl
:
MessageTmpls
[
Name
],
LimitValue
:
chk
.
GetLimitValue
(),
LimitValue
:
chk
.
GetLimitValue
(),
}
}
v
.
Errors
=
append
(
v
.
Errors
,
err
)
v
.
setError
(
err
)
// Also return it in the result.
// Also return it in the result.
return
&
ValidationResult
{
return
&
ValidationResult
{
...
@@ -201,6 +200,22 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
...
@@ -201,6 +200,22 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
}
}
}
}
func
(
v
*
Validation
)
setError
(
err
*
ValidationError
)
{
v
.
Errors
=
append
(
v
.
Errors
,
err
)
if
v
.
ErrorsMap
==
nil
{
v
.
ErrorsMap
=
make
(
map
[
string
]
*
ValidationError
)
}
if
_
,
ok
:=
v
.
ErrorsMap
[
err
.
Field
];
!
ok
{
v
.
ErrorsMap
[
err
.
Field
]
=
err
}
}
func
(
v
*
Validation
)
SetError
(
fieldName
string
,
errMsg
string
)
*
ValidationError
{
err
:=
&
ValidationError
{
Key
:
fieldName
,
Field
:
fieldName
,
Tmpl
:
errMsg
,
Message
:
errMsg
}
v
.
setError
(
err
)
return
err
}
// Apply a group of validators to a field, in order, and return the
// Apply a group of validators to a field, in order, and return the
// ValidationResult from the first one that fails, or the last one that
// ValidationResult from the first one that fails, or the last one that
// succeeds.
// succeeds.
...
@@ -241,5 +256,12 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
...
@@ -241,5 +256,12 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
}
}
}
}
}
}
if
!
v
.
HasErrors
()
{
if
form
,
ok
:=
obj
.
(
ValidFormer
);
ok
{
form
.
Valid
(
v
)
}
}
return
!
v
.
HasErrors
(),
nil
return
!
v
.
HasErrors
(),
nil
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment