1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00
HackMyResume/dist/verbs/validate.js

133 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Implementation of the 'validate' verb for HackMyResume.
@module verbs/validate
@license MIT. See LICENSE.md for details.
*/
/**
Validate a single resume.
@returns {
file: <fileName>,
isValid: <validFlag>,
status: <validationStatus>,
violations: <validationErrors>,
schema: <schemaType>,
error: <errorObject>
}
*/
var FS, HMEVENT, HMSTATUS, ResumeFactory, SyntaxErrorEx, ValidateVerb, Verb, _, _validate, _validateOne, chalk, safeLoadJSON;
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. */
2018-02-12 05:05:29 +00:00
module.exports = ValidateVerb = class ValidateVerb extends Verb {
constructor() {
super('validate', _validate);
2016-01-27 10:29:26 +00:00
}
2018-02-12 05:05:29 +00:00
};
2016-01-27 10:29:26 +00:00
2018-02-12 05:05:29 +00:00
// Validate 1 to N resumes in FRESH or JSON Resume format.
2016-02-02 02:14:36 +00:00
_validate = function(sources, unused, opts) {
var results, schemas, validator;
2016-01-27 10:29:26 +00:00
if (!sources || !sources.length) {
2016-02-02 02:14:36 +00:00
this.err(HMSTATUS.resumeNotFoundAlt, {
2016-01-27 10:29:26 +00:00
quit: true
2016-02-02 02:14:36 +00:00
});
return null;
2016-01-27 10:29:26 +00:00
}
validator = require('is-my-json-valid');
schemas = {
2018-02-02 09:48:28 +00:00
fresh: require('fresh-resume-schema'),
2016-01-27 10:29:26 +00:00
jars: require('../core/resume.json')
};
2016-02-02 02:14:36 +00:00
results = _.map(sources, function(t) {
var r;
r = _validateOne.call(this, t, validator, schemas, opts);
2016-02-13 04:47:08 +00:00
if (r.error) {
this.err(r.error.fluenterror, r.error);
2016-02-02 02:14:36 +00:00
}
return r;
}, this);
if (this.hasError() && !opts.assert) {
this.reject(this.errorCode);
} else if (!this.hasError()) {
this.resolve(results);
}
return results;
};
_validateOne = function(t, validator, schemas, opts) {
2018-02-12 05:05:29 +00:00
var err, errCode, obj, ret, validate;
2016-02-02 02:14:36 +00:00
ret = {
file: t,
isValid: false,
2016-02-13 04:47:08 +00:00
status: 'unknown',
schema: '-----'
2016-02-02 02:14:36 +00:00
};
try {
2018-02-12 05:05:29 +00:00
// Read and parse the resume JSON. Won't throw.
obj = safeLoadJSON(t);
2016-02-13 05:11:52 +00:00
if (!obj.ex) {
2016-02-13 04:47:08 +00:00
if (obj.json.basics) {
ret.schema = 'jars';
} else {
ret.schema = 'fresh';
}
validate = validator(schemas[ret.schema], {
formats: {
date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/
}
});
ret.isValid = validate(obj.json);
ret.status = ret.isValid ? 'valid' : 'invalid';
if (!ret.isValid) {
ret.violations = validate.errors;
2016-01-27 10:29:26 +00:00
}
2016-02-13 05:11:52 +00:00
} else {
2018-02-12 05:05:29 +00:00
// If failure, package JSON read/parse errors
if (obj.ex.op === 'parse') {
2016-02-13 05:11:52 +00:00
errCode = HMSTATUS.parseError;
ret.status = 'broken';
} else {
errCode = HMSTATUS.readError;
ret.status = 'missing';
}
ret.error = {
fluenterror: errCode,
inner: obj.ex.inner,
quiet: errCode === HMSTATUS.readError
};
2016-01-27 10:29:26 +00:00
}
2018-02-12 05:05:29 +00:00
} catch (error) {
err = error;
// Package any unexpected exceptions
2016-02-13 05:11:52 +00:00
ret.error = {
fluenterror: HMSTATUS.validateError,
2018-02-12 05:05:29 +00:00
inner: err
2016-02-13 05:11:52 +00:00
};
2016-02-02 02:14:36 +00:00
}
2016-02-13 04:47:08 +00:00
this.stat(HMEVENT.afterValidate, ret);
2016-02-02 02:14:36 +00:00
return ret;
2016-01-27 10:29:26 +00:00
};
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=validate.js.map