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 71cf6777
authored
Mar 10, 2014
by
Thanasis Polychronakis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
deprecate notNull built-in validator
1 parent
5d219eaf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
49 deletions
lib/dao-validator.js
test/dao-factory/create.test.js
test/dao.validations.test.js
lib/dao-validator.js
View file @
71cf677
...
...
@@ -5,10 +5,9 @@ var Validator = require("validator")
function
noop
()
{}
// Backwards compat for people using old validation function
// We cannot use .extend, since it coerces the first arg to string
Validator
.
notNull
=
function
(
val
)
{
return
[
null
,
undefined
].
indexOf
(
val
)
===
-
1
// Deprecate this.
Validator
.
notNull
=
function
()
{
throw
new
Error
(
'Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"'
);
}
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
...
...
@@ -137,9 +136,9 @@ DaoValidator.prototype.validate = function() {
self
.
_customValidators
(),
]).
then
(
function
()
{
if
(
Object
.
keys
(
self
.
errors
).
length
)
{
emitter
.
emit
(
'success'
,
self
.
errors
)
emitter
.
emit
(
'success'
,
self
.
errors
)
}
else
{
emitter
.
emit
(
'success'
)
emitter
.
emit
(
'success'
)
}
})
}).
run
()
...
...
@@ -245,8 +244,7 @@ DaoValidator.prototype._customValidators = function() {
DaoValidator
.
prototype
.
_builtinAttrValidate
=
function
(
value
,
field
)
{
var
self
=
this
;
// check if value is null (if null not allowed the Schema pass will capture it)
if
(
!
(
'notNull'
in
this
.
modelInstance
.
validators
[
field
])
&&
(
value
===
null
||
typeof
value
===
'undefined'
))
{
if
(
value
===
null
||
typeof
value
===
'undefined'
)
{
return
Promise
.
resolve
();
}
...
...
test/dao-factory/create.test.js
View file @
71cf677
...
...
@@ -376,42 +376,42 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
UserNull
.
create
({
username
:
'foo2'
,
smth
:
null
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
expect
(
err
.
smth
[
0
].
path
).
to
.
equal
(
'smth'
);
expect
(
err
.
smth
[
0
].
path
).
to
.
equal
(
'smth'
);
if
(
Support
.
dialectIsMySQL
())
{
// We need to allow two different errors for MySQL, see:
// http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_trans_tables
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
}
else
if
(
dialect
===
"sqlite"
)
{
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
}
else
{
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
expect
(
err
.
smth
[
0
].
message
).
to
.
match
(
/notNull Violation/
)
}
done
()
})
done
()
})
})
})
it
(
"raises an error if created object breaks definition contraints"
,
function
(
done
)
{
var
UserNull
=
this
.
sequelize
.
define
(
'UserWithNonNullSmth'
,
{
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
smth
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
}
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
smth
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
}
})
this
.
sequelize
.
options
.
omitNull
=
false
UserNull
.
sync
({
force
:
true
}).
success
(
function
()
{
UserNull
.
create
({
username
:
'foo'
,
smth
:
'foo'
}).
success
(
function
()
{
UserNull
.
create
({
username
:
'foo'
,
smth
:
'bar'
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
if
(
dialect
===
"sqlite"
)
{
expect
(
err
.
message
).
to
.
match
(
/.*SQLITE_CONSTRAINT.*/
)
}
else
if
(
Support
.
dialectIsMySQL
())
{
expect
(
err
.
message
).
to
.
match
(
/Duplicate entry 'foo' for key 'username'/
)
}
else
{
expect
(
err
.
message
).
to
.
match
(
/.*duplicate key value violates unique constraint.*/
)
}
done
()
UserNull
.
create
({
username
:
'foo'
,
smth
:
'foo'
}).
success
(
function
()
{
UserNull
.
create
({
username
:
'foo'
,
smth
:
'bar'
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
if
(
dialect
===
"sqlite"
)
{
expect
(
err
.
message
).
to
.
match
(
/.*SQLITE_CONSTRAINT.*/
)
}
else
if
(
Support
.
dialectIsMySQL
())
{
expect
(
err
.
message
).
to
.
match
(
/Duplicate entry 'foo' for key 'username'/
)
}
else
{
expect
(
err
.
message
).
to
.
match
(
/.*duplicate key value violates unique constraint.*/
)
}
done
()
})
})
})
...
...
@@ -431,12 +431,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
str2
.
str
).
to
.
equal
(
'http://sequelizejs.org'
)
StringIsNullOrUrl
.
create
({
str
:
''
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
expect
(
err
.
str
[
0
].
message
).
to
.
match
(
/Validation isURL failed/
)
expect
(
err
.
str
[
0
].
message
).
to
.
match
(
/Validation isURL failed/
)
done
()
})
})
}).
error
(
done
)
}).
error
(
done
)
})
})
...
...
@@ -952,9 +952,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var
Tasks
=
this
.
sequelize
.
define
(
'Task'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
notNull
:
{
args
:
true
,
msg
:
'name cannot be null'
}
}
allowNull
:
false
,
},
code
:
{
type
:
Sequelize
.
STRING
,
...
...
@@ -968,16 +966,16 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
Tasks
.
bulkCreate
([
{
name
:
'foo'
,
code
:
'123'
},
{
code
:
'1234'
},
{
name
:
'bar'
,
code
:
'1'
}
],
{
validate
:
true
}).
error
(
function
(
errors
)
{
expect
(
errors
).
to
.
not
.
be
.
null
expect
(
errors
).
to
.
be
.
an
(
'Array'
)
expect
(
errors
).
to
.
have
.
length
(
2
)
expect
(
errors
[
0
].
record
.
code
).
to
.
equal
(
'1234'
)
expect
(
errors
[
0
].
errors
.
name
[
0
].
message
).
to
.
equal
(
'name cannot be null
'
)
{
name
:
'bar'
,
code
:
'1'
}
],
{
validate
:
true
}).
error
(
function
(
errors
)
{
expect
(
errors
).
to
.
not
.
be
.
null
expect
(
errors
).
to
.
be
.
an
(
'Array'
)
expect
(
errors
).
to
.
have
.
length
(
2
)
expect
(
errors
[
0
].
record
.
code
).
to
.
equal
(
'1234'
)
expect
(
errors
[
0
].
errors
.
name
[
0
].
message
).
to
.
equal
(
'notNull Violation
'
)
expect
(
errors
[
1
].
record
.
name
).
to
.
equal
(
'bar'
)
expect
(
errors
[
1
].
record
.
code
).
to
.
equal
(
'1'
)
expect
(
errors
[
1
].
errors
.
code
[
0
].
message
).
to
.
equal
(
'Validation len failed'
)
expect
(
errors
[
1
].
errors
.
code
[
0
].
message
).
to
.
equal
(
'Validation len failed'
)
done
()
})
})
...
...
@@ -988,8 +986,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
not
Null
:
{
args
:
true
,
msg
:
'name cannot be null'
}
}
not
Empty
:
true
,
}
,
},
code
:
{
type
:
Sequelize
.
STRING
,
...
...
test/dao.validations.test.js
View file @
71cf677
...
...
@@ -68,10 +68,6 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
fail
:
"a"
,
pass
:
"9.2"
}
,
notNull
:
{
fail
:
null
,
pass
:
0
}
,
isNull
:
{
fail
:
0
,
pass
:
null
...
...
@@ -291,8 +287,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
,
validate
:
{
notNull
:
true
,
// won't allow null
notEmpty
:
true
// don't allow empty strings
}
}
...
...
@@ -313,8 +309,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
,
validate
:
{
notNull
:
true
,
// won't allow null
notEmpty
:
true
// don't allow empty strings
}
}
...
...
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