@architect/parser
Advanced tools
Comparing version 2.1.1-RC.0 to 2.1.1-RC.1
{ | ||
"name": "@architect/parser", | ||
"version": "2.1.1-RC.0", | ||
"version": "2.1.1-RC.1", | ||
"description": "Architect Parser accepts plaintext, JSON, or YAML .arc manifests and returns a plain JavaScript Object", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -6,3 +6,3 @@ let Ajv = require('./ajv-6.10.2') | ||
* @param {object} arc | ||
* @returns {boolean|array} returns either 'false' or an array of errors | ||
* @returns {boolean|string} returns either 'false' or a string of errors | ||
*/ | ||
@@ -12,3 +12,55 @@ module.exports = function validate(arc) { | ||
let valid = defn.validate(schema, arc) | ||
return valid? false : defn.errors | ||
let isValid = valid && !defn.errors | ||
let isInvalid = !valid && defn.errors | ||
if (isValid) { | ||
return false | ||
} | ||
else if (isInvalid) { | ||
let message = ['Architect schema validation error'] | ||
let unknownErrors = 0 | ||
let findPragma = /\w+(?=\[)/ | ||
let findPosition = /(?<=[\[])\d+(?=[\]])/ | ||
let pragmas = { | ||
app: 'app name', | ||
aws: 'setting', | ||
events: 'event', | ||
http: 'route', | ||
indexes: 'index or key', | ||
macros: 'macro', | ||
queues: 'event', | ||
scheduled: 'event', | ||
static: 'setting', | ||
tables: 'table or key', | ||
ws: 'route', | ||
} | ||
for (let error of defn.errors) { | ||
try { | ||
// Suss out the pragma and position of the offending statement | ||
let pragma = error.dataPath.match(findPragma)[0] | ||
let position = error.dataPath.match(findPosition)[0] | ||
let offender = arc[pragma][position] | ||
// Depending on what the user did we may get strings, arrays, or objects | ||
if (Array.isArray(offender)) { | ||
offender = offender.join(' ') | ||
} | ||
else if (offender instanceof Object) { | ||
offender = Object.keys(offender)[position] | ||
} | ||
let msg = ` @${pragma} invalid ${pragmas[pragma]}: "${offender}"` | ||
if (!message.includes(msg)) message.push(msg) | ||
} | ||
catch (err) { | ||
++unknownErrors | ||
} | ||
} | ||
if (message.length > 2) message[0] += 's' | ||
if (unknownErrors) message.push(` ${unknownErrors} unknown validation error${unknownErrors > 1 ? 's' : ''}`) | ||
return message.join('\n') | ||
} | ||
// Shouldn't be possible but jic ajv does something funky | ||
else throw Error('Invalid error reporting state') | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
479040
11815