2015-12-31 08:34:41 +00:00
|
|
|
/**
|
|
|
|
Implementation of the 'validate' verb for HackMyResume.
|
|
|
|
@module validate.js
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
*/
|
|
|
|
|
2015-12-21 07:56:02 +00:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
var FS = require('fs');
|
2015-12-28 09:37:42 +00:00
|
|
|
var ResumeFactory = require('../core/resume-factory');
|
2015-12-31 08:34:41 +00:00
|
|
|
var SyntaxErrorEx = require('../utils/syntax-error-ex');
|
2016-01-01 09:44:14 +00:00
|
|
|
var chalk = require('chalk');
|
2015-12-21 07:56:02 +00:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
|
|
|
/**
|
|
|
|
Validate 1 to N resumes in either FRESH or JSON Resume format.
|
|
|
|
*/
|
2015-12-31 08:34:41 +00:00
|
|
|
function validate( sources, unused, opts, logger ) {
|
2015-12-21 07:56:02 +00:00
|
|
|
var _log = logger || console.log;
|
2015-12-31 08:34:41 +00:00
|
|
|
if( !sources || !sources.length ) { throw { fluenterror: 6 }; }
|
2015-12-21 07:56:02 +00:00
|
|
|
var isValid = true;
|
|
|
|
|
|
|
|
var validator = require('is-my-json-valid');
|
|
|
|
var schemas = {
|
2015-12-24 08:23:56 +00:00
|
|
|
fresh: require('fresca'),
|
2015-12-21 07:56:02 +00:00
|
|
|
jars: require('../core/resume.json')
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load input resumes...
|
2015-12-31 08:34:41 +00:00
|
|
|
sources.forEach(function( src ) {
|
|
|
|
|
|
|
|
var result = ResumeFactory.loadOne( src, function(){}, null, false );
|
|
|
|
if( result.error ) {
|
2016-01-01 09:44:14 +00:00
|
|
|
_log( chalk.white('Validating ') + chalk.gray.bold(src) + chalk.white(' against ') + chalk.gray.bold('AUTO') + chalk.white(' schema:') + chalk.red.bold(' BROKEN') );
|
2015-12-31 08:34:41 +00:00
|
|
|
|
|
|
|
var ex = result.error; // alias
|
|
|
|
if ( ex instanceof SyntaxError) {
|
|
|
|
var info = new SyntaxErrorEx( ex, result.raw );
|
2016-01-01 09:44:14 +00:00
|
|
|
_log( chalk.red.bold('--> ') + chalk.red(src.toUpperCase() + ' contains invalid JSON on line ' +
|
|
|
|
info.line + ' column ' + info.col + '.') +
|
|
|
|
chalk.red(' Unable to validate.') );
|
|
|
|
_log( chalk.red(' INTERNAL: ' + ex) );
|
2015-12-21 07:56:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-01-01 09:44:14 +00:00
|
|
|
_log(chalk.red.bold('ERROR: ' + ex.toString()));
|
2015-12-21 07:56:02 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-31 08:34:41 +00:00
|
|
|
var json = result.json;
|
2015-12-21 07:56:02 +00:00
|
|
|
var isValid = false;
|
2016-01-01 09:44:14 +00:00
|
|
|
var style = 'green';
|
2015-12-21 07:56:02 +00:00
|
|
|
var errors = [];
|
2015-12-31 08:34:41 +00:00
|
|
|
var fmt = json.meta && (json.meta.format==='FRESH@0.1.0') ? 'fresh':'jars';
|
2015-12-21 07:56:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
var validate = validator( schemas[ fmt ], { // Note [1]
|
|
|
|
formats: {
|
|
|
|
date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-31 08:34:41 +00:00
|
|
|
isValid = validate( json );
|
2015-12-21 07:56:02 +00:00
|
|
|
if( !isValid ) {
|
2016-01-01 09:44:14 +00:00
|
|
|
style = 'yellow';
|
2015-12-21 07:56:02 +00:00
|
|
|
errors = validate.errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-31 08:34:41 +00:00
|
|
|
catch(exc) {
|
2015-12-21 07:56:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-01 09:44:14 +00:00
|
|
|
_log( chalk.white('Validating ') + chalk.white.bold(result.file) + chalk.white(' against ') +
|
|
|
|
chalk.white.bold(fmt.replace('jars','JSON Resume').toUpperCase()) +
|
|
|
|
chalk.white(' schema: ') + chalk[style].bold(isValid ? 'VALID!' : 'INVALID') );
|
2015-12-21 07:56:02 +00:00
|
|
|
|
|
|
|
errors.forEach(function(err,idx) {
|
2016-01-01 09:44:14 +00:00
|
|
|
_log( chalk.yellow.bold('--> ') +
|
|
|
|
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() + ' ' +
|
|
|
|
err.message) );
|
2015-12-21 07:56:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|