This means that as long as the array contains at least one valid item the validation will succeed. But if the array only contains invalid items the validation will fail.
The readme states: _stripUnknown - when true, unknown keys are deleted (only when value is an object)._
Example:
// This is the scenario that tripped me up. I expected object keys to be stripped but not arrays.
var schema = joi.array().items(joi.object().required().keys({
name: joi.string().required()
}));
var options = { stripUnknown: true };
var result = joi.validate([
{ name: 'a' },
{ name: 1 }, // <-- invalid name
{ name: 'b', extra_key: 1 }
], schema, options);
// result.value: [ { name: 'a' }, { name: 'b' } ]
My suggestion is to add an stripInvalidItems option for stripping invalid items from arrays and keeping stripUnknown the way the readme describes it.
This seems related to #559 but .required() on the array item does not seem to have any effect.
The readme was wrong (and now fixed), it was indeed the intended behavior, to make stripUnkown coherent across both type of objects, it has nothing to do with required.
You can always change that behavior in your object hierarchy with .options() but it will be used for children so you'd have to set it on the array and reset it on each of the items if you so desire. It's not very elegant but it's still a mystery to me why an unknown key would be ok to remove but an unkown element in an array would not be, so I'm still waiting for a compelling argument to change my mind.
This is still very relevant, I want to see an error for a object that is invalid in my array, vs removing an object from my array when using stripUnknown, it just removes the object.
If I understand you correctly, this is already supported with { stripUnknown: { arrays: false, objects: true } }.
Exactly! Had the same issue. My main concern is that it should at least print an error and treat it as error, but it just swallowed it and stripped my array values, and now users are losing data because of that!:)
{ stripUnknown: { arrays: false, objects: true } } works as a workaround, but I'd still like to control when we want to throw an error on invalid array items! Now seems like there's no way to throw such error?
It is doing what you told it to do 馃し If you're using stripUnknown (or any feature), I'm going to assume you at the very least read the docs. If you think the docs are unclear, I'm open to PRs. Note that the default behavior on arrays is going to change on the next major.
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
If I understand you correctly, this is already supported with
{ stripUnknown: { arrays: false, objects: true } }.