joifrisby.js

Validate referential integrity of object arrays with Joi


I'm trying to validate that the data I am returned it sensible. Validating data types is done. Now I want to validate that I've received all of the data needed to perform a task.

Here's a representative example:

{
  "things": [
    {
      "id": "00fb60c7-520e-4228-96c7-13a1f7a82749",
      "name": "Thing 1",
      "url": "https://lolagons.com"
    },
    {
      "id": "709b85a3-98be-4c02-85a5-e3f007ce4bbf",
      "name": "Thing 2",
      "url": "https://lolfacts.com"
    }
  ],
  "layouts": {
    "sections": [
       {
          "id": "34f10988-bb3d-4c38-86ce-ed819cb6daee",
          "name": "Section 1",
          "content:" [
             {
               "type": 2,
               "id": "00fb60c7-520e-4228-96c7-13a1f7a82749" //Ref to Thing 1
             }
          ]
       }
     ]
  }
}

So every Section references 0+ Things, and I want to validate that every id value returned in the Content of Sections also exists as an id in Things.

The docs for Object.assert(..) implies that I need a concrete reference. Even if I do the validation within the Object.keys or Array.items, I can't resolve the reference at the other end.

Not that it matters, but my context is that I'm validating HTTP responses within IcedFrisby, a Frisby.js fork.


Solution

  • This wasn't really solveable in the way I asked (i.e. with Joi).

    I solved this for my context by writing a plugin for icedfrisby (published on npm here) which uses jsonpath to fetch each id in Content and each id in Things. The plugin will then assert that all of the first set exist within the second.