Tuesday, 4 April 2017

joi: Custom errors are not returned, abortEarly is set to false

I can't get this joi validation to return all the errors just like what it does with default errors.

So here I'm setting individual custom errors for each field:

const schema = Joi.object().keys({
    a: Joi.string().error(new Error('must be string')), 
    b: Joi.number().error(new Error('must be number'))
});

Then when validating with abortEarly set to false, it only returns the first error it will encounter.

Joi.validate({a: 1, b: false}, x, {abortEarly: false})

The error returned is like this,

{ error: [Error: must be string], value: { a: 1, b: false }}

when it should be returning all the errors in some manner.

Am I using abortEarly incorrectly or is there a process needed to be done in returning all custom errors? Thanks in advance for any response.



via Phenelo

No comments:

Post a Comment