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 3a519205
authored
May 29, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extracted validation logic
1 parent
7625843d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
74 deletions
lib/dao-validator.js
lib/dao.js
spec/dao.validations.spec.js
lib/dao-validator.js
0 → 100644
View file @
3a51920
var
Validator
=
require
(
"validator"
)
,
Utils
=
require
(
"./utils"
)
var
DaoValidator
=
module
.
exports
=
function
(
model
)
{
this
.
model
=
model
}
DaoValidator
.
prototype
.
validate
=
function
()
{
var
self
=
this
.
model
var
failures
=
{}
// for each field and value
Utils
.
_
.
each
(
self
.
values
,
function
(
value
,
field
)
{
// if field has validators
var
hasAllowedNull
=
(
self
.
rawAttributes
[
field
].
allowNull
&&
self
.
rawAttributes
[
field
].
allowNull
===
true
&&
(
value
===
null
||
value
===
undefined
));
if
(
self
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
)
{
// 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
fn_method
=
null
// the validation function to call
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?
if
(
Utils
.
_
.
isFunction
(
details
))
{
is_custom_fn
=
true
fn_method
=
Utils
.
_
.
bind
(
details
,
self
,
value
)
}
// is it a validator module function?
else
{
// extra args
fn_args
=
details
.
hasOwnProperty
(
"args"
)
?
details
.
args
:
details
if
(
!
Array
.
isArray
(
fn_args
))
fn_args
=
[
fn_args
]
// error msg
fn_msg
=
details
.
hasOwnProperty
(
"msg"
)
?
details
.
msg
:
false
// check method exists
var
v
=
Validator
.
check
(
value
,
fn_msg
)
if
(
!
Utils
.
_
.
isFunction
(
v
[
validatorType
]))
throw
new
Error
(
"Invalid validator function: "
+
validatorType
)
// bind to validator obj
fn_method
=
Utils
.
_
.
bind
(
v
[
validatorType
],
v
)
}
try
{
fn_method
.
apply
(
null
,
fn_args
)
}
catch
(
err
)
{
err
=
err
.
message
// if we didn't provide a custom error message then augment the default one returned by the validator
if
(
!
fn_msg
&&
!
is_custom_fn
)
err
+=
": "
+
field
// each field can have multiple validation failures stored against it
if
(
failures
.
hasOwnProperty
(
field
))
{
failures
[
field
].
push
(
err
)
}
else
{
failures
[
field
]
=
[
err
]
}
}
})
// for each validator for this field
}
// if field has validator set
})
// for each field
// for each model validator for this DAO
Utils
.
_
.
each
(
self
.
__options
.
validate
,
function
(
validator
,
validatorType
)
{
try
{
validator
.
apply
(
self
)
}
catch
(
err
)
{
failures
[
validatorType
]
=
[
err
.
message
]
// TODO: data structure needs to change for 2.0
}
})
return
failures
}
lib/dao.js
View file @
3a51920
var
Utils
=
require
(
"./utils"
)
,
Mixin
=
require
(
"./associations/mixin"
)
,
Validator
=
require
(
"
validator"
)
,
DataTypes
=
require
(
"./data-types"
)
var
Utils
=
require
(
"./utils"
)
,
Mixin
=
require
(
"./associations/mixin"
)
,
DaoValidator
=
require
(
"./dao-
validator"
)
,
DataTypes
=
require
(
"./data-types"
)
module
.
exports
=
(
function
()
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
var
self
=
this
this
.
dataValues
=
{}
this
.
__options
=
options
this
.
hasPrimaryKeys
=
options
.
hasPrimaryKeys
...
...
@@ -211,74 +209,10 @@ module.exports = (function() {
* @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
*/
DAO
.
prototype
.
validate
=
function
()
{
var
self
=
this
var
failures
=
{}
// for each field and value
Utils
.
_
.
each
(
self
.
values
,
function
(
value
,
field
)
{
// if field has validators
var
hasAllowedNull
=
(
self
.
rawAttributes
[
field
].
allowNull
&&
self
.
rawAttributes
[
field
].
allowNull
===
true
&&
(
value
===
null
||
value
===
undefined
));
if
(
self
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
)
{
// 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
fn_method
=
null
// the validation function to call
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?
if
(
Utils
.
_
.
isFunction
(
details
))
{
is_custom_fn
=
true
fn_method
=
Utils
.
_
.
bind
(
details
,
self
,
value
)
}
// is it a validator module function?
else
{
// extra args
fn_args
=
details
.
hasOwnProperty
(
"args"
)
?
details
.
args
:
details
if
(
!
Array
.
isArray
(
fn_args
))
fn_args
=
[
fn_args
]
// error msg
fn_msg
=
details
.
hasOwnProperty
(
"msg"
)
?
details
.
msg
:
false
// check method exists
var
v
=
Validator
.
check
(
value
,
fn_msg
)
if
(
!
Utils
.
_
.
isFunction
(
v
[
validatorType
]))
throw
new
Error
(
"Invalid validator function: "
+
validatorType
)
// bind to validator obj
fn_method
=
Utils
.
_
.
bind
(
v
[
validatorType
],
v
)
}
try
{
fn_method
.
apply
(
null
,
fn_args
)
}
catch
(
err
)
{
err
=
err
.
message
// if we didn't provide a custom error message then augment the default one returned by the validator
if
(
!
fn_msg
&&
!
is_custom_fn
)
err
+=
": "
+
field
// each field can have multiple validation failures stored against it
if
(
failures
.
hasOwnProperty
(
field
))
{
failures
[
field
].
push
(
err
)
}
else
{
failures
[
field
]
=
[
err
]
}
}
})
// for each validator for this field
}
// if field has validator set
})
// for each field
// for each model validator for this DAO
Utils
.
_
.
each
(
self
.
__options
.
validate
,
function
(
validator
,
validatorType
)
{
try
{
validator
.
apply
(
self
)
}
catch
(
err
)
{
failures
[
validatorType
]
=
[
err
.
message
]
// TODO: data structure needs to change for 2.0
}
})
var
validator
=
new
DaoValidator
(
this
)
,
errors
=
validator
.
validate
()
return
(
Utils
.
_
.
isEmpty
(
failures
)
?
null
:
failure
s
)
return
(
Utils
.
_
.
isEmpty
(
errors
)
?
null
:
error
s
)
}
...
...
spec/dao.validations.spec.js
View file @
3a51920
...
...
@@ -7,7 +7,7 @@ if(typeof require === 'function') {
buster
.
spec
.
expose
()
describe
(
Helpers
.
getTestDialectTeaser
(
"D
AO
"
),
function
()
{
describe
(
Helpers
.
getTestDialectTeaser
(
"D
aoValidator
"
),
function
()
{
describe
(
'validations'
,
function
()
{
before
(
function
(
done
)
{
Helpers
.
initTests
({
...
...
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