Skip to content

how to concat array of objects inside an object property #561

@Maxlufs

Description

@Maxlufs

Concat'ing arrays of primitive types: (OK)

var s1 = Joi.array().includes(Joi.number());
var s2 = Joi.array().includes(Joi.string());

var schema = s1.concat(s2);
Joi.validate([1,"a"], schema); 
// works fine. allow array of mixed of number and string

Concat'ing arrays of objects: (OK)

var s1 = Joi.array().includes(Joi.object().keys({
    "x": Joi.any()
}));
var s2 = Joi.array().includes(Joi.object().keys({
    "y": Joi.any()
}));

var schema = s1.concat(s2);
Joi.validate([{
    "x":1, 
    "y":2
}], schema);
// works fine. allow array of objects that contain both "x" and "y" properties

Concat'ing objects of arrays of objects: (not OK)

var s1 = Joi.object().keys({
    "prop": Joi.array().includes(Joi.object().keys({
        "x": Joi.any()
    }))
});
var s2 = Joi.object().keys({
    "prop": Joi.array().includes(Joi.object().keys({
        "y": Joi.any()
    }))
});

var schema = s1.concat(s2);
Joi.validate([{
    "prop": []
}], schema); 
// works fine

Joi.validate([{
    "prop": [{
        "x": 1
    }]
}], schema); 
// Uh-oh, prop at position 0 fails because x is not allowed

I can work around it by concat'ing the inner object first then include it into a schema of arrays
Is there a way to deep concat like this using built-in Joi.concat?

Thanks

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing codebugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions