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 735b2a48
authored
Jul 14, 2018
by
Pavel Kabakin
Committed by
Sushant
Jul 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(validation): improve validation for type(#9660)
1 parent
31113cec
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
95 additions
and
9 deletions
lib/dialects/abstract/query-generator.js
lib/instance-validator.js
test/integration/instance.validations.test.js
test/unit/model/validation.test.js
lib/dialects/abstract/query-generator.js
View file @
735b2a4
...
...
@@ -16,6 +16,7 @@ const BelongsToMany = require('../../associations/belongs-to-many');
const
HasMany
=
require
(
'../../associations/has-many'
);
const
QueryTypes
=
require
(
'../../query-types'
);
const
Op
=
require
(
'../../operators'
);
const
sequelizeError
=
require
(
'../../errors'
);
const
QuoteHelper
=
require
(
'./query-generator/helpers/quote'
);
...
...
@@ -1031,12 +1032,27 @@ class QueryGenerator {
*/
validate
(
value
,
field
,
options
)
{
if
(
this
.
typeValidation
&&
field
.
type
.
validate
&&
value
)
{
if
(
options
.
isList
&&
Array
.
isArray
(
value
))
{
for
(
const
item
of
value
)
{
field
.
type
.
validate
(
item
,
options
);
try
{
if
(
options
.
isList
&&
Array
.
isArray
(
value
))
{
for
(
const
item
of
value
)
{
field
.
type
.
validate
(
item
,
options
);
}
}
else
{
field
.
type
.
validate
(
value
,
options
);
}
}
else
{
field
.
type
.
validate
(
value
,
options
);
}
catch
(
error
)
{
if
(
error
instanceof
sequelizeError
.
ValidationError
)
{
error
.
errors
.
push
(
new
sequelizeError
.
ValidationErrorItem
(
error
.
message
,
'Validation error'
,
field
.
fieldName
,
value
,
null
,
`
${
field
.
type
.
key
}
validator`
));
}
throw
error
;
}
}
}
...
...
lib/instance-validator.js
View file @
735b2a4
...
...
@@ -132,6 +132,10 @@ class InstanceValidator {
const
value
=
this
.
modelInstance
.
dataValues
[
field
];
if
(
value
instanceof
Utils
.
SequelizeMethod
)
{
return
;
}
if
(
!
rawAttribute
.
_autoGenerated
&&
!
rawAttribute
.
autoIncrement
)
{
// perform validations based on schema
this
.
_validateSchema
(
rawAttribute
,
field
,
value
);
...
...
test/integration/instance.validations.test.js
View file @
735b2a4
...
...
@@ -546,6 +546,24 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
return
expect
(
failingBar
.
validate
({
skip
:
[
'field'
]
})).
not
.
to
.
be
.
rejected
;
});
it
(
'skips validations for fields with value that is SequelizeMethod'
,
function
()
{
const
values
=
[
'value1'
,
'value2'
];
const
Bar
=
this
.
sequelize
.
define
(
'Bar'
+
config
.
rand
(),
{
field
:
{
type
:
Sequelize
.
ENUM
,
values
,
validate
:
{
isIn
:
[
values
]
}
}
});
const
failingBar
=
Bar
.
build
({
field
:
this
.
sequelize
.
literal
(
'5 + 1'
)
});
return
expect
(
failingBar
.
validate
()).
not
.
to
.
be
.
rejected
;
});
it
(
'raises an error if saving a different value into an immutable field'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
...
...
test/unit/model/validation.test.js
View file @
735b2a4
...
...
@@ -382,13 +382,37 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
it
(
'should throw when passing string'
,
()
=>
{
return
expect
(
User
.
create
({
age
:
'jan'
})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
);
})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
)
.
which
.
eventually
.
have
.
property
(
'errors'
)
.
that
.
is
.
an
(
'array'
)
.
with
.
lengthOf
(
1
)
.
and
.
with
.
property
(
0
)
.
that
.
is
.
an
.
instanceOf
(
current
.
ValidationErrorItem
)
.
and
.
include
({
type
:
'Validation error'
,
path
:
'age'
,
value
:
'jan'
,
instance
:
null
,
validatorKey
:
'INTEGER validator'
});
});
it
(
'should throw when passing decimal'
,
()
=>
{
return
expect
(
User
.
create
({
age
:
4.5
})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
);
})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
)
.
which
.
eventually
.
have
.
property
(
'errors'
)
.
that
.
is
.
an
(
'array'
)
.
with
.
lengthOf
(
1
)
.
and
.
with
.
property
(
0
)
.
that
.
is
.
an
.
instanceOf
(
current
.
ValidationErrorItem
)
.
and
.
include
({
type
:
'Validation error'
,
path
:
'age'
,
value
:
4.5
,
instance
:
null
,
validatorKey
:
'INTEGER validator'
});
});
});
...
...
@@ -396,13 +420,37 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
it
(
'should throw when passing string'
,
()
=>
{
return
expect
(
User
.
update
({
age
:
'jan'
},
{
where
:
{}})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
);
},
{
where
:
{}})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
)
.
which
.
eventually
.
have
.
property
(
'errors'
)
.
that
.
is
.
an
(
'array'
)
.
with
.
lengthOf
(
1
)
.
and
.
with
.
property
(
0
)
.
that
.
is
.
an
.
instanceOf
(
current
.
ValidationErrorItem
)
.
and
.
include
({
type
:
'Validation error'
,
path
:
'age'
,
value
:
'jan'
,
instance
:
null
,
validatorKey
:
'INTEGER validator'
});
});
it
(
'should throw when passing decimal'
,
()
=>
{
return
expect
(
User
.
update
({
age
:
4.5
},
{
where
:
{}})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
);
},
{
where
:
{}})).
to
.
be
.
rejectedWith
(
current
.
ValidationError
)
.
which
.
eventually
.
have
.
property
(
'errors'
)
.
that
.
is
.
an
(
'array'
)
.
with
.
lengthOf
(
1
)
.
and
.
with
.
property
(
0
)
.
that
.
is
.
an
.
instanceOf
(
current
.
ValidationErrorItem
)
.
and
.
include
({
type
:
'Validation error'
,
path
:
'age'
,
value
:
4.5
,
instance
:
null
,
validatorKey
:
'INTEGER validator'
});
});
});
...
...
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