Permalink
Browse files

add: rule func

  • Loading branch information...
1 parent b524725 commit 373a62089187f8b5f9c12600d5674cdf3e9c14b6 @mah0x211 committed Nov 21, 2011
Showing with 29 additions and 8 deletions.
  1. +27 −6 lib/auditor.js
  2. +2 −2 package.json
View
@@ -13,10 +13,12 @@
field_name: {
type: [text | email | email_loose | url | date | signed | unsigned]
required: [any]
+ multiline: [boolean]
fix = [number]
min = [number]
max = [number]
match = [regexp]
+ func = [function(val)]
}
}
*/
@@ -33,6 +35,7 @@ var pkg = {
REGEXP_URL = undefined,
// Status
STATUS = {
+ OK: 0,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_ACCEPTABLE: 406,
@@ -226,7 +229,7 @@ this.audit.prototype.addField = function( label, field, rule )
else if( !rule.hasOwnProperty( 'type' ) ||
!self.isString( rule.type ) ||
!TypeRule.test( rule.type ) ){
- throw new Error( 'invalid rule type' );
+ throw new Error( 'invalid rule type: ' + rule.type );
}
else
{
@@ -249,10 +252,19 @@ this.audit.prototype.addField = function( label, field, rule )
}
}
else if( p === 'required' ){
- newRule[p] = val;
+ newRule[p] = ( val ) ? true : false;
}
else if( p === 'multiline' ){
- newRule[p] = val;
+ newRule[p] = ( val ) ? true : false;
+ }
+ else if( p === 'func' )
+ {
+ if( !self.isFunction( val ) ){
+ throw new Error( 'invalid rule: ' + p );
+ }
+ else {
+ newRule[p] = val;
+ }
}
else if( p === 'match' )
{
@@ -342,7 +354,7 @@ this.audit.prototype.check = function( label, field, val )
type: rule.type,
required: ( rule.required ) ? true : false,
val: val,
- errno: 0
+ errno: STATUS.OK
},
tmp = val;
@@ -368,9 +380,11 @@ this.audit.prototype.check = function( label, field, val )
}
else
{
+ // multi-line text
if( rule.multiline ){
tmp = tmp.replace( /\r\n?/g, "\n" );
}
+ // single-line text
else {
tmp = tmp.replace( /[\r\n]/g, '' );
}
@@ -432,12 +446,13 @@ this.audit.prototype.check = function( label, field, val )
if( !self.isNumber( tmp ) || isNaN( tmp ) ){
self.setStatus( result, STATUS.BAD_REQUEST );
}
- // check fix/min/max/match
+ // check fix/min/max/match/func
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 ) ||
- ( self.isRegExp( rule.match ) && !rule.match.test( tmp ) ) ){
+ ( self.isRegExp( rule.match ) && !rule.match.test( tmp ) ) ||
+ ( self.isFunction( rule.func ) && !rule.func( tmp ) ) ){
// 406 NOT_ACCEPTABLE
self.setStatus( result, STATUS.NOT_ACCEPTABLE );
}
@@ -461,10 +476,16 @@ this.audit.prototype.check = function( label, field, val )
}
}
+ // check required
if( !result.required && result.errno === STATUS.NO_CONTENT ){
delete result;
result = undefined;
}
+ // check func
+ else if( result.errno === STATUS.OK && self.isFunction( rule.func ) && !rule.func( tmp ) ){
+ // 406 NOT_ACCEPTABLE
+ self.setStatus( result, STATUS.NOT_ACCEPTABLE );
+ }
return result;
}
View
@@ -6,7 +6,7 @@
],
"description": "",
"homepage": "http://github.com/mah0x211/node-auditor",
- "version" : "0.1.5",
+ "version" : "0.1.6",
"licenses" : [
{
"type": "MIT"
@@ -17,7 +17,7 @@
"lib" : "./lib"
},
"engines" : {
- "node" : ">= 0.3.7"
+ "node" : ">= 0.4.12"
},
"maintainers": [
{

0 comments on commit 373a620

Please sign in to comment.