Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 2e96f20e
authored
May 29, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
splitted validation logic into multiple parts
1 parent
3a519205
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
51 deletions
lib/dao-validator.js
spec/dao.validations.spec.js
lib/dao-validator.js
View file @
2e96f20
...
@@ -6,72 +6,113 @@ var DaoValidator = module.exports = function(model) {
...
@@ -6,72 +6,113 @@ var DaoValidator = module.exports = function(model) {
}
}
DaoValidator
.
prototype
.
validate
=
function
()
{
DaoValidator
.
prototype
.
validate
=
function
()
{
var
self
=
this
.
model
var
errors
=
{}
var
failures
=
{}
// for each field and value
errors
=
Utils
.
_
.
extend
(
errors
,
validateAttributes
.
call
(
this
))
Utils
.
_
.
each
(
self
.
values
,
function
(
value
,
field
)
{
errors
=
Utils
.
_
.
extend
(
errors
,
validateModel
.
call
(
this
))
// if field has validat
ors
return
err
ors
var
hasAllowedNull
=
(
self
.
rawAttributes
[
field
].
allowNull
&&
self
.
rawAttributes
[
field
].
allowNull
===
true
&&
(
value
===
null
||
value
===
undefined
));
}
if
(
self
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
)
{
// private
// for each validator
Utils
.
_
.
each
(
self
.
validators
[
field
],
function
(
details
,
validatorType
)
{
var
is_custom_fn
=
false
// if true then it's a custom validation method
var
validateModel
=
function
()
{
var
fn_method
=
null
// the validation function to call
var
errors
=
{}
var
fn_args
=
[]
// extra arguments to pass to validation function
var
fn_msg
=
""
// the error message to return if validation fails
// is it a custom validator function?
// for each model validator for this DAO
if
(
Utils
.
_
.
isFunction
(
details
))
{
Utils
.
_
.
each
(
this
.
model
.
__options
.
validate
,
function
(
validator
,
validatorType
)
{
is_custom_fn
=
true
try
{
fn_method
=
Utils
.
_
.
bind
(
details
,
self
,
value
)
validator
.
apply
(
this
.
model
)
}
catch
(
err
)
{
errors
[
validatorType
]
=
[
err
.
message
]
// TODO: data structure needs to change for 2.0
}
}
// is it a validator module function?
}.
bind
(
this
))
else
{
// extra args
return
errors
fn_args
=
details
.
hasOwnProperty
(
"args"
)
?
details
.
args
:
details
}
if
(
!
Array
.
isArray
(
fn_args
))
fn_args
=
[
fn_args
]
var
validateAttributes
=
function
()
{
// error msg
var
errors
=
{}
fn_msg
=
details
.
hasOwnProperty
(
"msg"
)
?
details
.
msg
:
false
// check method exists
// for each field and value
var
v
=
Validator
.
check
(
value
,
fn_msg
)
Utils
.
_
.
each
(
this
.
model
.
values
,
function
(
value
,
field
)
{
if
(
!
Utils
.
_
.
isFunction
(
v
[
validatorType
]))
var
rawAttribute
=
this
.
model
.
rawAttributes
[
field
]
throw
new
Error
(
"Invalid validator function: "
+
validatorType
)
,
hasAllowedNull
=
((
rawAttribute
.
allowNull
===
true
)
&&
((
value
===
null
)
||
(
value
===
undefined
)))
// bind to validator obj
fn_method
=
Utils
.
_
.
bind
(
v
[
validatorType
],
v
)
if
(
this
.
model
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
)
{
errors
=
Utils
.
_
.
merge
(
errors
,
validateAttribute
.
call
(
this
,
value
,
field
))
}
}
}.
bind
(
this
))
// for each field
return
errors
}
var
validateAttribute
=
function
(
value
,
field
)
{
var
errors
=
{}
// for each validator
Utils
.
_
.
each
(
this
.
model
.
validators
[
field
],
function
(
details
,
validatorType
)
{
var
validator
=
prepareValidationOfAttribute
.
call
(
this
,
value
,
details
,
validatorType
)
try
{
try
{
fn_method
.
apply
(
null
,
fn_
args
)
validator
.
fn
.
apply
(
null
,
validator
.
args
)
}
catch
(
err
)
{
}
catch
(
err
)
{
err
=
err
.
message
var
msg
=
err
.
message
// if we didn't provide a custom error message then augment the default one returned by the validator
// if we didn't provide a custom error message then augment the default one returned by the validator
if
(
!
fn_msg
&&
!
is_custom_fn
)
if
(
!
validator
.
msg
&&
!
validator
.
isCustom
)
{
err
+=
": "
+
field
msg
+=
": "
+
field
// each field can have multiple validation failures stored against it
if
(
failures
.
hasOwnProperty
(
field
))
{
failures
[
field
].
push
(
err
)
}
else
{
failures
[
field
]
=
[
err
]
}
}
// each field can have multiple validation errors stored against it
errors
[
field
]
=
errors
[
field
]
||
[]
errors
[
field
].
push
(
msg
)
}
}
}.
bind
(
this
))
// for each validator for this field
})
// for each validator for this field
return
errors
}
// if field has validator set
}
})
// for each field
// for each model validator for this DAO
var
prepareValidationOfAttribute
=
function
(
value
,
details
,
validatorType
)
{
Utils
.
_
.
each
(
self
.
__options
.
validate
,
function
(
validator
,
validatorType
)
{
var
isCustomValidator
=
false
// if true then it's a custom validation method
try
{
,
validatorFunction
=
null
// the validation function to call
validator
.
apply
(
self
)
,
validatorArgs
=
[]
// extra arguments to pass to validation function
}
catch
(
err
)
{
,
errorMessage
=
""
// the error message to return if validation fails
failures
[
validatorType
]
=
[
err
.
message
]
// TODO: data structure needs to change for 2.0
if
(
typeof
details
===
'function'
)
{
// it is a custom validator function?
isCustomValidator
=
true
validatorFunction
=
Utils
.
_
.
bind
(
details
,
this
.
model
,
value
)
}
else
{
// it is a validator module function?
// extract extra arguments for the validator
validatorArgs
=
details
.
hasOwnProperty
(
"args"
)
?
details
.
args
:
details
if
(
!
Array
.
isArray
(
validatorArgs
))
{
validatorArgs
=
[
validatorArgs
]
}
}
})
return
failures
// extract the error msg
errorMessage
=
details
.
hasOwnProperty
(
"msg"
)
?
details
.
msg
:
false
// check method exists
var
validator
=
Validator
.
check
(
value
,
errorMessage
)
// check if Validator knows that kind of validation test
if
(
!
Utils
.
_
.
isFunction
(
validator
[
validatorType
]))
{
throw
new
Error
(
"Invalid validator function: "
+
validatorType
)
}
// bind to validator obj
validatorFunction
=
Utils
.
_
.
bind
(
validator
[
validatorType
],
validator
)
}
return
{
fn
:
validatorFunction
,
msg
:
errorMessage
,
args
:
validatorArgs
,
isCustom
:
isCustomValidator
}
}
}
spec/dao.validations.spec.js
View file @
2e96f20
...
@@ -329,6 +329,7 @@ describe(Helpers.getTestDialectTeaser("DaoValidator"), function() {
...
@@ -329,6 +329,7 @@ describe(Helpers.getTestDialectTeaser("DaoValidator"), function() {
var
failingFoo
=
Foo
.
build
({
field1
:
null
,
field2
:
null
})
var
failingFoo
=
Foo
.
build
({
field1
:
null
,
field2
:
null
})
,
errors
=
failingFoo
.
validate
()
,
errors
=
failingFoo
.
validate
()
expect
(
errors
).
not
.
toBeNull
()
expect
(
errors
).
not
.
toBeNull
()
expect
(
errors
).
toEqual
({
'xnor'
:
[
'xnor failed'
]
})
expect
(
errors
).
toEqual
({
'xnor'
:
[
'xnor failed'
]
})
...
...
Write
Preview
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