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!"
- "INVALID"
- "BROKEN"
- "MISSING"
- "UNKNOWN"
beforePeek:
msg:
- 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) {
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;
L = function() {
return that.log.apply(that, arguments);
@ -132,8 +132,28 @@ Output routines for HackMyResume.
case HME.afterInlineConvert:
return L(M2C(this.msgs.afterInlineConvert.msg, 'gray', 'white.dim'), evt.file, evt.fmt);
case HME.afterValidate:
style = evt.isValid ? 'green' : 'yellow';
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);
style = 'red';
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) {
_.each(evt.errors, function(err, idx) {
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) {
return {};
}
r = _validateOne.call(this, t, validator, schemas);
r = _validateOne.call(this, t, validator, schemas, opts);
if (r.fluenterror) {
r.quit = opts.assert;
this.err(r.fluenterror, r);
}
return r;
@ -78,33 +77,42 @@ Implementation of the 'validate' verb for HackMyResume.
return results;
};
_validateOne = function(t, validator, schemas) {
var errCode, errors, fmt, json, obj, ret, validate;
_validateOne = function(t, validator, schemas, opts) {
var errCode, errors, fmt, obj, ret, validate;
ret = {
file: t,
isValid: false
isValid: false,
status: 'unknown'
};
obj = safeLoadJSON(t);
if (obj.ex) {
errCode = obj.ex.operation === 'parse' ? HMSTATUS.parseError : HMSTATUS.readError;
if (errCode === HMSTATUS.readError) {
obj.ex.quiet = true;
}
return {
fluenterror: errCode,
inner: obj.ex
};
}
json = obj.json;
fmt = json.basics ? 'jars' : 'fresh';
errors = [];
fmt = '------';
try {
obj = safeLoadJSON(t);
if (obj.ex) {
if (obj.ex.operation === 'parse') {
errCode = HMSTATUS.parseError;
ret.status = 'broken';
} else {
errCode = HMSTATUS.readError;
ret.status = 'missing';
}
throw {
fluenterror: errCode,
inner: obj.ex.inner
};
}
if (obj.json.basics) {
fmt = 'jars';
} else {
fmt = 'fresh';
}
errors = [];
validate = validator(schemas[fmt], {
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 = ret.isValid ? 'valid' : 'invalid';
if (!ret.isValid) {
errors = validate.errors;
}
@ -113,8 +121,8 @@ Implementation of the 'validate' verb for HackMyResume.
}
this.stat(HMEVENT.afterValidate, {
file: t,
isValid: ret.isValid,
fmt: fmt != null ? fmt.replace('jars', 'JSON Resume') : void 0,
status: ret.status,
fmt: fmt.replace('jars', 'JSON Resume'),
errors: errors
});
if (opts.assert && !ret.isValid) {

View File

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

View File

@ -140,21 +140,25 @@ module.exports = class OutputHandler
evt.file, evt.fmt );
when HME.afterValidate
style = if evt.isValid then 'green' else 'yellow'
L(
M2C( this.msgs.afterValidate.msg[0], 'white' ) +
chalk[style].bold(
if evt.isValid
then this.msgs.afterValidate.msg[1]
else this.msgs.afterValidate.msg[2] ),
evt.file, evt.fmt
);
style = 'red'
adj = ''
msgs = @msgs.afterValidate.msg;
switch evt.status
when 'valid' then style = 'green'; adj = msgs[1]
when 'invalid' then style = 'yellow'; adj = msgs[2]
when 'broken' then style = 'red'; adj = msgs[3]
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
_.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
when HME.afterPeek
sty = if evt.error then 'red' else ( if evt.target != undefined then 'green' else 'yellow' )

View File

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