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 2bf4ea51
authored
Nov 09, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
parameter validation
1 parent
3e92cb5a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
1 deletions
lib/utils.js
lib/utils/parameter-validator.js
package.json
test/utils.test.js
lib/utils.js
View file @
2bf4ea5
...
@@ -3,6 +3,7 @@ var util = require("util")
...
@@ -3,6 +3,7 @@ var util = require("util")
,
SqlString
=
require
(
"./sql-string"
)
,
SqlString
=
require
(
"./sql-string"
)
,
lodash
=
require
(
"lodash"
)
,
lodash
=
require
(
"lodash"
)
,
_string
=
require
(
'underscore.string'
)
,
_string
=
require
(
'underscore.string'
)
,
ParameterValidator
=
require
(
'./utils/parameter-validator'
)
var
Utils
=
module
.
exports
=
{
var
Utils
=
module
.
exports
=
{
_
:
(
function
()
{
_
:
(
function
()
{
...
@@ -535,6 +536,10 @@ var Utils = module.exports = {
...
@@ -535,6 +536,10 @@ var Utils = module.exports = {
var
r
=
Math
.
random
()
*
16
|
0
,
v
=
c
==
'x'
?
r
:
(
r
&
0x3
|
0x8
)
var
r
=
Math
.
random
()
*
16
|
0
,
v
=
c
==
'x'
?
r
:
(
r
&
0x3
|
0x8
)
return
v
.
toString
(
16
)
return
v
.
toString
(
16
)
})
})
},
validateParameter
:
function
(
value
,
expectation
,
options
)
{
return
ParameterValidator
.
check
(
value
,
expectation
,
options
)
}
}
}
}
...
...
lib/utils/parameter-validator.js
0 → 100644
View file @
2bf4ea5
var
ParameterValidator
=
module
.
exports
=
{
check
:
function
(
value
,
expectation
,
options
)
{
options
=
Utils
.
_
.
extend
({
throwError
:
true
,
deprecated
:
false
,
onDeprecated
:
console
.
log
},
options
||
{})
if
(
value
===
undefined
)
{
throw
new
Error
(
'No value has been passed.'
)
}
if
(
expectation
===
undefined
)
{
throw
new
Error
(
'No expectation has been passed.'
)
}
validateDeprication
(
value
,
expectation
,
options
)
return
validate
(
value
,
expectation
,
options
)
}
}
var
matchesExpectation
=
function
(
value
,
expectation
)
{
if
(
typeof
expectation
===
'string'
)
{
return
(
typeof
value
===
expectation
.
toString
())
}
else
{
return
(
value
instanceof
expectation
)
}
}
var
validateDeprication
=
function
(
value
,
expectation
,
options
)
{
if
(
options
.
deprecated
)
{
if
(
matchesExpectation
(
value
,
options
.
deprecated
))
{
options
.
onDeprecated
(
"Deprecated!"
)
}
}
}
var
validate
=
function
(
value
,
expectation
,
options
)
{
var
result
=
matchesExpectation
(
value
,
expectation
)
if
(
result
)
{
return
result
}
else
if
(
!
options
.
throwError
)
{
return
false
}
else
{
throw
new
Error
(
'The parameter (value: '
+
value
.
toString
()
+
') is no '
+
expectation
+
'.'
)
}
}
package.json
View file @
2bf4ea5
...
@@ -58,7 +58,8 @@
...
@@ -58,7 +58,8 @@
"mocha"
:
"~1.13.0"
,
"mocha"
:
"~1.13.0"
,
"chai-datetime"
:
"~1.1.1"
,
"chai-datetime"
:
"~1.1.1"
,
"sinon"
:
"~1.7.3"
,
"sinon"
:
"~1.7.3"
,
"mariasql"
:
"git://github.com/sequelize/node-mariasql.git"
"mariasql"
:
"git://github.com/sequelize/node-mariasql.git"
,
"chai-spies"
:
"~0.5.1"
},
},
"keywords"
:
[
"keywords"
:
[
"mysql"
,
"mysql"
,
...
...
test/utils.test.js
View file @
2bf4ea5
var
chai
=
require
(
'chai'
)
var
chai
=
require
(
'chai'
)
,
spies
=
require
(
'chai-spies'
)
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
,
Utils
=
require
(
__dirname
+
'/../lib/utils'
)
,
Utils
=
require
(
__dirname
+
'/../lib/utils'
)
,
Support
=
require
(
__dirname
+
'/support'
)
,
Support
=
require
(
__dirname
+
'/support'
)
chai
.
use
(
spies
)
chai
.
Assertion
.
includeStack
=
true
chai
.
Assertion
.
includeStack
=
true
describe
(
Support
.
getTestDialectTeaser
(
"Utils"
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
"Utils"
),
function
()
{
...
@@ -158,4 +160,53 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
...
@@ -158,4 +160,53 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
done
()
done
()
})
})
})
})
describe
(
'validateParameter'
,
function
()
{
describe
(
'method signature'
,
function
()
{
it
(
'throws an error if the value is not defined'
,
function
()
{
expect
(
function
()
{
Utils
.
validateParameter
()
}).
to
.
throw
(
'No value has been passed.'
)
})
it
(
'throws an error if the expectation is not defined'
,
function
()
{
expect
(
function
()
{
Utils
.
validateParameter
(
1
)
}).
to
.
throw
(
'No expectation has been passed.'
)
})
})
describe
(
'expectation'
,
function
()
{
it
(
'uses the typeof method if the expectation is a string'
,
function
()
{
expect
(
Utils
.
validateParameter
(
1
,
'number'
)).
to
.
be
.
true
})
it
(
'uses the instanceof method if the expectation is a class'
,
function
()
{
expect
(
Utils
.
validateParameter
(
new
Number
(
1
),
Number
)).
to
.
be
.
true
})
})
describe
(
'failing expectations'
,
function
()
{
it
(
'throws an error if the expectation does not match'
,
function
()
{
expect
(
function
()
{
Utils
.
validateParameter
(
1
,
String
)
}).
to
.
throw
(
/The parameter.*is no.*/
)
})
it
(
'does not throw an error if throwError is false'
,
function
()
{
expect
(
Utils
.
validateParameter
(
1
,
String
,
{
throwError
:
false
})).
to
.
be
.
false
})
})
describe
(
'deprecation warning'
,
function
()
{
it
(
'uses the passed function'
,
function
()
{
var
spy
=
chai
.
spy
(
function
(
s
){})
Utils
.
validateParameter
([],
Object
,
{
deprecated
:
Array
,
onDeprecated
:
spy
})
expect
(
spy
).
to
.
have
.
been
.
called
()
})
})
})
})
})
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