1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/tests/helpers/schemas.js
2023-03-08 23:14:47 +00:00

46 lines
1.6 KiB
JavaScript

// lightly format the schema errors object returned from ajv to connect the
// error message to where the problem is -- for example, if a top level 'date'
// property isn't correctly formatted as a date we return:
//
// at 'date': must match format "date"
//
// if sections > features has an array of objects that must have a 'notes'
// property and we misspell the property name in the first item:
//
// at 'sections > features > item 0': must have required property 'notes'
export const formatAjvErrors = (errors = []) => {
return errors
.map((errorObj) => {
// ajv instancePath tells us in the data we're checking where there was a
// schema error -- for release notes looks like this for example
// `/sections/features/0` if the error is in the first feature under sections.
const split = errorObj.instancePath.split('/')
split.shift()
if (split.length === 0) {
return `at '/' (top-level): ${errorObj.message}`
}
const schemaErrorPath = split
.map((item) => {
if (!isNaN(item)) {
return `item ${item}`
} else {
return item
}
})
.join(' > ')
// handle additional properties error specifically since we can call out
// which property shouldn't be there
let additionalProperties = ''
if (errorObj.keyword === 'additionalProperties') {
additionalProperties = `: additional property is '${errorObj.params.additionalProperty}'`
}
return `at '${schemaErrorPath}': ${errorObj.message}${additionalProperties}`
})
.join('\n ')
}