Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
278f8eb1
authored
2013-10-09 20:26:23 +0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add ValidFormer interface, can custom valid
1 parent
658a671b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
33 deletions
validation/validation.go
validation/validation.go
View file @
278f8eb
...
...
@@ -7,6 +7,10 @@ import (
"strings"
)
type
ValidFormer
interface
{
Valid
(
*
Validation
)
}
type
ValidationError
struct
{
Message
,
Key
,
Name
,
Field
,
Tmpl
string
Value
interface
{}
...
...
@@ -21,9 +25,35 @@ func (e *ValidationError) String() string {
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.
type
Validation
struct
{
Errors
[]
*
ValidationError
ErrorsMap
map
[
string
]
*
ValidationError
}
func
(
v
*
Validation
)
Clear
()
{
...
...
@@ -38,13 +68,7 @@ func (v *Validation) HasErrors() bool {
// 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
(
v
*
Validation
)
ErrorMap
()
map
[
string
]
*
ValidationError
{
m
:=
map
[
string
]
*
ValidationError
{}
for
_
,
e
:=
range
v
.
Errors
{
if
_
,
ok
:=
m
[
e
.
Key
];
!
ok
{
m
[
e
.
Key
]
=
e
}
}
return
m
return
v
.
ErrorsMap
}
// Add an error to the validation context.
...
...
@@ -57,31 +81,6 @@ func (v *Validation) Error(message string, args ...interface{}) *ValidationResul
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)
func
(
v
*
Validation
)
Required
(
obj
interface
{},
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Required
{
key
},
obj
)
...
...
@@ -192,7 +191,7 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
Tmpl
:
MessageTmpls
[
Name
],
LimitValue
:
chk
.
GetLimitValue
(),
}
v
.
Errors
=
append
(
v
.
Errors
,
err
)
v
.
setError
(
err
)
// Also return it in the result.
return
&
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
// ValidationResult from the first one that fails, or the last one that
// succeeds.
...
...
@@ -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
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment