Using AJV to enforce specific elements within an array

I need to validate a JSON object by using a schema to make sure that all the elements specified in the schema are present in the array. For instance, my people array should include the following individuals:

{
  "people":[
    {
      "name":"Bob",
      "age":"26"
    },
    {
      "name":"Jim",
      "age":"35"
    },
    {
      "name":"John",
      "age":"27"
    }
  ]
}

Any extra individuals will be disregarded, but these must exist. I attempted to use allOf in the items specifier, but it didn't work because allOf requires each element to match each of the provided schemas.

{
  "people":{
    "type":"array",
    "items":{
      "allOf":[
        {
          "type":"object",
          "properties":{
            "name":{
              "type":"string",
              "const":"Bob"
            },
            "age":{
              "type":"string",
              "const":"26"
            }
          },
          "required":[
            "name",
            "age"
          ]

        },
        {
          "type":"object",
          "properties":{
            "name":{
              "type":"string",
              "const":"Jim"
            },
            "age":{
              "type":"string",
              "const":"35"
            }

          },
          "required":[
            "name",
            "age"
          ]

        },
        {
          "type":"object",
          "properties":{
            "name":{
              "type":"string",
              "const":"John"
            },
            "age":{
              "type":"string",
              "const":"27"
            }

          },
          "required":[
            "name",
            "age"
          ]

        }

      ]
    }
  }
}

From this data, it can be determined that a valid people array would consist of:

  [
    {
      "name":"Bob",
      "age":"26"
    },
    {
      "name":"Jim",
      "age":"35"
    },
    {
      "name":"John",
      "age":"27"
    }
  ]

While an invalid array would be:

  [
    {
      "name":"Bob",
      "age":"26"
    },
    {
      "name":"Jim",
      "age":"35"
    },
    {
      "name":"Tim",
      "age":"47"
    }
  ]

since John is missing from the list.

This is the code for the AJV file.

const Ajv = require('ajv');
const jsonInput = require('./people.json');
const schema = require('./peopleSchema.json');

const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(jsonInput);

console.log(valid);

if (!valid) console.log(validate.errors);

Which property should be used to specify the required elements in an array as mentioned above?

Update: In response to the comment, I attempted to incorporate 'contains' without success.

{
  "$id": "root",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "people": {
      "type": "array",
      "items": {
        "type": "object",
        "allOf": [
          {
            "contains": {
              "properties": {
                "name": {
                  "enum": ["Bob"]
                },
                "age": {
                  "enum": ["26"]
                }
              }
            }
          },
          {
            "contains": {
              "properties": {
                "name": {
                  "enum": ["John"]
                },
                "age": {
                  "enum": ["27"]
                }
              }
            }
          },
          {
            "contains": {
              "properties": {
                "name": {
                  "enum": ["Jim"]
                },
                "age": {
                  "enum": ["36"]
                }
              }
            }
          }
        ]
      }
    }
  },
  "required": ["people"]
}

Answer №1

My needs were successfully addressed by implementing the following schema.

{
  "properties": {
    "persons": {
      "items": {
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "string" }
        }
      },
      "allOf": [
        {
          "contains": {
            "const": {
                "name": "Bob",
                "color": "26"
            }
          }
        },
        {
          "contains": {
            "const": {
                "name": "Jim",
                "color": "35"
            }
          }
        },
        {
          "contains": {
            "const": {
                "name": "John",
                "color": "27"
            }
          }
        }
        ]
    }
  },
  "required": [
    "persons"
  ],
  "additionalProperties": false
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The NodeJS puppeteer encountered an error due to a timeout exceeding 30000 milliseconds

While working on a web scraper for SPA's, I encounter errors on some websites but not others. It's unclear whether the issue lies with my targeted selector, request throttling due to large data sets, or too many requests from the same IP address. ...

Step-by-step guide on utilizing PHP to retrieve and interpret JSON data

