From c889664c31aa65067009b54715ccd2305aef4837 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Fri, 12 Feb 2016 23:47:08 -0500 Subject: [PATCH] More VALIDATE fixups. --- dist/cli/main.js | 3 ++ dist/cli/out.js | 8 ++-- dist/verbs/validate.js | 78 +++++++++++++++++++-------------------- src/cli/main.coffee | 1 + src/cli/out.coffee | 8 ++-- src/verbs/validate.coffee | 57 ++++++++++++++-------------- 6 files changed, 80 insertions(+), 75 deletions(-) diff --git a/dist/cli/main.js b/dist/cli/main.js index faea6f6..44b48c8 100644 --- a/dist/cli/main.js +++ b/dist/cli/main.js @@ -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); }; diff --git a/dist/cli/out.js b/dist/cli/out.js index 8b6d7c1..9e1adae 100644 --- a/dist/cli/out.js +++ b/dist/cli/out.js @@ -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); } diff --git a/dist/verbs/validate.js b/dist/verbs/validate.js index 9b86409..4a11eb9 100644 --- a/dist/verbs/validate.js +++ b/dist/verbs/validate.js @@ -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: , + isValid: , + status: , + violations: , + schema: , + error: + } + */ + _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; }; diff --git a/src/cli/main.coffee b/src/cli/main.coffee index 52318a2..5f23162 100644 --- a/src/cli/main.coffee +++ b/src/cli/main.coffee @@ -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 diff --git a/src/cli/out.coffee b/src/cli/out.coffee index 0cc0859..1065188 100644 --- a/src/cli/out.coffee +++ b/src/cli/out.coffee @@ -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)) diff --git a/src/verbs/validate.coffee b/src/verbs/validate.coffee index 277457b..4ef892e 100644 --- a/src/verbs/validate.coffee +++ b/src/verbs/validate.coffee @@ -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: , isValid: } 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: , + isValid: , + status: , + violations: , + schema: , + error: +} +### _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