1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-04-19 14:20:25 +01:00
HackMyResume/src/verbs/validate.coffee
hacksalot 7a60cd0bab Fixup VALIDATE command.
Introduce MISSING and UNKNOWN states alongside BROKEN, VALID, and
INVALID and fix regressions introduced in previous refactorings.
2016-02-12 22:49:56 -05:00

97 lines
2.5 KiB
CoffeeScript

###*
Implementation of the 'validate' verb for HackMyResume.
@module verbs/validate
@license MIT. See LICENSE.md for details.
###
FS = require 'fs'
ResumeFactory = require '../core/resume-factory'
SyntaxErrorEx = require '../utils/syntax-error-ex'
chalk = require 'chalk'
Verb = require '../verbs/verb'
HMSTATUS = require '../core/status-codes'
HMEVENT = require '../core/event-codes'
_ = require 'underscore'
safeLoadJSON = require '../utils/safe-json-loader'
###* An invokable resume validation command. ###
module.exports = class ValidateVerb extends Verb
constructor: -> super 'validate', _validate
###* Validate 1 to N resumes in FRESH or JSON Resume format. ###
_validate = (sources, unused, opts) ->
if !sources || !sources.length
@err HMSTATUS.resumeNotFoundAlt, quit: true
return null
validator = require 'is-my-json-valid'
schemas =
fresh: require 'fresca'
jars: require '../core/resume.json'
# 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
r
, @
if @hasError() and !opts.assert
@reject @errorCode
else if !@hasError()
@resolve results
results
_validateOne = (t, validator, schemas, opts) ->
ret = file: t, isValid: false, status: 'unknown'
fmt = '------'
try
# 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
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