I have a service that returns an array of hashes, the order of which is non-deterministic. I need to validate that there exists one hash that has a certain key/value, and that hash is populated with data, but the rest of the hashes I don't care about their data. For example, if the service returns this:
[
{
"key":"meaningless1",
"data": {
}
},
{
"key":"meaningless2",
"data": {
"some": "data",
"goes": ["here"]
}
},
{
"key":"meaningful",
"data": {
"regex": "value",
"integer": 1,
"boolean": true
}
}
]
I want to validate that in the array, there is a hash that has "key":"meaningful"
, and that the data
hash in that hash has a key of regex
with a value that matches a regex, a key of integer
that is an integer, and a key of boolean
that is a boolean. I don't care if the other hashes have data at all, or if the data they have matches this schema.
I can't use EachLike
, because that will verify the schema against all the hashes, not just the one with the right key
. I also tried something like this:
expected = [
{
"key":"meaningful",
"data":{
"regex":Term("v.*", "value"),
"integer":Like(1),
"boolean":Like(True)
}
}
]
But it simply tried to verify that against the 0th element in the resulting array, which means it tried to verify against:
{
"key":"meaningless1",
"data": {
}
},
Is what I want possible?