Skip to content

Commit

Permalink
Assume Joi returns first error (it's default)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Nov 20, 2019
1 parent bec09a4 commit 9812873
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 18 deletions.
4 changes: 1 addition & 3 deletions src/services/joi_validation_error_converter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const ValidationError = require('../errors/validation')

module.exports = (error, pathPrefix = null) => {
const detail = error.details.reduce((mostSpecific, detail) =>
mostSpecific.path.length >= detail.path.length ? mostSpecific : detail
)
const detail = error.details[0]

let path = detail.path.reduce(
(accumulator, value, index) =>
Expand Down
15 changes: 0 additions & 15 deletions test/src/services/joi_validation_error_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ test('uses optional path prefix as path if no path exists', () => {
)
})

test('uses the most specific error detail (by path length)', () => {
const { error } = Joi.alternatives()
.try(
Joi.string(),
Joi.object().keys({
invalid: Joi.number(),
})
)
.validate({ invalid: 'invalid' })

expect(joiValidationErrorConverter(error)).toEqual(
new ValidationError('invalid must be a number')
)
})

test('delineates path segments with []', () => {
const { error } = Joi.object()
.keys({
Expand Down

0 comments on commit 9812873

Please sign in to comment.