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

Fixup VALIDATE command.

Introduce MISSING and UNKNOWN states alongside BROKEN, VALID, and
INVALID and fix regressions introduced in previous refactorings.
This commit is contained in:
hacksalot 2016-02-12 22:49:56 -05:00
parent 964350d3c7
commit 7a60cd0bab
6 changed files with 99 additions and 63 deletions

2
dist/cli/msg.yml vendored
View File

@ -43,6 +43,8 @@ events:
- "VALID!" - "VALID!"
- "INVALID" - "INVALID"
- "BROKEN" - "BROKEN"
- "MISSING"
- "UNKNOWN"
beforePeek: beforePeek:
msg: msg:
- Peeking at **%s** in **%s** - Peeking at **%s** in **%s**

26
dist/cli/out.js vendored
View File

@ -57,7 +57,7 @@ Output routines for HackMyResume.
}; };
OutputHandler.prototype["do"] = function(evt) { OutputHandler.prototype["do"] = function(evt) {
var L, WRAP, info, msg, numFormats, output, rawTpl, sty, style, suffix, template, that, themeName, tot; var L, WRAP, adj, info, msg, msgs, numFormats, output, rawTpl, sty, style, suffix, template, that, themeName, tot;
that = this; that = this;
L = function() { L = function() {
return that.log.apply(that, arguments); return that.log.apply(that, arguments);
@ -132,8 +132,28 @@ Output routines for HackMyResume.
case HME.afterInlineConvert: case HME.afterInlineConvert:
return L(M2C(this.msgs.afterInlineConvert.msg, 'gray', 'white.dim'), evt.file, evt.fmt); return L(M2C(this.msgs.afterInlineConvert.msg, 'gray', 'white.dim'), evt.file, evt.fmt);
case HME.afterValidate: case HME.afterValidate:
style = evt.isValid ? 'green' : 'yellow'; style = 'red';
L(M2C(this.msgs.afterValidate.msg[0], 'white') + chalk[style].bold(evt.isValid ? this.msgs.afterValidate.msg[1] : this.msgs.afterValidate.msg[2]), evt.file, evt.fmt); adj = '';
msgs = this.msgs.afterValidate.msg;
switch (evt.status) {
case 'valid':
style = 'green';
adj = msgs[1];
break;
case 'invalid':
style = 'yellow';
adj = msgs[2];
break;
case 'broken':
style = 'red';
adj = msgs[3];
break;
case 'missing':
style = 'red';
adj = msgs[4];
}
evt.fmt = evt.fmt.toUpperCase();
L(M2C(msgs[0], 'white') + chalk[style].bold(adj), evt.file, evt.fmt);
if (evt.errors) { if (evt.errors) {
_.each(evt.errors, function(err, idx) { _.each(evt.errors, function(err, idx) {
L(chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.', 'resume.').toUpperCase() + ' ' + err.message)); L(chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.', 'resume.').toUpperCase() + ' ' + err.message));

View File

@ -63,9 +63,8 @@ Implementation of the 'validate' verb for HackMyResume.
if (this.hasError() && opts.assert) { if (this.hasError() && opts.assert) {
return {}; return {};
} }
r = _validateOne.call(this, t, validator, schemas); r = _validateOne.call(this, t, validator, schemas, opts);
if (r.fluenterror) { if (r.fluenterror) {
r.quit = opts.assert;
this.err(r.fluenterror, r); this.err(r.fluenterror, r);
} }
return r; return r;
@ -78,33 +77,42 @@ Implementation of the 'validate' verb for HackMyResume.
return results; return results;
}; };
_validateOne = function(t, validator, schemas) { _validateOne = function(t, validator, schemas, opts) {
var errCode, errors, fmt, json, obj, ret, validate; var errCode, errors, fmt, obj, ret, validate;
ret = { ret = {
file: t, file: t,
isValid: false isValid: false,
status: 'unknown'
}; };
fmt = '------';
try {
obj = safeLoadJSON(t); obj = safeLoadJSON(t);
if (obj.ex) { if (obj.ex) {
errCode = obj.ex.operation === 'parse' ? HMSTATUS.parseError : HMSTATUS.readError; if (obj.ex.operation === 'parse') {
if (errCode === HMSTATUS.readError) { errCode = HMSTATUS.parseError;
obj.ex.quiet = true; ret.status = 'broken';
} else {
errCode = HMSTATUS.readError;
ret.status = 'missing';
} }
return { throw {
fluenterror: errCode, fluenterror: errCode,
inner: obj.ex inner: obj.ex.inner
}; };
} }
json = obj.json; if (obj.json.basics) {
fmt = json.basics ? 'jars' : 'fresh'; fmt = 'jars';
} else {
fmt = 'fresh';
}
errors = []; errors = [];
try {
validate = validator(schemas[fmt], { validate = validator(schemas[fmt], {
formats: { formats: {
date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/
} }
}); });
ret.isValid = validate(json); ret.isValid = validate(obj.json);
ret.status = ret.isValid ? 'valid' : 'invalid';
if (!ret.isValid) { if (!ret.isValid) {
errors = validate.errors; errors = validate.errors;
} }
@ -113,8 +121,8 @@ Implementation of the 'validate' verb for HackMyResume.
} }
this.stat(HMEVENT.afterValidate, { this.stat(HMEVENT.afterValidate, {
file: t, file: t,
isValid: ret.isValid, status: ret.status,
fmt: fmt != null ? fmt.replace('jars', 'JSON Resume') : void 0, fmt: fmt.replace('jars', 'JSON Resume'),
errors: errors errors: errors
}); });
if (opts.assert && !ret.isValid) { if (opts.assert && !ret.isValid) {

View File

@ -43,6 +43,8 @@ events:
- "VALID!" - "VALID!"
- "INVALID" - "INVALID"
- "BROKEN" - "BROKEN"
- "MISSING"
- "UNKNOWN"
beforePeek: beforePeek:
msg: msg:
- Peeking at **%s** in **%s** - Peeking at **%s** in **%s**

View File

@ -140,18 +140,22 @@ module.exports = class OutputHandler
evt.file, evt.fmt ); evt.file, evt.fmt );
when HME.afterValidate when HME.afterValidate
style = if evt.isValid then 'green' else 'yellow' style = 'red'
L( adj = ''
M2C( this.msgs.afterValidate.msg[0], 'white' ) + msgs = @msgs.afterValidate.msg;
chalk[style].bold( switch evt.status
if evt.isValid when 'valid' then style = 'green'; adj = msgs[1]
then this.msgs.afterValidate.msg[1] when 'invalid' then style = 'yellow'; adj = msgs[2]
else this.msgs.afterValidate.msg[2] ), when 'broken' then style = 'red'; adj = msgs[3]
evt.file, evt.fmt when 'missing' then style = 'red'; adj = msgs[4]
); evt.fmt = evt.fmt.toUpperCase()
L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.fmt)
if evt.errors if evt.errors
_.each evt.errors, (err,idx) -> _.each evt.errors, (err,idx) ->
L( chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.','resume.').toUpperCase() + ' ' + err.message)) L( chalk.yellow.bold('--> ') +
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() +
' ' + err.message))
return return
, @ , @
return return

View File

@ -29,7 +29,7 @@ module.exports = class ValidateVerb extends Verb
_validate = (sources, unused, opts) -> _validate = (sources, unused, opts) ->
if !sources || !sources.length if !sources || !sources.length
@err HMSTATUS.resumeNotFoundAlt, { quit: true } @err HMSTATUS.resumeNotFoundAlt, quit: true
return null return null
validator = require 'is-my-json-valid' validator = require 'is-my-json-valid'
@ -41,10 +41,8 @@ _validate = (sources, unused, opts) ->
# each resume valid, invalid, or broken. # each resume valid, invalid, or broken.
results = _.map sources, (t) -> results = _.map sources, (t) ->
return { } if @hasError() and opts.assert return { } if @hasError() and opts.assert
r = _validateOne.call @, t, validator, schemas r = _validateOne.call @, t, validator, schemas, opts
if r.fluenterror @err r.fluenterror, r if r.fluenterror
r.quit = opts.assert
@err r.fluenterror, r
r r
, @ , @
@ -55,30 +53,32 @@ _validate = (sources, unused, opts) ->
results results
_validateOne = (t, validator, schemas) -> _validateOne = (t, validator, schemas, opts) ->
ret = file: t, isValid: false ret = file: t, isValid: false, status: 'unknown'
fmt = '------'
try
# Load the input file JSON 1st # Load the input file JSON 1st
obj = safeLoadJSON t obj = safeLoadJSON t
if obj.ex if obj.ex # safeLoadJSON can only return a READ error or a PARSE error
# safeLoadJSON can only return a READ error or a PARSE error if obj.ex.operation == 'parse'
errCode = if obj.ex.operation == 'parse' then HMSTATUS.parseError else HMSTATUS.readError errCode = HMSTATUS.parseError
if errCode == HMSTATUS.readError ret.status = 'broken'
obj.ex.quiet = true else
return fluenterror: errCode, inner: obj.ex errCode = HMSTATUS.readError
ret.status = 'missing'
throw fluenterror: errCode, inner: obj.ex.inner
# Successfully read the resume. Now parse it as JSON. # Successfully read the resume. Now parse it as JSON.
json = obj.json if obj.json.basics then fmt = 'jars' else fmt = 'fresh'
fmt = if json.basics then 'jars' else 'fresh'
errors = [] errors = []
validate = validator schemas[ fmt ], # Note [1]
try
validate = validator schemas[ fmt ], { # Note [1]
formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ } formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ }
};
ret.isValid = validate json
ret.isValid = validate obj.json
ret.status = if ret.isValid then 'valid' else 'invalid'
if !ret.isValid if !ret.isValid
errors = validate.errors errors = validate.errors
catch catch
@ -86,8 +86,8 @@ _validateOne = (t, validator, schemas) ->
@stat HMEVENT.afterValidate, @stat HMEVENT.afterValidate,
file: t file: t
isValid: ret.isValid status: ret.status
fmt: fmt?.replace 'jars', 'JSON Resume' fmt: fmt.replace 'jars', 'JSON Resume'
errors: errors errors: errors
if opts.assert and !ret.isValid if opts.assert and !ret.isValid