Skip to content

Instantly share code, notes, and snippets.

@razorthink-com
Last active August 30, 2015 10:27
Show Gist options
  • Save razorthink-com/bf38c1f590fec3313590 to your computer and use it in GitHub Desktop.
Save razorthink-com/bf38c1f590fec3313590 to your computer and use it in GitHub Desktop.
function validateTypes( type, value, shouldLog ) {
/* --------- Type Checker --------- */
function _typeOf( item ) {
return Object.prototype.toString.call( item ).slice( 8, -1 );
}
function checkPrimitiveType( type, value, errorLog ) {
var isValid = ( type == _typeOf( value ) );
if ( !isValid ) errorLog.push( { type: type, value: value } );
return isValid;
}
function checkObjectType( type, value, errorLog ) {
if ( _typeOf( value ) != 'Object' ) return false;
return Object.keys( type ).every( function checkPropertyType ( tKey ) {
var isValid = checkType( type[ tKey ], value[ tKey ], errorLog );
if ( !isValid ) errorLog.push( { type: type[ tKey ], value: value[ tKey ], property: tKey } );
else errorLog.pop( 'property' );
return isValid;
} );
}
function checkArrayType( type, value, errorLog ) {
if ( _typeOf( value ) != 'Array' ) return false;
return value.every( function checkArrayItemType ( item, index ) {
var isValid = checkType( type[ 0 ], item, errorLog );
if ( !isValid ) errorLog.push( { type: type[ 0 ], value: item, index: index } );
else errorLog.pop( 'index' );
return isValid;
} );
}
function checkType( type, value, errorLog ) {
switch ( _typeOf( type ) ) {
case 'Object': return checkObjectType( type, value, errorLog );
case 'Array': return checkArrayType( type, value, errorLog );
default: return checkPrimitiveType( type, value, errorLog );
}
}
/* --------- Error Log Accumulator --------- */
function Logger( items ) {
var log = items.reduce( function ( obj, key ) {
obj[ key ] = []; return obj;
}, { } );
function push( items ) {
Object.keys( log ).forEach( function ( key ) {
if ( key in items ) log[ key ].push( items[ key ] );
} );
}
function pop( item ) { if ( item in log ) log[ item ].pop() }
function get( item ) { if ( item in log ) return log[ item ] }
return { push: push, pop: pop, get: get }
}
/* --------- Console Error Printer --------- */
function buildObjectPath( properties, indexes ) {
return properties.reduce( function ( path, prop, index ) {
var indexStr = !!indexes[ index ] ? '[' + indexes[ index ] + ']' : ''
return path + '.' + prop + indexStr;
}, '' )
}
function consolePrintError( errorLog ) {
var objPath = buildObjectPath( errorLog.get('property').reverse(), errorLog.get('index') ) || '~';
console.log( '%cType Mismatch!', 'color: red' );
console.log( '\t%cPath:', 'color: red', objPath );
console.log( '\t%cExpected:', 'color: red', errorLog.get('type')[ 0 ] );
console.log( '\t%cReceived:', 'color: red', errorLog.get('value')[ 0 ] );
}
/* --------- Validate Types --------- */
var errorLog = Logger( [ 'type', 'value', 'index', 'property' ] );
var isValid = checkType( type, value, errorLog );
if ( !isValid && shouldLog ) consolePrintError( errorLog );
return isValid;
}
function validateTypes( type, value ) {
function _typeOf( item ) {
return Object.prototype.toString.call( item ).slice( 8, -1 );
}
function checkPrimitiveType( type, value ) {
return ( type == _typeOf( value ) );
}
function checkObjectType( type, value ) {
if ( _typeOf( value ) != 'Object' ) return false;
return Object.keys( type ).every( function checkPropertyType ( tKey ) {
return checkType( type[ tKey ], value[ tKey ] );
} );
}
function checkArrayType( type, value ) {
if ( _typeOf( value ) != 'Array' ) return false;
return value.every( function checkArrayItemType ( item ) {
return checkType( type[ 0 ], item )
} );
}
function checkType( type, value ) {
switch ( _typeOf( type ) ) {
case 'Object': return checkObjectType( type, value );
case 'Array': return checkArrayType( type, value );
default: return checkPrimitiveType( type, value );
}
}
return checkType( type, value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment