More VALIDATE fixups.

This commit is contained in:
hacksalot 2016-02-12 23:47:08 -05:00
parent 7a60cd0bab
commit c889664c31
6 changed files with 80 additions and 75 deletions

3
dist/cli/main.js vendored
View File

@ -240,6 +240,9 @@ Definition of the `main` function.
if (_opts.debug) {
msgs = require('./msg').errors;
logMsg(printf(M2C(msgs.exiting.msg, 'cyan'), finalErrorCode));
if (err.stack) {
logMsg(err.stack);
}
}
_exitCallback(finalErrorCode);
};

8
dist/cli/out.js vendored
View File

@ -152,10 +152,10 @@ Output routines for HackMyResume.
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) {
evt.schema = evt.schema.toUpperCase();
L(M2C(msgs[0], 'white') + chalk[style].bold(adj), evt.file, evt.schema);
if (evt.violations) {
_.each(evt.violations, function(err, idx) {
L(chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.', 'resume.').toUpperCase() + ' ' + err.message));
}, this);
}

View File

@ -42,9 +42,6 @@ Implementation of the 'validate' verb for HackMyResume.
})(Verb);
/** Validate 1 to N resumes in FRESH or JSON Resume format. */
_validate = function(sources, unused, opts) {
var results, schemas, validator;
if (!sources || !sources.length) {
@ -60,12 +57,9 @@ Implementation of the 'validate' verb for HackMyResume.
};
results = _.map(sources, function(t) {
var r;
if (this.hasError() && opts.assert) {
return {};
}
r = _validateOne.call(this, t, validator, schemas, opts);
if (r.fluenterror) {
this.err(r.fluenterror, r);
if (r.error) {
this.err(r.error.fluenterror, r.error);
}
return r;
}, this);
@ -77,14 +71,27 @@ Implementation of the 'validate' verb for HackMyResume.
return results;
};
/**
Validate a single resume.
@returns {
file: <fileName>,
isValid: <validFlag>,
status: <validationStatus>,
violations: <validationErrors>,
schema: <schemaType>,
error: <errorObject>
}
*/
_validateOne = function(t, validator, schemas, opts) {
var errCode, errors, fmt, obj, ret, validate;
var errCode, obj, ret, validate;
ret = {
file: t,
isValid: false,
status: 'unknown'
status: 'unknown',
schema: '-----'
};
fmt = '------';
try {
obj = safeLoadJSON(t);
if (obj.ex) {
@ -95,42 +102,33 @@ Implementation of the 'validate' verb for HackMyResume.
errCode = HMSTATUS.readError;
ret.status = 'missing';
}
throw {
ret.error = {
fluenterror: errCode,
inner: obj.ex.inner
inner: obj.ex.inner,
quiet: errCode === HMSTATUS.readError
};
}
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})?)?$/
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;
}
});
ret.isValid = validate(obj.json);
ret.status = ret.isValid ? 'valid' : 'invalid';
if (!ret.isValid) {
errors = validate.errors;
}
} catch (_error) {
ret.ex = _error;
}
this.stat(HMEVENT.afterValidate, {
file: t,
status: ret.status,
fmt: fmt.replace('jars', 'JSON Resume'),
errors: errors
});
if (opts.assert && !ret.isValid) {
return {
fluenterror: HMSTATUS.invalid,
errors: errors
};
ret.error = _error;
}
ret.schema = ret.schema.replace('jars', 'JSON Resume');
this.stat(HMEVENT.afterValidate, ret);
return ret;
};

View File

@ -274,6 +274,7 @@ executeFail = (err) ->
if _opts.debug
msgs = require('./msg').errors;
logMsg printf M2C( msgs.exiting.msg, 'cyan' ), finalErrorCode
logMsg err.stack if err.stack
_exitCallback finalErrorCode
return

View File

@ -148,11 +148,11 @@ module.exports = class OutputHandler
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)
evt.schema = evt.schema.toUpperCase()
L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.schema)
if evt.errors
_.each evt.errors, (err,idx) ->
if evt.violations
_.each evt.violations, (err,idx) ->
L( chalk.yellow.bold('--> ') +
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() +
' ' + err.message))

View File

@ -25,7 +25,7 @@ module.exports = class ValidateVerb extends Verb
###* Validate 1 to N resumes in FRESH or JSON Resume format. ###
# Validate 1 to N resumes in FRESH or JSON Resume format.
_validate = (sources, unused, opts) ->
if !sources || !sources.length
@ -40,9 +40,8 @@ _validate = (sources, unused, opts) ->
# Validate input resumes. Return a { file: <f>, isValid: <v>} object for
# each resume valid, invalid, or broken.
results = _.map sources, (t) ->
return { } if @hasError() and opts.assert
r = _validateOne.call @, t, validator, schemas, opts
@err r.fluenterror, r if r.fluenterror
@err r.error.fluenterror, r.error if r.error
r
, @
@ -53,44 +52,48 @@ _validate = (sources, unused, opts) ->
results
###*
Validate a single resume.
@returns {
file: <fileName>,
isValid: <validFlag>,
status: <validationStatus>,
violations: <validationErrors>,
schema: <schemaType>,
error: <errorObject>
}
###
_validateOne = (t, validator, schemas, opts) ->
ret = file: t, isValid: false, status: 'unknown'
fmt = '------'
ret = file: t, isValid: false, status: 'unknown', schema: '-----'
try
# Load the input file JSON 1st
# Read and parse the resume JSON
obj = safeLoadJSON t
if obj.ex # safeLoadJSON can only return a READ error or a PARSE error
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
ret.error = fluenterror: errCode, inner: obj.ex.inner, quiet: errCode == HMSTATUS.readError
# 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})?)?$/ }
else
# Set up a FRESH or JSON Resume validator
if obj.json.basics then ret.schema = 'jars' else ret.schema = 'fresh'
validate = validator schemas[ ret.schema ], # Note [1]
formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ }
# Validate the resume against the schema
ret.isValid = validate obj.json
ret.status = if ret.isValid then 'valid' else 'invalid'
ret.violations = validate.errors if !ret.isValid
ret.isValid = validate obj.json
ret.status = if ret.isValid then 'valid' else 'invalid'
if !ret.isValid
errors = validate.errors
catch
ret.ex = _error
@stat HMEVENT.afterValidate,
file: t
status: ret.status
fmt: fmt.replace 'jars', 'JSON Resume'
errors: errors
if opts.assert and !ret.isValid
return fluenterror: HMSTATUS.invalid, errors: errors
ret.error = _error
ret.schema = ret.schema.replace 'jars', 'JSON Resume'
@stat HMEVENT.afterValidate, ret
ret