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

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) { if (_opts.debug) {
msgs = require('./msg').errors; msgs = require('./msg').errors;
logMsg(printf(M2C(msgs.exiting.msg, 'cyan'), finalErrorCode)); logMsg(printf(M2C(msgs.exiting.msg, 'cyan'), finalErrorCode));
if (err.stack) {
logMsg(err.stack);
}
} }
_exitCallback(finalErrorCode); _exitCallback(finalErrorCode);
}; };

8
dist/cli/out.js vendored
View File

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

View File

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

View File

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

View File

@ -148,11 +148,11 @@ module.exports = class OutputHandler
when 'invalid' then style = 'yellow'; adj = msgs[2] when 'invalid' then style = 'yellow'; adj = msgs[2]
when 'broken' then style = 'red'; adj = msgs[3] when 'broken' then style = 'red'; adj = msgs[3]
when 'missing' then style = 'red'; adj = msgs[4] when 'missing' then style = 'red'; adj = msgs[4]
evt.fmt = evt.fmt.toUpperCase() evt.schema = evt.schema.toUpperCase()
L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.fmt) L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.schema)
if evt.errors if evt.violations
_.each evt.errors, (err,idx) -> _.each evt.violations, (err,idx) ->
L( chalk.yellow.bold('--> ') + L( chalk.yellow.bold('--> ') +
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() + chalk.yellow(err.field.replace('data.','resume.').toUpperCase() +
' ' + err.message)) ' ' + 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) -> _validate = (sources, unused, opts) ->
if !sources || !sources.length if !sources || !sources.length
@ -40,9 +40,8 @@ _validate = (sources, unused, opts) ->
# Validate input resumes. Return a { file: <f>, isValid: <v>} object for # Validate input resumes. Return a { file: <f>, isValid: <v>} object for
# 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
r = _validateOne.call @, t, validator, schemas, opts r = _validateOne.call @, t, validator, schemas, opts
@err r.fluenterror, r if r.fluenterror @err r.error.fluenterror, r.error if r.error
r r
, @ , @
@ -53,44 +52,48 @@ _validate = (sources, unused, opts) ->
results results
###*
Validate a single resume.
@returns {
file: <fileName>,
isValid: <validFlag>,
status: <validationStatus>,
violations: <validationErrors>,
schema: <schemaType>,
error: <errorObject>
}
###
_validateOne = (t, validator, schemas, opts) -> _validateOne = (t, validator, schemas, opts) ->
ret = file: t, isValid: false, status: 'unknown' ret = file: t, isValid: false, status: 'unknown', schema: '-----'
fmt = '------'
try try
# Load the input file JSON 1st # Read and parse the resume JSON
obj = safeLoadJSON t 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' if obj.ex.operation == 'parse'
errCode = HMSTATUS.parseError errCode = HMSTATUS.parseError
ret.status = 'broken' ret.status = 'broken'
else else
errCode = HMSTATUS.readError errCode = HMSTATUS.readError
ret.status = 'missing' 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. else
if obj.json.basics then fmt = 'jars' else fmt = 'fresh' # Set up a FRESH or JSON Resume validator
errors = [] if obj.json.basics then ret.schema = 'jars' else ret.schema = 'fresh'
validate = validator schemas[ fmt ], # Note [1] validate = validator schemas[ ret.schema ], # 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})?)?$/ }
# 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 catch
ret.ex = _error ret.error = _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.schema = ret.schema.replace 'jars', 'JSON Resume'
@stat HMEVENT.afterValidate, ret
ret ret