Permalink
Browse files

add: define status code

  • Loading branch information...
1 parent 966a0ff commit 7bcc32b4b53b51dbce5c9e714c09dc5965de1702 @mah0x211 committed May 23, 2011
Showing with 29 additions and 16 deletions.
  1. +28 −15 lib/auditor.js
  2. +1 −1 package.json
View
@@ -16,7 +16,7 @@
fix = [number]
min = [number]
max = [number]
- match = [builtin_* | your_regexp]
+ match = [regexp]
}
}
*/
@@ -26,7 +26,14 @@ var TypeRule = /^(text|email(_loose)?|url|date|(un)?signed)$/,
// REGEXP
REGEXP_EMAIL = undefined,
REGEXP_EMAIL_LOOSE = undefined,
- REGEXP_URL = undefined;
+ REGEXP_URL = undefined,
+ // Status
+ STATUS = {
+ NO_CONTENT = 204,
+ BAD_REQUEST = 400,
+ NOT_ACCEPTABLE = 406,
+ PRECONDITION_FAILED = 412
+ };
function Init()
{
@@ -142,6 +149,12 @@ function auditor()
this.isURL = isURL;
}
+// MARK: defined status
+// add options
+for( var name in STATUS ){
+ auditor.prototype[ename] = STATUS[name];
+}
+
auditor.prototype.addField = function( label, field, rule )
{
if( !isString( label ) ){
@@ -276,13 +289,13 @@ auditor.prototype.check = function( label, field, val )
// unknown field type
// PRECONDITION_FAILED
if( !TypeRule.test( rule.type ) ){
- result.errno = 412;
+ result.errno = STATUS.PRECONDITION_FAILED;
result.errstr = 'unknown field type';
}
// check defined
// 204 NO_CONTENT
else if( tmp === undefined || tmp === null ){
- result.errno = 204;
+ result.errno = STATUS.NO_CONTENT;
result.errstr = 'field val undefined';
}
// check text
@@ -293,13 +306,13 @@ auditor.prototype.check = function( label, field, val )
}
// 400 BAD_REQUEST
if( !isString( tmp ) ){
- result.errno = 400;
+ result.errno = STATUS.BAD_REQUEST;
result.errstr = 'invalid field type';
}
// check length
else if( !tmp.length ){
// 204 NO_CONTENT
- result.errno = 204;
+ result.errno = STATUS.NO_CONTENT;
result.errstr = 'field val undefined';
}
// check fix/min/max/match
@@ -308,7 +321,7 @@ auditor.prototype.check = function( label, field, val )
( isNumber( rule.max ) && tmp.length > rule.max ) ||
( isRegExp( rule.match ) && !rule.test( tmp ) ) ){
// 406 NOT_ACCEPTABLE
- result.errno = 406;
+ result.errno = STATUS.NOT_ACCEPTABLE;
result.errstr = 'invalid field val';
}
else {
@@ -321,7 +334,7 @@ auditor.prototype.check = function( label, field, val )
{
// 406 NOT_ACCEPTABLE
if( !isEmail( tmp ) ){
- result.errno = 406;
+ result.errno = STATUS.NOT_ACCEPTABLE;
result.errstr = 'invalid field val';
}
}
@@ -330,7 +343,7 @@ auditor.prototype.check = function( label, field, val )
{
// 406 NOT_ACCEPTABLE
if( !isEmailLoose( tmp ) ){
- result.errno = 406;
+ result.errno = STATUS.NOT_ACCEPTABLE;
result.errstr = 'invalid field val';
}
}
@@ -339,7 +352,7 @@ auditor.prototype.check = function( label, field, val )
{
// 406 NOT_ACCEPTABLE
if( !isURL( tmp ) ){
- result.errno = 406;
+ result.errno = STATUS.NOT_ACCEPTABLE;
result.errstr = 'invalid field val';
}
}
@@ -352,7 +365,7 @@ auditor.prototype.check = function( label, field, val )
// 400 BAD_REQUEST
if( !isNumber( tmp ) || isNaN( tmp ) ){
- result.errno = 400;
+ result.errno = STATUS.BAD_REQUEST;
result.errstr = 'invalid field type';
}
// check fix/min/max/match
@@ -362,7 +375,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 = 406;
+ result.errno = STATUS.NOT_ACCEPTABLE;
result.errstr = 'invalid field val';
}
else {
@@ -373,21 +386,21 @@ auditor.prototype.check = function( label, field, val )
else if( result.type === 'date' )
{
if( !isString( tmp ) ){
- result.errno = 400;
+ result.errno = STATUS.BAD_REQUEST;
result.errstr = 'invalid field type';
}
else
{
tmp = Date.parse( tmp );
// 400 BAD_REQUEST
if( !isNumber( tmp ) || isNaN( tmp ) ){
- result.errno = 400;
+ result.errno = STATUS.BAD_REQUEST;
result.errstr = 'invalid field type';
}
}
}
- if( !result.required && result.errno === 204 ){
+ if( !result.required && result.errno === STATUS.NO_CONTENT ){
delete result;
result = undefined;
}
View
@@ -6,7 +6,7 @@
],
"description": "",
"homepage": "http://github.com/mah0x211/node-auditor",
- "version" : "0.1.0",
+ "version" : "0.1.1",
"licenses" : [
{
"type": "MIT"

0 comments on commit 7bcc32b

Please sign in to comment.