hi,
I just switched from joi to yup, and was wondering if there was a way to get _all_ the validation errors – not only the first one.
var yup = require('yup');
var schema = yup.object().shape({
age: yup.number().required(),
name: yup.string().required()
});
schema.validate({})
.catch(function(e) {
console.log(e);
});
only complains about age being required. whereas joi's result.error.details gives me all the errors:
const result = joi.validate({}, schema);
result.error.details
.forEach(function(detail) {
// ...
});
its not super visible but you can pass in an abortEarly: false as an option to validate to get all the errors;
https://github.com/jquense/yup#mixedvalidatevalue-any-options-object-callback-function-promiseany-validationerror
cool, thx!
With a single error, I can get the path that failed with error.path. With abortEarly: false the path will be undefined.
What I really expected to see was an error map. Is there such a thing?
the error is an aggregate one, check it's 'inner' property which has the errors for each failed test and path
I know this is a really old ticket, but I haven't had any luck.
const valid = AddChhandTypeSchema.isValid(
{
unicode,
gurmukhiScript,
english,
},
{ abortEarly: false }
).then(function (res, error) {
// Only a Boolean is returned
});
Whose inner property am I supposed to check? I only get back a boolean.
This is version: 0.29.3
Whose
innerproperty am I supposed to check? I only get back a boolean.
So I kind of got it, but this is really dirty to work with. I had to use the validateSync instead of the isValid.
const isValidInput = async () => {
try {
const valid = await AddChhandTypeSchema.validateSync(
{
unicode,
gurmukhiScript,
english,
},
{ abortEarly: false }
);
debugger;
} catch (error) {
// error.inner
}
};
Which gives you something like:
[
{
"name": "ValidationError",
"value": "Kabitt",
"path": "unicode",
"type": "isGurmukhi",
"errors": [
"Must be Gurmukhi Unicode"
],
"inner": [],
"message": "Must be Gurmukhi Unicode",
"params": {
"path": "unicode",
"value": "Kabitt",
"originalValue": "Kabitt"
}
}
]
Which leaves it to us to have to reformat into something practical, like how @kendallroth did it.
https://github.com/jquense/yup/issues/111#issuecomment-369966425
const yup = require('yup');
const schema = yup.object().shape({
age: yup.number().required('This is required.'),
name: yup.string().required('This is required.')
});
schema.validate(data, { abortEarly: false }).then(function() {
// Success
}).catch(function (err) {
err.inner.forEach(e => {
console.log(e.message, e.path));
});
});
Most helpful comment
its not super visible but you can pass in an
abortEarly: falseas an option to validate to get all the errors;https://github.com/jquense/yup#mixedvalidatevalue-any-options-object-callback-function-promiseany-validationerror