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}, schema, {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.
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}, schema, {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.
Share Improve this question edited Apr 5, 2017 at 4:55 Phenelo asked Apr 5, 2017 at 4:41 PheneloPhenelo 1051 gold badge1 silver badge8 bronze badges2 Answers
Reset to default 2Well, I think I found the answer. My joi library wasn't updated so I had it rolled up to 10.4.1 from 10.2.x. There was some features that I've seen in the documentation that didn't work when I tried it in the older version, including the solution I did.
I tried using this pattern and it works:
const schema = Joi.object().keys({
a: Joi.string().error(() => 'must be string'),
b: Joi.number().error(() => 'must be number')
});
Like so:
{ [ValidationError: child "a" fails because [must be string]. child "b" fails because [must be number]]
isJoi: true,
name: 'ValidationError',
details:
[ { message: '"a" must be a string',
path: 'a',
type: 'string.base',
context: [Object] },
{ message: '"b" must be a number',
path: 'b',
type: 'number.base',
context: [Object] } ],
_object: { a: 1, b: false },
annotate: [Function] }
Then I'll just parse the error.message to get all the error messages and process it.
'child "a" fails because [must be string]. child "b" fails because [must be number]'
I have another way to check each validation errors as well. If you have one validation, it doesn't matter what condition would be, you can do like this :
username: Joi.string() // It has to be string
.label("Username") // The label
.error(new Error('It is whatever error')) // Custom general error
You can do this with arrow function too :
username: Joi.string() // It has to be string
.label("Username") // The label
.error(() => 'It is whatever error') // Custom general error
But If there are some validation params and errors we have these solutions :
password: Joi.string() // It has to be string
.min(8) // It has to have at least 8 characters
.required() // It has to have a value
.label("Password") // The label
.error(errors => {
return errors.map(err => { // Here we map the errors (ES6) discover within an array
if (err.type === "string.min") { // Check the type of error e.g: 'string.min,any.empty,number.min,...'
return { message: "The length of the parameter should be more than 8 characters" }; // Which message we want to display
} else {
return { message: "another validation error" };
}
});
})
There is another solution with Switch Case :
password: Joi.string() // It has to be string
.min(8) // It has to have at least 8 characters
.required() // It has to have a value
.label("Password") // The label
.error(errors => {
return errors.map(err => { // Here we map the errors (ES6) discover within an array
switch (err.type) {
case "string.min":
return { message: "The length of the parameter should be more than 8 characters" }; // Which message we want to display
case "any.empty":
return { message: "The parameter should have a value" };
}
});
})
Please noticed that if one of the validation errors did not specify you have error that err.type is undefined.
you can use err.context within the message to display the label, limit, max , ... in dynamic :
message: `${err.context.label} must have at least ${err.context.limit} characters.`
refrence : Joi Document
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745090168a4610648.html
评论列表(0条)