1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-23 00:40:10 +00:00
HackMyResume/dist/verbs/validate.js

103 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

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() {
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');
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() {
2016-01-31 13:37:12 +00:00
return this._super('validate', validate);
2016-01-27 10:29:26 +00:00
}
});
/** Validate 1 to N resumes in FRESH or JSON Resume format. */
validate = function(sources, unused, opts) {
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')
};
return _.map(sources, function(t) {
var errCode, errors, fmt, json, obj, ret;
2016-01-27 10:29:26 +00:00
ret = {
file: t,
2016-01-27 10:29:26 +00:00
isValid: false
};
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
}
} 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
}
this.setError(errCode, obj.ex);
this.err(errCode, obj.ex);
2016-01-27 10:29:26 +00:00
}
this.stat(HMEVENT.afterValidate, {
file: t,
2016-01-27 10:29:26 +00:00
isValid: ret.isValid,
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 {
fluenterror: HMSTATUS.invalid,
shouldExit: true
2016-01-27 10:29:26 +00:00
};
}
return ret;
}, this);
};
}).call(this);