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 f5066366
authored
Jan 10, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validation skipping logic was inverted. Fixed #1169
1 parent
f5d6065f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
11 deletions
lib/dao-factory.js
lib/dao-validator.js
test/dao-factory/create.test.js
test/dao.validations.test.js
lib/dao-factory.js
View file @
f506636
...
...
@@ -870,9 +870,14 @@ module.exports = (function() {
options
.
fields
=
newFields
||
options
.
fields
if
(
options
.
validate
===
true
)
{
if
(
options
.
fields
.
length
)
{
var
skippedFields
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
options
.
fields
);
}
if
(
options
.
hooks
===
true
)
{
var
iterate
=
function
(
i
)
{
daos
[
i
].
hookValidate
({
skip
:
options
.
fields
}).
error
(
function
(
err
)
{
daos
[
i
].
hookValidate
({
skip
:
skippedFields
}).
error
(
function
(
err
)
{
errors
[
errors
.
length
]
=
{
record
:
v
,
errors
:
err
}
i
++
if
(
i
>
daos
.
length
)
{
...
...
@@ -887,7 +892,7 @@ module.exports = (function() {
}
}
else
{
daos
.
forEach
(
function
(
v
)
{
var
valid
=
v
.
validate
({
skip
:
options
.
f
ields
})
var
valid
=
v
.
validate
({
skip
:
skippedF
ields
})
if
(
valid
!==
null
)
{
errors
[
errors
.
length
]
=
{
record
:
v
,
errors
:
valid
}
}
...
...
@@ -1145,7 +1150,12 @@ module.exports = (function() {
if
(
options
.
validate
===
true
)
{
var
build
=
self
.
build
(
attrValueHash
)
build
.
hookValidate
({
skip
:
Object
.
keys
(
attrValueHash
)}).
error
(
function
(
err
)
{
// We want to skip validations for all other fields
var
updatedFields
=
Object
.
keys
(
attrValueHash
)
var
skippedFields
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
updatedFields
)
build
.
hookValidate
({
skip
:
skippedFields
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}).
success
(
function
(
attributes
)
{
if
(
!!
attributes
&&
!!
attributes
.
dataValues
)
{
...
...
lib/dao-validator.js
View file @
f506636
...
...
@@ -71,7 +71,7 @@ var validateAttributes = function() {
Utils
.
_
.
each
(
this
.
model
.
rawAttributes
,
function
(
rawAttribute
,
field
)
{
var
value
=
self
.
model
.
dataValues
[
field
]
||
undefined
,
hasAllowedNull
=
((
rawAttribute
===
undefined
||
rawAttribute
.
allowNull
===
true
)
&&
((
value
===
null
)
||
(
value
===
undefined
)))
,
isSkipped
=
self
.
options
.
skip
.
length
>
0
&&
self
.
options
.
skip
.
indexOf
(
field
)
=
==
-
1
,
isSkipped
=
self
.
options
.
skip
.
length
>
0
&&
self
.
options
.
skip
.
indexOf
(
field
)
!
==
-
1
if
(
self
.
model
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
&&
!
isSkipped
)
{
errors
=
Utils
.
_
.
merge
(
errors
,
validateAttribute
.
call
(
self
,
value
,
field
))
...
...
test/dao-factory/create.test.js
View file @
f506636
...
...
@@ -597,7 +597,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it
(
'should allow blank creates (with timestamps: false)'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
create
({},
{
fields
:
[]}).
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
worker
).
to
.
be
.
ok
...
...
@@ -608,7 +608,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it
(
'should allow truly blank creates'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
create
({},
{
fields
:
[]}).
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
worker
).
to
.
be
.
ok
...
...
@@ -896,7 +896,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it
(
'should allow blank arrays (return immediatly)'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{})
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
bulkCreate
([]).
done
(
function
(
err
,
workers
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
workers
).
to
.
be
.
ok
...
...
@@ -908,7 +908,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it
(
'should allow blank creates (with timestamps: false)'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
bulkCreate
([{},
{}]).
done
(
function
(
err
,
workers
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
workers
).
to
.
be
.
ok
...
...
@@ -919,7 +919,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it
(
'should allow autoincremented attributes to be set'
,
function
(
done
)
{
var
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{},
{
timestamps
:
false
})
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
sync
().
done
(
function
(
err
)
{
Worker
.
bulkCreate
([
{
id
:
5
},
{
id
:
10
}
...
...
@@ -953,4 +953,4 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
\ No newline at end of file
})
test/dao.validations.test.js
View file @
f506636
...
...
@@ -562,5 +562,24 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
expect
(
errors
.
field
).
to
.
have
.
length
(
1
)
expect
(
errors
.
field
[
0
]).
to
.
equal
(
"Unexpected value or invalid argument: field"
)
})
it
(
'skips validations for the given fields'
,
function
()
{
var
values
=
[
'value1'
,
'value2'
]
var
Bar
=
this
.
sequelize
.
define
(
'Bar'
+
config
.
rand
(),
{
field
:
{
type
:
Sequelize
.
ENUM
,
values
:
values
,
validate
:
{
isIn
:
[
values
]
}
}
})
var
failingBar
=
Bar
.
build
({
field
:
'value3'
})
,
errors
=
failingBar
.
validate
({
skip
:
[
'field'
]
})
expect
(
errors
).
to
.
be
.
null
})
})
})
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