praetorian
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -31,3 +31,3 @@ /** | ||
STRING: 'string', | ||
DATE: 'date' // ISO 8601 | ||
DATE: 'date' // ISO 8601, naturally | ||
} | ||
@@ -64,16 +64,9 @@ | ||
return { | ||
isInteger: function() { | ||
// console.log( 'isInteger() on ', check.data ); | ||
if( !_.isNumber( check.data ) ) { | ||
check.error = 'Not a number'; | ||
isBoolean: function() { | ||
// must be one of these bad boys | ||
if( _.indexOf( [true, false, "true", "false"], check.data ) === -1 ) { | ||
check.error = 'Not a boolean'; | ||
} | ||
return this; | ||
}, | ||
isString: function() { | ||
// console.log( 'isString() on ', check.data ); | ||
if( !_.isString( check.data ) ) { | ||
check.error = 'Not a string'; | ||
} | ||
return this; | ||
}, | ||
isDate: function() { | ||
@@ -122,11 +115,23 @@ // console.log( 'isDate() on ', check.data ); | ||
}, | ||
lengthBetween: function( min, max ) { | ||
// console.log( 'lengthBetween() on ', check.data ); | ||
if ( check.data.length < min ) { | ||
check.error = 'Value is too short'; | ||
} else if ( ( max !== undefined ) && ( check.data.length > max ) ) { | ||
check.error = 'Value is too long'; | ||
isDecimal: function() { | ||
// cant assume automaticTypeConversion is on so isNumber will hopefully cover the check here | ||
if( _.isNaN( parseFloat( check.data ) ) ) { | ||
check.error = 'Not a decimal'; | ||
} | ||
return this; | ||
}, | ||
isInteger: function() { | ||
// console.log( 'isInteger() on ', check.data ); | ||
if( _.isNaN( parseInt( check.data ) ) ) { | ||
check.error = 'Not a number'; | ||
} | ||
return this; | ||
}, | ||
isString: function() { | ||
// console.log( 'isString() on ', check.data ); | ||
if( !_.isString( check.data ) ) { | ||
check.error = 'Not a string'; | ||
} | ||
return this; | ||
}, | ||
execute: function( callback ) { | ||
@@ -191,7 +196,6 @@ callback( check.error ); | ||
var parseJson = function( json, schema, schemaType, elementPrefix ) { | ||
var parseJson = function( json, schema, elementPrefix ) { | ||
// for this level, loop over all the json and see if its required by the schema | ||
_.each( json, function( levelValue, levelKey ) { | ||
// is it a node in the schema? | ||
@@ -216,3 +220,4 @@ if( !schema[levelKey] ) { | ||
_.each( json[levelKey], function( arrayElement, loopCount ) { | ||
parseJson( arrayElement, schema[levelKey].items, schema[levelKey].type, levelKey + '[' + loopCount + '].' ); | ||
// console.log( 'arrayElement', arrayElement ); | ||
parseJson( arrayElement, schema[levelKey].items, levelKey + '[' + loopCount + '].' ); | ||
} ); | ||
@@ -227,3 +232,3 @@ } else { | ||
if( _.isObject( json[levelKey] ) && !_.isArray( json[levelKey] ) && !_.isNull( json[levelKey] ) && !_.isUndefined( json[levelKey] ) ) { | ||
parseJson( json[levelKey], schema[levelKey].properties, schema[levelKey].type, levelKey + '.' ); | ||
parseJson( json[levelKey], schema[levelKey].properties, levelKey + '.' ); | ||
} else { | ||
@@ -233,43 +238,39 @@ // ERROR: this value should be an OBJECT but its an array | ||
} | ||
} else if( ( !_.isUndefined( schema[levelKey].validation ) ) && ( !_.isUndefined( schema[levelKey].validation.rules ) ) ) { | ||
} else if( schema[levelKey].type ) { | ||
// Handle anything else which has rules here (primitives? string, number...) | ||
_.each( schema[levelKey].validation.rules, function( rule, ruleKey ) { | ||
// basic "type" validation only non complex primitive types, | ||
// objects and arrays are dealt with by recursing into them | ||
var validator = new rules( json[levelKey] ); | ||
switch( schema[levelKey].type ) { | ||
var validator = new rules( json[levelKey] ); | ||
switch( ruleKey ) { | ||
case 'boolean': | ||
validator.isBoolean(); | ||
break; | ||
case 'type': | ||
// basic type checking | ||
switch( rule ) { | ||
case 'string': | ||
validator.isString(); | ||
break; | ||
case 'date': | ||
validator.isDate(); | ||
break; | ||
case 'integer': | ||
validator.isInteger(); | ||
break; | ||
case 'decimal': | ||
validator.isDecimal(); | ||
break; | ||
case 'date': | ||
validator.isDate(); | ||
break; | ||
} | ||
break; | ||
case 'integer': | ||
validator.isInteger(); | ||
break; | ||
case 'lengthBetween': // max and min (inclusive) | ||
validator.lengthBetween( rule.min, rule.max ); | ||
break; | ||
case 'string': | ||
validator.isString(); | ||
break; | ||
default: | ||
// case not handled | ||
break; | ||
default: | ||
// case not handled | ||
break; | ||
} | ||
// execute the validation routines and push any errors onto the Praetorian stack | ||
validator.execute( function( err ) { | ||
if( err ){ | ||
addError( elementPrefix + levelKey, err ); | ||
} | ||
// execute the validation routines and push any errors onto the Praetorian stack | ||
validator.execute( function( err ) { | ||
if( err ){ | ||
addError( elementPrefix + levelKey, err ); | ||
} | ||
} ); | ||
} ); | ||
@@ -300,31 +301,7 @@ | ||
// #-1. Do we treat the whole thing as a stack? Or reusable "function" | ||
// 0. benchmark against Doorman for validating a data set against a structure | ||
// #1. remove all junk data (recursive) keys that dont exist in the structure | ||
// #2. ensure items that are present (if REQUIRED) and are of the correct type | ||
// 3. check for ITEMS | ||
// 4. check for STRUCTURES | ||
// #5. what happens when we find an OBJECT? i.e. roomInfo on POST book | ||
// #6. deal with error handling (pass back up stack, modified data) | ||
// 7. individual validation routines | ||
// 8. config for Praetorian, build Doorman config in to somewhere but can be overridable :/ | ||
// 9. isValidDate, isNumber etc, can we use Underscore (isFinite)? | ||
// #10. Identical objects are equal. `0 === -0`, but they aren't identical. | ||
// #11. better error messageing structure (consolidated) | ||
// #12. requirements() for outputting errors from a stack | ||
// #13. figure out what sort of return structures we will provide | ||
// #14. what is the difference between ARRAY and ITEMS | ||
// #15. list types, OBJECT ARRAY, STRUCTURE, ITEMS, CHECKSUM | ||
// #16. in Revolver, book roomInfo is ARRAY but is passed as [object]... | ||
// #17. requirements needs to recurse (see test/index.js test) | ||
// #18. transpose underscore to lodash | ||
// #19. turn off default type conversion | ||
// 20. rules passed in on construction but don't sit inside Praetorian module, basic types should be basked in + date | ||
// 21. type should always be checked as a validation routine itself, validation then works afterwards | ||
// 22. internationalisation, abstract errors to a lib GB and default settings go to GB | ||
// #23. why does index.js throw 2 errors [location && dave] only when new field "dave" was added? | ||
// console.log( 'this', this ); | ||
// TODO: | ||
// 1. rules passed in on construction but don't sit inside Praetorian module, basic types should be basked in + date | ||
// 2. internationalisation, abstract errors to a lib GB and default settings go to GB | ||
parseJson( json, schema, undefined, '' ); | ||
parseJson( json, schema, '' ); | ||
@@ -361,3 +338,3 @@ requiredJson( json, schema, '' ); | ||
'example': message, | ||
'description': ( field['description'] ) ? field['description'] : 'N/A', | ||
'description': ( field['description'] ) ? field['description'] : '', | ||
'required': ( field['required'] ) ? true : false | ||
@@ -364,0 +341,0 @@ }; |
{ | ||
"name": "praetorian", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A structured JSON parser and validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,7 +6,2 @@ Praetorian | ||
Version | ||
-- | ||
0.0.3 (in development) | ||
Installation | ||
@@ -13,0 +8,0 @@ -- |
16539
361
90