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 1b552994
authored
May 24, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #640 from tremby/model-validations
Add model validations option
2 parents
42b68bfc
39c0b628
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
3 deletions
README.md
lib/dao-factory.js
lib/dao.js
spec/dao-factory.spec.js
spec/dao.validations.spec.js
README.md
View file @
1b55299
...
@@ -78,6 +78,9 @@ A very basic roadmap. Chances aren't too bad, that not mentioned things are impl
...
@@ -78,6 +78,9 @@ A very basic roadmap. Chances aren't too bad, that not mentioned things are impl
-
encapsulate attributes if a dao inside the attributes property
-
encapsulate attributes if a dao inside the attributes property
-
~~add getters and setters for dao~~ Implemented in
[
#538
](
https://github.com/sequelize/sequelize/pull/538
)
, thanks to iamjochem
-
~~add getters and setters for dao~~ Implemented in
[
#538
](
https://github.com/sequelize/sequelize/pull/538
)
, thanks to iamjochem
-
add proper error message everywhere
-
add proper error message everywhere
-
refactor validate() output data structure, separating field-specific errors
from general model validator errors (i.e.
`{fields: {field1: ['field1error1']}, model: ['modelError1']}`
or similar)
## Collaboration 2.0 ##
## Collaboration 2.0 ##
...
...
lib/dao-factory.js
View file @
1b55299
...
@@ -21,6 +21,14 @@ module.exports = (function() {
...
@@ -21,6 +21,14 @@ module.exports = (function() {
schemaDelimiter
:
''
schemaDelimiter
:
''
},
options
||
{})
},
options
||
{})
// error check options
Utils
.
_
.
each
(
options
.
validate
,
function
(
validator
,
validatorType
)
{
if
(
Utils
.
_
.
contains
(
Utils
.
_
.
keys
(
attributes
),
validatorType
))
throw
new
Error
(
"A model validator function must not have the same name as a field. Model: "
+
name
+
", field/validation name: "
+
validatorType
)
if
(
!
Utils
.
_
.
isFunction
(
validator
))
throw
new
Error
(
"Members of the validate option must be functions. Model: "
+
name
+
", error with validate member "
+
validatorType
)
})
this
.
name
=
name
this
.
name
=
name
if
(
!
this
.
options
.
tableName
)
{
if
(
!
this
.
options
.
tableName
)
{
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
...
@@ -30,9 +38,6 @@ module.exports = (function() {
...
@@ -30,9 +38,6 @@ module.exports = (function() {
this
.
rawAttributes
=
attributes
this
.
rawAttributes
=
attributes
this
.
daoFactoryManager
=
null
// defined in init function
this
.
daoFactoryManager
=
null
// defined in init function
this
.
associations
=
{}
this
.
associations
=
{}
// extract validation
this
.
validate
=
this
.
options
.
validate
||
{}
}
}
Object
.
defineProperty
(
DAOFactory
.
prototype
,
'attributes'
,
{
Object
.
defineProperty
(
DAOFactory
.
prototype
,
'attributes'
,
{
...
...
lib/dao.js
View file @
1b55299
...
@@ -269,6 +269,15 @@ module.exports = (function() {
...
@@ -269,6 +269,15 @@ module.exports = (function() {
}
// if field has validator set
}
// if field has validator set
})
// for each field
})
// for each field
// for each model validator for this DAO
Utils
.
_
.
each
(
self
.
__options
.
validate
,
function
(
validator
,
validatorType
)
{
try
{
validator
.
apply
(
self
)
}
catch
(
err
)
{
failures
[
validatorType
]
=
[
err
.
message
]
// TODO: data structure needs to change for 2.0
}
})
return
(
Utils
.
_
.
isEmpty
(
failures
)
?
null
:
failures
)
return
(
Utils
.
_
.
isEmpty
(
failures
)
?
null
:
failures
)
}
}
...
...
spec/dao-factory.spec.js
View file @
1b55299
...
@@ -70,6 +70,34 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -70,6 +70,34 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
}.
bind
(
this
),
'Invalid DAO definition. Only one autoincrement field allowed.'
)
}.
bind
(
this
),
'Invalid DAO definition. Only one autoincrement field allowed.'
)
})
})
it
(
'throws an error if a custom model-wide validation is not a function'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
sequelize
.
define
(
'Foo'
,
{
field
:
{
type
:
Sequelize
.
INTEGER
}
},
{
validate
:
{
notFunction
:
33
}
})
}.
bind
(
this
),
'Members of the validate option must be functions. Model: Foo, error with validate member notFunction'
)
})
it
(
'throws an error if a custom model-wide validation has the same name as a field'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
sequelize
.
define
(
'Foo'
,
{
field
:
{
type
:
Sequelize
.
INTEGER
}
},
{
validate
:
{
field
:
function
()
{}
}
})
}.
bind
(
this
),
'A model validator function must not have the same name as a field. Model: Foo, field/validation name: field'
)
})
})
})
describe
(
'build'
,
function
()
{
describe
(
'build'
,
function
()
{
...
...
spec/dao.validations.spec.js
View file @
1b55299
...
@@ -306,5 +306,34 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -306,5 +306,34 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
var
successfulUser2
=
User
.
build
({
age
:
1
})
var
successfulUser2
=
User
.
build
({
age
:
1
})
expect
(
successfulUser2
.
validate
()).
toBeNull
()
expect
(
successfulUser2
.
validate
()).
toBeNull
()
})
})
it
(
'validates a model with custom model-wide validation methods'
,
function
()
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
Math
.
random
(),
{
field1
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
true
},
field2
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
true
}
},
{
validate
:
{
xnor
:
function
()
{
if
((
this
.
field1
===
null
)
===
(
this
.
field2
===
null
))
{
throw
new
Error
(
'xnor failed'
);
}
}
}
})
var
failingFoo
=
Foo
.
build
({
field1
:
null
,
field2
:
null
})
,
errors
=
failingFoo
.
validate
()
expect
(
errors
).
not
.
toBeNull
()
expect
(
errors
).
toEqual
({
'xnor'
:
[
'xnor failed'
]
})
var
successfulFoo
=
Foo
.
build
({
field1
:
33
,
field2
:
null
})
expect
(
successfulFoo
.
validate
()).
toBeNull
()
})
})
})
})
})
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