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 3c054531
authored
May 09, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validations will now be called upon .save() and allowNull: true skips validation…
…s (if the value is null).
1 parent
7e9d13de
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
2 deletions
lib/dao.js
spec/dao.spec.js
lib/dao.js
View file @
3c05453
...
...
@@ -150,7 +150,14 @@ module.exports = (function() {
this
[
updatedAtAttr
]
=
values
[
updatedAtAttr
]
=
Utils
.
now
()
}
if
(
this
.
isNewRecord
)
{
var
errors
=
this
.
validate
()
if
(
!!
errors
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
emitter
.
emit
(
'error'
,
errors
)
}).
run
()
}
else
if
(
this
.
isNewRecord
)
{
return
this
.
QueryInterface
.
insert
(
this
,
this
.
QueryInterface
.
QueryGenerator
.
addSchema
(
this
.
__factory
),
values
)
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
;
...
...
@@ -211,7 +218,9 @@ module.exports = (function() {
Utils
.
_
.
each
(
self
.
values
,
function
(
value
,
field
)
{
// if field has validators
if
(
self
.
validators
.
hasOwnProperty
(
field
))
{
var
allowsNulls
=
(
self
.
rawAttributes
[
field
].
allowNull
&&
self
.
rawAttributes
[
field
].
allowNull
===
true
&&
(
value
===
null
||
value
===
undefined
));
if
(
self
.
validators
.
hasOwnProperty
(
field
)
&&
!
allowsNulls
)
{
// for each validator
Utils
.
_
.
each
(
self
.
validators
[
field
],
function
(
details
,
validatorType
)
{
...
...
spec/dao.spec.js
View file @
3c05453
...
...
@@ -20,6 +20,18 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
touchedAt
:
{
type
:
DataTypes
.
DATE
,
defaultValue
:
DataTypes
.
NOW
},
aNumber
:
{
type
:
DataTypes
.
INTEGER
},
bNumber
:
{
type
:
DataTypes
.
INTEGER
},
validateTest
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
true
,
validate
:
{
isInt
:
true
}
},
validateCustom
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
true
,
validate
:
{
len
:
{
msg
:
'Length failed.'
,
args
:
[
1
,
20
]}}
},
dateAllowNullTrue
:
{
type
:
DataTypes
.
DATE
,
allowNull
:
true
...
...
@@ -378,6 +390,44 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
})
describe
(
'save'
,
function
()
{
it
(
'should fail a validation upon creating'
,
function
(
done
){
this
.
User
.
create
({
aNumber
:
0
,
validateTest
:
'hello'
}).
error
(
function
(
err
){
expect
(
err
).
toBeDefined
()
expect
(
err
).
toBeObject
()
expect
(
err
.
validateTest
).
toBeArray
()
expect
(
err
.
validateTest
[
0
]).
toBeDefined
()
expect
(
err
.
validateTest
[
0
].
indexOf
(
'Invalid integer'
)).
toBeGreaterThan
(
-
1
);
done
();
});
})
it
(
'should fail a validation upon building'
,
function
(
done
){
this
.
User
.
build
({
aNumber
:
0
,
validateCustom
:
'aaaaaaaaaaaaaaaaaaaaaaaaaa'
}).
save
()
.
error
(
function
(
err
){
expect
(
err
).
toBeDefined
()
expect
(
err
).
toBeObject
()
expect
(
err
.
validateCustom
).
toBeDefined
()
expect
(
err
.
validateCustom
).
toBeArray
()
expect
(
err
.
validateCustom
[
0
]).
toBeDefined
()
expect
(
err
.
validateCustom
[
0
]).
toEqual
(
'Length failed.'
)
done
()
})
})
it
(
'should fail a validation when updating'
,
function
(
done
){
this
.
User
.
create
({
aNumber
:
0
}).
success
(
function
(
user
){
user
.
updateAttributes
({
validateTest
:
'hello'
}).
error
(
function
(
err
){
expect
(
err
).
toBeDefined
()
expect
(
err
).
toBeObject
()
expect
(
err
.
validateTest
).
toBeDefined
()
expect
(
err
.
validateTest
).
toBeArray
()
expect
(
err
.
validateTest
[
0
]).
toBeDefined
()
expect
(
err
.
validateTest
[
0
].
indexOf
(
'Invalid integer:'
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
it
(
'takes zero into account'
,
function
(
done
)
{
this
.
User
.
build
({
aNumber
:
0
}).
save
([
'aNumber'
]).
success
(
function
(
user
)
{
expect
(
user
.
aNumber
).
toEqual
(
0
)
...
...
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