Skip to content

Commit 4326205

Browse files
committed
feat: provide API compatible errors for API
- instead of using cli based error format for API calls, use js format unless cli formatting is requested Signed-off-by: Michael Dawson <[email protected]>
1 parent 432dc3e commit 4326205

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ const supportElementSchema = module.exports.schema = require('./support-element-
1616

1717
module.exports.defaultPackageSupport = 'package-support.json'
1818

19-
module.exports.validateSupportElement = (obj) => {
19+
function getBetterAjvErrorsOptions (cli) {
20+
const options = { format: 'js' }
21+
if (cli === true) {
22+
options.format = 'cli'
23+
}
24+
return options
25+
}
26+
27+
module.exports.validateSupportElement = (obj, cli = false) => {
2028
if (!ajvElement) {
2129
ajvElement = new Ajv({ jsonPointers: true })
2230
compiledSchemaElement = ajvElement.compile(supportElementSchema)
@@ -26,14 +34,14 @@ module.exports.validateSupportElement = (obj) => {
2634
if (!validates) {
2735
const err = new Error('Validation Failed')
2836
err.validationErrors = compiledSchemaElement.errors
29-
err.prettyValidationErrors = betterAjvErrors(supportElementSchema, obj, compiledSchemaElement.errors)
37+
err.prettyValidationErrors = betterAjvErrors(supportElementSchema, obj, compiledSchemaElement.errors, getBetterAjvErrorsOptions(cli))
3038
err.validationSchema = compiledSchemaElement.schema
3139
throw err
3240
}
3341
return true
3442
}
3543

36-
module.exports.validate = (obj) => {
44+
module.exports.validate = (obj, cli = false) => {
3745
if (!ajv) {
3846
ajv = new Ajv({ jsonPointers: true })
3947
compiledSchema = ajv.compile(schema)
@@ -43,7 +51,7 @@ module.exports.validate = (obj) => {
4351
if (!validates) {
4452
const err = new Error('Validation Failed')
4553
err.validationErrors = compiledSchema.errors
46-
err.prettyValidationErrors = betterAjvErrors(schema, obj, compiledSchema.errors)
54+
err.prettyValidationErrors = betterAjvErrors(schema, obj, compiledSchema.errors, getBetterAjvErrorsOptions(cli))
4755
err.validationSchema = compiledSchema.schema
4856
throw err
4957
}

lib/cli/commands/validate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function (opts) {
3434
return
3535
} else {
3636
try {
37-
support.validateSupportElement({ support: supportData })
37+
support.validateSupportElement({ support: supportData }, true)
3838
} catch (e) {
3939
console.log(errorInSupportElement)
4040
console.log(e.prettyValidationErrors)
@@ -51,7 +51,7 @@ module.exports = function (opts) {
5151
if (supportData.resolved) {
5252
const supportDataJSON = JSON.parse(supportData.contents)
5353
try {
54-
const result = support.validate(supportDataJSON)
54+
const result = support.validate(supportDataJSON, true)
5555
if (result) {
5656
argv.log.info('Your support information is valid!')
5757
return

0 commit comments

Comments
 (0)