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 921f6955
authored
Mar 11, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge pull request #1374 from tonytamps:feature/immutable-validation
1 parent
f449abc9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
2 deletions
lib/dao-validator.js
test/dao.validations.test.js
lib/dao-validator.js
View file @
921f695
...
...
@@ -78,6 +78,13 @@ Validator.extend('is', function(str, pattern, modifiers) {
return
this
.
regex
(
str
,
pattern
,
modifiers
);
})
function
extendModelValidations
(
modelInstance
){
Validator
.
extend
(
'isImmutable'
,
function
(
str
,
param
,
field
)
{
return
(
modelInstance
.
isNewRecord
||
modelInstance
.
dataValues
[
field
]
===
modelInstance
.
_previousDataValues
[
field
]);
})
}
/**
* The Main DAO Validator.
*
...
...
@@ -112,6 +119,8 @@ var DaoValidator = module.exports = function(modelInstance, options) {
/** @type {boolean} Indicates if validations are in progress */
this
.
inProgress
=
false
;
extendModelValidations
(
modelInstance
);
}
/** @define {string} The error key for arguments as passed by custom validators */
...
...
@@ -259,7 +268,7 @@ DaoValidator.prototype._builtinAttrValidate = function(value, field) {
true
,
value
,
field
))
}
var
validatorPromise
=
self
.
_invokeBuiltinValidator
(
value
,
test
,
validatorType
);
var
validatorPromise
=
self
.
_invokeBuiltinValidator
(
value
,
test
,
validatorType
,
field
);
// errors are handled in settling, stub this
validatorPromise
.
catch
(
noop
)
validators
.
push
(
validatorPromise
)
...
...
@@ -320,11 +329,12 @@ DaoValidator.prototype._invokeCustomValidator = Promise.method(function(validato
* @param {*} value Anything.
* @param {*} test The test case.
* @param {string} validatorType One of known to Sequelize validators.
* @param {string} field The field that is being validated
* @return {Object} An object with specific keys to invoke the validator.
* @private
*/
DaoValidator
.
prototype
.
_invokeBuiltinValidator
=
Promise
.
method
(
function
(
value
,
test
,
validatorType
)
{
test
,
validatorType
,
field
)
{
// check if Validator knows that kind of validation test
if
(
typeof
Validator
[
validatorType
]
!==
'function'
)
{
...
...
@@ -343,6 +353,7 @@ DaoValidator.prototype._invokeBuiltinValidator = Promise.method(function(value,
}
else
{
validatorArgs
=
validatorArgs
.
slice
(
0
);
}
validatorArgs
.
push
(
field
)
if
(
!
Validator
[
validatorType
].
apply
(
Validator
,
[
value
].
concat
(
validatorArgs
)))
{
throw
errorMessage
}
...
...
test/dao.validations.test.js
View file @
921f695
...
...
@@ -718,5 +718,50 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
done
()
})
})
it
(
"raises an error if saving a different value into an immutable field"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
isImmutable
:
true
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
"RedCat"
}).
success
(
function
(
user
){
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
)
user
.
setDataValue
(
'name'
,
'YellowCat'
)
user
.
save
()
.
done
(
function
(
errors
){
expect
(
errors
).
to
.
not
.
be
.
null
expect
(
errors
).
to
.
be
.
an
.
instanceOf
(
Error
)
expect
(
errors
.
name
[
0
].
message
).
to
.
eql
(
'Validation isImmutable failed'
)
done
()
})
})
})
})
it
(
"allows setting an immutable field if the record is unsaved"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
isImmutable
:
true
}
}
})
var
user
=
User
.
build
({
name
:
"RedCat"
})
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
)
user
.
setDataValue
(
'name'
,
'YellowCat'
)
user
.
validate
().
done
(
function
(
errors
){
expect
(
errors
).
to
.
be
.
null
done
()
})
})
})
})
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