Permalink
Browse files

add: rule bit

  • Loading branch information...
1 parent cc29fbe commit 786691e2c66403c5f8cc584b104293d72653439b @mah0x211 committed Jan 21, 2012
Showing with 49 additions and 24 deletions.
  1. +48 −23 lib/auditor.js
  2. +1 −1 package.json
View
71 lib/auditor.js
@@ -17,6 +17,7 @@
fix = [number]
min = [number]
max = [number]
+ bit = [array]
match = [regexp]
func = [function(val)]
}
@@ -28,7 +29,7 @@ var pkg = {
},
self = this,
TypeRule = /^(text|email(_loose)?|url|date|(un)?signed)$/,
- NumberRule = /^(fix|min|max)$/,
+ NumberRule = /^(fix|min|max|bit)$/,
// REGEXP
REGEXP_EMAIL = undefined,
REGEXP_EMAIL_LOOSE = undefined,
@@ -145,7 +146,7 @@ this.isBool = function( arg ){
return ( typeof arg === 'boolean' );
};
this.isNumber = function( arg ){
- return ( typeof arg === 'number' );
+ return ( typeof arg === 'number' && isFinite(arg) );
};
this.isString = function( arg ){
return ( typeof arg === 'string' );
@@ -177,16 +178,20 @@ this.isURL = function( arg, retVal )
if( self.isString( arg ) )
{
- var url = REGEXP_URL.exec( arg ),
- hostname = ( url[REGEXP_URL_PART.HOSTNAME] ) ?
- url[REGEXP_URL_PART.HOSTNAME].split('.') :
- [],
- port = +( url[REGEXP_URL_PART.PORT] || 0 );
+ var url = REGEXP_URL.exec( arg );
- if( url[REGEXP_URL_PART.SCHEME] &&
- hostname.length >= 2 &&
- port >= 0 && port <= 65535 ){
- valid = ( retVal ) ? url : arg;
+ if( url )
+ {
+ var hostname = ( url[REGEXP_URL_PART.HOSTNAME] ) ?
+ url[REGEXP_URL_PART.HOSTNAME].split('.') :
+ [],
+ port = +( url[REGEXP_URL_PART.PORT] || 0 );
+
+ if( url[REGEXP_URL_PART.SCHEME] &&
+ hostname.length >= 2 &&
+ port >= 0 && port <= 65535 ){
+ valid = ( retVal ) ? url : arg;
+ }
}
}
@@ -234,7 +239,13 @@ this.audit.prototype.addField = function( label, field, rule )
}
else
{
- var newRule = {};
+ var newRule = {},
+ invalidType = function( key ){
+ throw new TypeError( 'invalid rule: ' + key );
+ },
+ invalidVal = function( key, val ){
+ throw new TypeError( 'invalid rule['+p+'] value type: ' + typeof val );
+ };
if( !self.isObject( this.RuleSet[label] ) ){
this.RuleSet[label] = {};
@@ -246,7 +257,7 @@ this.audit.prototype.addField = function( label, field, rule )
if( p === 'type' )
{
if( !TypeRule.test( val ) ){
- throw new Error( 'invalid rule: ' + p );
+ invalidType( p );
}
else {
newRule[p] = val;
@@ -261,16 +272,16 @@ this.audit.prototype.addField = function( label, field, rule )
else if( p === 'func' )
{
if( !self.isFunction( val ) ){
- throw new Error( 'invalid rule: ' + p );
+ invalidVal( p, val );
}
else {
newRule[p] = val;
}
}
else if( p === 'match' )
{
- if( !self.isObject( val ) || !val.hasOwnProperty( 'pattern' ) ){
- throw new Error( 'invalid rule: ' + p );
+ if( !self.isRegExp( val ) ){
+ invalidVal( p, val );
}
else
{
@@ -282,13 +293,26 @@ this.audit.prototype.addField = function( label, field, rule )
}
}
}
- else if( NumberRule.test( p ) )
+ else if( p === 'bit' )
{
- if( self.isString( val ) ){
- val = +val;
+ if( !self.isArray( val ) ){
+ invalidVal( p, val );
+ }
+ else
+ {
+ newRule[p] = 0;
+ for( var i = 0, len = val.length; i < len; i++ )
+ {
+ if( val[i] ){
+ newRule[p] |= 1 << i;
+ }
+ }
}
- if( !self.isNumber( val ) || isNaN( val ) ){
- throw new Error( 'invalid rule: ' + p );
+ }
+ else if( NumberRule.test( p ) )
+ {
+ if( !self.isNumber( val ) ){
+ invalidVal( p, val );
}
else {
newRule[p] = val;
@@ -455,14 +479,15 @@ this.audit.prototype.check = function( label, field, val )
}
// 400 BAD_REQUEST
- if( !self.isNumber( tmp ) || isNaN( tmp ) ){
+ if( !self.isNumber( tmp ) ){
self.setStatus( result, STATUS.BAD_REQUEST );
}
// check fix/min/max/match
else if( ( result.type === 'unsigned' && tmp < 0 ) ||
( self.isNumber( rule.fix ) && tmp !== rule.fix ) ||
( self.isNumber( rule.min ) && tmp < rule.min ) ||
( self.isNumber( rule.max ) && tmp > rule.max ) ||
+ ( result.type === 'unsigned' && self.isNumber( rule.bit ) && !( rule.bit & ( 1 << tmp ) ) ) ||
( self.isRegExp( rule.match ) && !rule.match.test( tmp ) ) ){
// 406 NOT_ACCEPTABLE
self.setStatus( result, STATUS.NOT_ACCEPTABLE );
@@ -481,7 +506,7 @@ this.audit.prototype.check = function( label, field, val )
{
tmp = Date.parse( tmp );
// 400 BAD_REQUEST
- if( !self.isNumber( tmp ) || isNaN( tmp ) ){
+ if( !self.isNumber( tmp ) ){
self.setStatus( result, STATUS.BAD_REQUEST );
}
else {
View
2 package.json
@@ -6,7 +6,7 @@
],
"description": "",
"homepage": "http://github.com/mah0x211/node-auditor",
- "version" : "0.2.0",
+ "version" : "0.3.0",
"licenses" : [
{
"type": "MIT"

0 comments on commit 786691e

Please sign in to comment.