2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Implementation of the 'validate' verb for HackMyResume.
|
|
|
|
@module verbs/validate
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
2016-01-29 20:23:57 +00:00
|
|
|
var FS, HMEVENT, HMSTATUS, ResumeFactory, SyntaxErrorEx, ValidateVerb, Verb, _, chalk, safeLoadJSON, validate;
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
FS = require('fs');
|
|
|
|
|
|
|
|
ResumeFactory = require('../core/resume-factory');
|
|
|
|
|
|
|
|
SyntaxErrorEx = require('../utils/syntax-error-ex');
|
|
|
|
|
|
|
|
chalk = require('chalk');
|
|
|
|
|
|
|
|
Verb = require('../verbs/verb');
|
|
|
|
|
|
|
|
HMSTATUS = require('../core/status-codes');
|
|
|
|
|
|
|
|
HMEVENT = require('../core/event-codes');
|
|
|
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
2016-01-29 20:23:57 +00:00
|
|
|
safeLoadJSON = require('../utils/safe-json-loader');
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** An invokable resume validation command. */
|
|
|
|
|
|
|
|
ValidateVerb = module.exports = Verb.extend({
|
|
|
|
init: function() {
|
|
|
|
return this._super('validate');
|
|
|
|
},
|
|
|
|
invoke: function() {
|
2016-01-29 20:23:57 +00:00
|
|
|
var ret;
|
2016-01-27 10:29:26 +00:00
|
|
|
this.stat(HMEVENT.begin, {
|
|
|
|
cmd: 'validate'
|
|
|
|
});
|
2016-01-29 20:23:57 +00:00
|
|
|
ret = validate.apply(this, arguments);
|
2016-01-27 10:29:26 +00:00
|
|
|
this.stat(HMEVENT.end);
|
2016-01-29 20:23:57 +00:00
|
|
|
return ret;
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/** Validate 1 to N resumes in FRESH or JSON Resume format. */
|
|
|
|
|
|
|
|
validate = function(sources, unused, opts) {
|
2016-01-29 20:23:57 +00:00
|
|
|
var schemas, validator;
|
2016-01-27 10:29:26 +00:00
|
|
|
if (!sources || !sources.length) {
|
|
|
|
throw {
|
|
|
|
fluenterror: HMSTATUS.resumeNotFoundAlt,
|
|
|
|
quit: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
validator = require('is-my-json-valid');
|
|
|
|
schemas = {
|
|
|
|
fresh: require('fresca'),
|
|
|
|
jars: require('../core/resume.json')
|
|
|
|
};
|
2016-01-29 20:23:57 +00:00
|
|
|
return _.map(sources, function(t) {
|
|
|
|
var errCode, errors, fmt, json, obj, ret;
|
2016-01-27 10:29:26 +00:00
|
|
|
ret = {
|
2016-01-29 20:23:57 +00:00
|
|
|
file: t,
|
2016-01-27 10:29:26 +00:00
|
|
|
isValid: false
|
|
|
|
};
|
2016-01-29 20:23:57 +00:00
|
|
|
obj = safeLoadJSON(t);
|
|
|
|
if (!obj.ex) {
|
|
|
|
json = obj.json;
|
|
|
|
fmt = json.basics ? 'jrs' : 'fresh';
|
|
|
|
errors = [];
|
|
|
|
try {
|
|
|
|
validate = validator(schemas[fmt], {
|
|
|
|
formats: {
|
|
|
|
date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ret.isValid = validate(json);
|
|
|
|
if (!ret.isValid) {
|
|
|
|
errors = validate.errors;
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2016-01-29 20:23:57 +00:00
|
|
|
} catch (_error) {
|
|
|
|
ret.ex = _error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errCode = obj.ex.operation === 'parse' ? HMSTATUS.parseError : HMSTATUS.readError;
|
|
|
|
if (errCode === HMSTATUS.readError) {
|
|
|
|
obj.ex.quiet = true;
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2016-01-29 20:23:57 +00:00
|
|
|
this.setError(errCode, obj.ex);
|
|
|
|
this.err(errCode, obj.ex);
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
|
|
|
this.stat(HMEVENT.afterValidate, {
|
2016-01-29 20:23:57 +00:00
|
|
|
file: t,
|
2016-01-27 10:29:26 +00:00
|
|
|
isValid: ret.isValid,
|
2016-01-29 20:23:57 +00:00
|
|
|
fmt: fmt != null ? fmt.replace('jars', 'JSON Resume') : void 0,
|
2016-01-27 10:29:26 +00:00
|
|
|
errors: errors
|
|
|
|
});
|
|
|
|
if (opts.assert && !ret.isValid) {
|
|
|
|
throw {
|
2016-01-29 20:23:57 +00:00
|
|
|
fluenterror: HMSTATUS.invalid,
|
|
|
|
shouldExit: true
|
2016-01-27 10:29:26 +00:00
|
|
|
};
|
|
|
|
}
|
2016-01-29 20:23:57 +00:00
|
|
|
console.log('1111');
|
2016-01-27 10:29:26 +00:00
|
|
|
return ret;
|
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|