Is there a way to extract the values of "price_usd" and "price_btc" from the JSON data available at ? I've attempted to write a script for this purpose, but it doesn't seem to be working as expected. <?php $tick = file_get_contents('htt ...

Issue with Axios Get Method - Displaying Only One Result During forEach Iteration

Below is the snippet of HTML code: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>movieCard</title> <link rel="stylesheet" href="https://stac ...

Once the image is requested in HTML, three.js makes a subsequent request for the same image

This is a block of code. let image = new THREE.TextureLoader().load("http://odf9m3avc.bkt.clouddn.com/1493817789932.jpg") <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script> <img class='preLoad&apo ...

Send error messages directly to the client side or retrieve status codes and messages

When responding to an AJAX request, encountering an app error like data validation failure can be tricky. How can we effectively communicate this to the user? 1. Returning a Status Code and Fetching Message with JS $.ajax(options).done(function(response) ...

ES7 Map JSON introduces a new feature by using square brackets

Currently, I am utilizing core-js for the Map collection because it appears that ES7 Map includes a Map to JSON feature that is absent in ES6 Map. (ES6): JSON.stringify(new Map().set('myKey1', 'val123').set('myKey2', 'va ...

Exploring the power of Next.js dynamic routes connected to a Firestore collection

Currently seeking a solution to create a dynamic route that will display each document in a Firestore collection using Server-side Rendering. For instance, if there is a document named foo, it would be accessible at test.com/foo under the [doc] page compo ...

How to send a JSON object using Node.js Express 4.0

I'm currently working on parsing the JSON object sent in the request and displaying the data being transmitted. Below is my post request: $.ajax({ url: url, type: 'POST', contentType: 'application/json' ...

Retrieve or extract information stored in a JSON array as a text string

After receiving a JSON Response, it contains: [{"name":"value 1"}] In my html.twig file, I am attempting to extract this value using the following code snippet: // extracting route parameter in JavaScript var for slug and confChecked $ ...

Discovering the power of ng-change in an Angular typeahead search functionality

I am facing an issue with displaying the result list when searching for users on key press using customTemplate.js. The list is not showing up after the first key press. Below is the code I am using: <input type="text" placeholder="Search people here ...

Bringing Vue Components Together in Laravel: A Guide to Component Importation

Recently, I started learning Vue.js and I am looking to understand how to use component. Unfortunately, when I attempted to import my component into another component, it didn't work as expected. The framework I am currently using is Laravel 5.8 and I ...

What is the best method for freezing an HTML element in relation to a container using either CSS or JavaScript?

I've been diligently researching this topic up until the final day, but unfortunately, I have yet to find a solution. I am in dire need of assistance with my project. Could someone please help me out? One of my related searches led me to Can I positi ...

Nodejs client application for maintaining constant communication with an echo server

As someone who is just starting out in the world of nodejs, I recently managed to create an echo server using tutorials from YouTube. While there's nothing wrong with my server code, I am now facing the challenge of developing a client program that ca ...

I would greatly appreciate any recommendations on how to troubleshoot and

I've been working on the "Map the Debris" challenge at freecodecamp, and I'm facing an issue. While my code works fine in my PC's editor, it doesn't satisfy the conditions when I paste it into the website area. Any suggestions on how t ...

Is jQuery failing to correctly validate the RadioButtonList?

Using jQuery, I am attempting to validate a RadioButtonList to make sure that the user has selected one of the values. If none of the radio buttons are checked, an alert message should appear. However, even after selecting a radio button, the alert continu ...

What is the best way to load a model and save it to a variable for future use?

One issue I'm facing involves loading a model in this manner: var Beech; var loader = new THREE.JSONLoader(); loader.load( "./models/trees/Beech/Beech.json", function( geometry, materials ) { Beech = addMorphTree(geometry,materials,0.5); }); and uti ...

The before send function in the javascript ajax call is not being triggered

Here is the Javascript code snippet that I am working with: this.confirmBox = function(data) { $.jconfirm((function(_this) { return function() { var objConfig; objConfig = $.jconfirm("getConfig"); alert("here"); return true; ...

A guide to loading a Sencha Touch 2 list with data retrieved from a MySQL database

After successfully following this helpful tutorial (specifically focusing on the blog section), I managed to create a list that presents information from JSON. Now, my aim is to replicate a similar process but with values extracted from a MySQL table. Sp ...

How can I delay the loading of a lazy loaded module in Angular until certain data is resolved from an API call?

How can I preload data before loading a lazy module in Angular? I attempted using app_initializer, but it didn't work due to an existing app_initializer in AppModule. Is there a different approach to achieve this? ...

Retrieve the final non-null element within each collection

const totalItems = $('.item').length; $.each(JSON.parse(json), function(index, object) { const itemId = object.itemId; if (itemId !== null && index !== totalItems - 1){ const temp = &apo ...