mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-03 04:47:07 +01: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:
@ -43,6 +43,8 @@ events:
|
||||
- "VALID!"
|
||||
- "INVALID"
|
||||
- "BROKEN"
|
||||
- "MISSING"
|
||||
- "UNKNOWN"
|
||||
beforePeek:
|
||||
msg:
|
||||
- Peeking at **%s** in **%s**
|
||||
|
@ -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' )
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user