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 4e57b889
authored
Sep 06, 2014
by
overlookmotel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Options passed to validate hooks
1 parent
ccda887b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
10 deletions
lib/instance-validator.js
lib/instance.js
lib/model.js
lib/instance-validator.js
View file @
4e57b88
...
@@ -165,14 +165,14 @@ InstanceValidator.prototype.validate = function() {
...
@@ -165,14 +165,14 @@ InstanceValidator.prototype.validate = function() {
*/
*/
InstanceValidator
.
prototype
.
hookValidate
=
function
()
{
InstanceValidator
.
prototype
.
hookValidate
=
function
()
{
var
self
=
this
;
var
self
=
this
;
return
self
.
modelInstance
.
Model
.
runHooks
(
'beforeValidate'
,
self
.
modelInstance
).
then
(
function
()
{
return
self
.
modelInstance
.
Model
.
runHooks
(
'beforeValidate'
,
self
.
modelInstance
,
self
.
options
).
then
(
function
()
{
return
self
.
validate
().
then
(
function
(
error
)
{
return
self
.
validate
().
then
(
function
(
error
)
{
if
(
error
)
{
if
(
error
)
{
throw
error
;
throw
error
;
}
}
});
});
}).
then
(
function
()
{
}).
then
(
function
()
{
return
self
.
modelInstance
.
Model
.
runHooks
(
'afterValidate'
,
self
.
modelInstance
);
return
self
.
modelInstance
.
Model
.
runHooks
(
'afterValidate'
,
self
.
modelInstance
,
self
.
options
);
}).
return
(
self
.
modelInstance
);
}).
return
(
self
.
modelInstance
);
};
};
...
...
lib/instance.js
View file @
4e57b88
...
@@ -495,7 +495,10 @@ module.exports = (function() {
...
@@ -495,7 +495,10 @@ module.exports = (function() {
return
Promise
.
try
(
function
()
{
return
Promise
.
try
(
function
()
{
// Validate
// Validate
if
(
options
.
hooks
)
{
if
(
options
.
hooks
)
{
return
self
.
hookValidate
({
skip
:
_
.
difference
(
Object
.
keys
(
self
.
rawAttributes
),
options
.
fields
)});
options
.
skip
=
_
.
difference
(
Object
.
keys
(
self
.
rawAttributes
),
options
.
fields
);
return
self
.
hookValidate
(
options
).
then
(
function
()
{
delete
options
.
skip
;
});
}
}
}).
then
(
function
()
{
}).
then
(
function
()
{
options
.
fields
.
forEach
(
function
(
field
)
{
options
.
fields
.
forEach
(
function
(
field
)
{
...
@@ -683,8 +686,8 @@ module.exports = (function() {
...
@@ -683,8 +686,8 @@ module.exports = (function() {
return
new
InstanceValidator
(
this
,
options
).
validate
();
return
new
InstanceValidator
(
this
,
options
).
validate
();
};
};
Instance
.
prototype
.
hookValidate
=
function
(
o
bject
)
{
Instance
.
prototype
.
hookValidate
=
function
(
o
ptions
)
{
var
validator
=
new
InstanceValidator
(
this
,
o
bject
);
var
validator
=
new
InstanceValidator
(
this
,
o
ptions
);
return
validator
.
hookValidate
();
return
validator
.
hookValidate
();
};
};
...
...
lib/model.js
View file @
4e57b88
...
@@ -1033,7 +1033,9 @@ module.exports = (function() {
...
@@ -1033,7 +1033,9 @@ module.exports = (function() {
var
build
=
self
.
build
(
params
);
var
build
=
self
.
build
(
params
);
return
build
.
hookValidate
({
skip
:
Object
.
keys
(
params
)}).
then
(
function
()
{
options
.
skip
=
Object
.
keys
(
params
);
return
build
.
hookValidate
(
options
).
then
(
function
()
{
delete
options
.
skip
;
return
Promise
.
resolve
([
build
,
true
]);
return
Promise
.
resolve
([
build
,
true
]);
});
});
}
}
...
@@ -1193,17 +1195,18 @@ module.exports = (function() {
...
@@ -1193,17 +1195,18 @@ module.exports = (function() {
// Validate
// Validate
if
(
options
.
validate
)
{
if
(
options
.
validate
)
{
var
skippedFields
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
options
.
fields
);
options
.
skip
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
options
.
fields
);
var
errors
=
[];
var
errors
=
[];
return
Promise
.
map
(
daos
,
function
(
dao
)
{
return
Promise
.
map
(
daos
,
function
(
dao
)
{
var
fn
=
options
.
individualHooks
?
'hookValidate'
:
'validate'
;
var
fn
=
options
.
individualHooks
?
'hookValidate'
:
'validate'
;
return
dao
[
fn
](
{
skip
:
skippedFields
}
).
then
(
function
(
err
)
{
return
dao
[
fn
](
options
).
then
(
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
errors
.
push
({
record
:
dao
,
errors
:
err
});
errors
.
push
({
record
:
dao
,
errors
:
err
});
}
}
});
});
}).
then
(
function
()
{
}).
then
(
function
()
{
delete
options
.
skip
;
if
(
errors
.
length
)
{
if
(
errors
.
length
)
{
return
Promise
.
reject
(
errors
);
return
Promise
.
reject
(
errors
);
}
}
...
@@ -1384,8 +1387,9 @@ module.exports = (function() {
...
@@ -1384,8 +1387,9 @@ module.exports = (function() {
build
.
set
(
self
.
_timestampAttributes
.
updatedAt
,
attrValueHash
[
self
.
_timestampAttributes
.
updatedAt
],
{
raw
:
true
});
build
.
set
(
self
.
_timestampAttributes
.
updatedAt
,
attrValueHash
[
self
.
_timestampAttributes
.
updatedAt
],
{
raw
:
true
});
// We want to skip validations for all other fields
// We want to skip validations for all other fields
var
skippedFields
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
Object
.
keys
(
attrValueHash
));
options
.
skip
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
Object
.
keys
(
attrValueHash
));
return
build
.
hookValidate
({
skip
:
skippedFields
}).
then
(
function
(
attributes
)
{
return
build
.
hookValidate
(
options
).
then
(
function
(
attributes
)
{
delete
options
.
skip
;
if
(
attributes
&&
attributes
.
dataValues
)
{
if
(
attributes
&&
attributes
.
dataValues
)
{
attrValueHash
=
Utils
.
_
.
pick
(
attributes
.
dataValues
,
Object
.
keys
(
attrValueHash
));
attrValueHash
=
Utils
.
_
.
pick
(
attributes
.
dataValues
,
Object
.
keys
(
attrValueHash
));
}
}
...
...
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