Permalink
Browse files

add: ename field

  • Loading branch information...
1 parent 5c58c01 commit c13ad06ddb92d230c6a1175ee6ce2b29e8cbdbf7 @mah0x211 committed Jul 14, 2011
Showing with 40 additions and 25 deletions.
  1. +39 −24 lib/auditor.js
  2. +1 −1 package.json
View
@@ -35,6 +35,33 @@ var TypeRule = /^(text|email(_loose)?|url|date|(un)?signed)$/,
PRECONDITION_FAILED: 412
};
+function setStatus( obj, status )
+{
+ switch( status )
+ {
+ case STATUS.NO_CONTENT:
+ obj.errno = status;
+ obj.ename = 'NO_CONTENT';
+ obj.errstr = 'field val undefined';
+ break;
+ case STATUS.BAD_REQUEST:
+ obj.errno = status;
+ obj.ename = 'BAD_REQUEST';
+ obj.errstr = 'invalid field type';
+ break;
+ case STATUS.NOT_ACCEPTABLE:
+ obj.errno = status;
+ obj.ename = 'NOT_ACCEPTABLE';
+ obj.errstr = 'invalid field val';
+ break;
+ case STATUS.PRECONDITION_FAILED:
+ obj.errno = status;
+ obj.ename = 'PRECONDITION_FAILED';
+ obj.errstr = 'unknown field type';
+ break;
+ }
+}
+
function Init()
{
// EMAIL REGEXP
@@ -289,14 +316,12 @@ auditor.prototype.check = function( label, field, val )
// unknown field type
// PRECONDITION_FAILED
if( !TypeRule.test( rule.type ) ){
- result.errno = STATUS.PRECONDITION_FAILED;
- result.errstr = 'unknown field type';
+ setStatus( result, STATUS.PRECONDITION_FAILED );
}
// check defined
// 204 NO_CONTENT
else if( tmp === undefined || tmp === null || ( isString( tmp ) && tmp.length === 0 ) ){
- result.errno = STATUS.NO_CONTENT;
- result.errstr = 'field val undefined';
+ setStatus( result, STATUS.NO_CONTENT );
}
// check text
else if( result.type === 'text' )
@@ -306,23 +331,20 @@ auditor.prototype.check = function( label, field, val )
}
// 400 BAD_REQUEST
if( !isString( tmp ) ){
- result.errno = STATUS.BAD_REQUEST;
- result.errstr = 'invalid field type';
+ setStatus( result, STATUS.BAD_REQUEST );
}
// check length
else if( !tmp.length ){
// 204 NO_CONTENT
- result.errno = STATUS.NO_CONTENT;
- result.errstr = 'field val undefined';
+ setStatus( result, STATUS.NO_CONTENT );
}
// check fix/min/max/match
else if( ( isNumber( rule.fix ) && tmp.length !== rule.fix ) ||
( isNumber( rule.min ) && tmp.length < rule.min ) ||
( isNumber( rule.max ) && tmp.length > rule.max ) ||
( isRegExp( rule.match ) && !rule.test( tmp ) ) ){
// 406 NOT_ACCEPTABLE
- result.errno = STATUS.NOT_ACCEPTABLE;
- result.errstr = 'invalid field val';
+ setStatus( result, STATUS.NOT_ACCEPTABLE );
}
else {
result.val = tmp;
@@ -334,26 +356,23 @@ auditor.prototype.check = function( label, field, val )
{
// 406 NOT_ACCEPTABLE
if( !isEmail( tmp ) ){
- result.errno = STATUS.NOT_ACCEPTABLE;
- result.errstr = 'invalid field val';
+ setStatus( result, STATUS.NOT_ACCEPTABLE );
}
}
// check email_loose
else if( result.type === 'email_loose' )
{
// 406 NOT_ACCEPTABLE
if( !isEmailLoose( tmp ) ){
- result.errno = STATUS.NOT_ACCEPTABLE;
- result.errstr = 'invalid field val';
+ setStatus( result, STATUS.NOT_ACCEPTABLE );
}
}
// check url
else if( result.type === 'url' )
{
// 406 NOT_ACCEPTABLE
if( !isURL( tmp ) ){
- result.errno = STATUS.NOT_ACCEPTABLE;
- result.errstr = 'invalid field val';
+ setStatus( result, STATUS.NOT_ACCEPTABLE );
}
}
// check number
@@ -365,8 +384,7 @@ auditor.prototype.check = function( label, field, val )
// 400 BAD_REQUEST
if( !isNumber( tmp ) || isNaN( tmp ) ){
- result.errno = STATUS.BAD_REQUEST;
- result.errstr = 'invalid field type';
+ setStatus( result, STATUS.BAD_REQUEST );
}
// check fix/min/max/match
else if( ( result.type === 'unsigned' && tmp < 0 ) ||
@@ -375,8 +393,7 @@ auditor.prototype.check = function( label, field, val )
( isNumber( rule.max ) && tmp > rule.max ) ||
( isRegExp( rule.match ) && !rule.match.test( tmp ) ) ){
// 406 NOT_ACCEPTABLE
- result.errno = STATUS.NOT_ACCEPTABLE;
- result.errstr = 'invalid field val';
+ setStatus( result, STATUS.NOT_ACCEPTABLE );
}
else {
result.val = tmp;
@@ -386,16 +403,14 @@ auditor.prototype.check = function( label, field, val )
else if( result.type === 'date' )
{
if( !isString( tmp ) ){
- result.errno = STATUS.BAD_REQUEST;
- result.errstr = 'invalid field type';
+ setStatus( result, STATUS.BAD_REQUEST );
}
else
{
tmp = Date.parse( tmp );
// 400 BAD_REQUEST
if( !isNumber( tmp ) || isNaN( tmp ) ){
- result.errno = STATUS.BAD_REQUEST;
- result.errstr = 'invalid field type';
+ setStatus( result, STATUS.BAD_REQUEST );
}
}
}
View
@@ -6,7 +6,7 @@
],
"description": "",
"homepage": "http://github.com/mah0x211/node-auditor",
- "version" : "0.1.3",
+ "version" : "0.1.4",
"licenses" : [
{
"type": "MIT"

0 comments on commit c13ad06

Please sign in to comment.