Looking for a way to verify the presence of a value within another properties' JSON schema?

Is it possible to validate if a value exists in other properties in the JSON file, rather than the schema file? I've tried searching for keywords like "lookup" or "reference", but all results refer to the schema file, not the JSON data.

Here is the schema:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "title": "List of people cars",
  "properties": {
    "car":{
      "type":"array",
      "items": {
        "properties": {
          "name": {
            "type":"string"
          }
        }
      }
    },
    "people": {
      "type":"array",
      "items":{
        "properties": {
          "fullname":{
            "type":"string"
          },
          "cars":{
            "type":"string"
            // somehow validate if the car exists in the car properties
          }
        }
      }
    }
  }
}

Here is the JSON data:

{
  "$schema": "./schema.json",
  "car": [
    {
      "name": "Lamborghini"
    }
  ],
  "people": [
    {
      "fullname": "Ucok",
      "cars": "Lamborghini" //check if the car name exists in the car properties
    }
  ]
}

I hope to have the "cars" property value in the people object autocomplete from the list of cars listed above.

Answer №1

To determine if a value exists using JavaScript, you can utilize the find / findIndex functions.

let data = {
  "$schema": "./schema.json",
  "car": [
    {
      "name": "Lamborghini"
    }
  ],
  "people": [
    {
      "fullname": "Ucok",
      "cars": "Lamborghini" //check if the car name exist in the car properties
    }
  ]
};

data.car.find((item) => item.name === 'Lamborghini'); // will return {name: 'Lamborghini'}
data.car.find((item) => item.name === 'Ferrari'); // will return undefined

data.car.findIndex((item) => item.name === 'Lamborghini'); // will return 0
data.car.findIndex((item) => item.name === 'Ferrari'); // will return -1

If the return value is undefined or -1, then the value does not exist.

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

Challenges in the Knockout Framework Due to Synchronization Issues

Recently, I've encountered a slight problem with race conditions in my code. Utilizing Knockout.Js to collect information for user display has been the cause. The issue arises when a dropdown needs to be created before a value can be selected. Typica ...

The alterations made to a single dropdown option are causing ripple effects across all other dropdowns

There is an add button that continuously adds a div container containing two dropdowns. The selection in one dropdown affects the data in the other dropdown. When the add button is clicked, a second div with both dropdowns is added. However, changing the ...

Retrieving a particular data field from JSON output through jq

Below is a JSON output: { "example": { "sub-example": [ { "name": "123-345", "tag" : 100 }, { "name": "234-456", "tag" : 100 }, { "name": "4a7-a07a5", "tag" : 100 } ...

What is the method for sorting flowfiles by their contained data?

Within a single output flowfile, there are 23 rows of data, each with a key and value. If any similar flowfiles in the queue do not contain all the necessary rows of data, they should be directed to the unmatched queue. What processor or method would be ...

To display a pattern with a series of alternating addition and subtraction operators, starting from -1 and ending at n, use JavaScript to iterate through the numbers and implement the pattern -1+

I've been struggling to locate the values despite numerous attempts. What steps can I take to resolve this issue? Below is my code snippet: var numVal = prompt(""); for (var i = 1; i <= numVal; i++) { if (i % 2 !== 0) { console.log("-"); ...

Modify the text of a button using JavaScript without referencing a specific div class

THE ISSUE I'm facing a challenge in changing the text of a button on my website. The <div> I need to target doesn't have a specific class, making it difficult for me to make this edit. While I have some basic understanding of JavaScript, ...

Having trouble installing the gem json version 1.6.3

Encountering issues with building native extensions. The process might take longer... ERROR: While trying to install json, an error occurred: ERROR: Unable to build gem native extension. /Users/..../.rvm/rubies/ruby-1.9.2-p180/bin/ruby extconf.rb ...

Exploring the dynamic duo of Github and vue.js

As I am new to programming and currently learning JavaScript and Vue.js, I have been trying to deploy my first Vue.js app without success. Despite spending hours on it, I still cannot figure it out and need to seek assistance. Every time I try to deploy, ...

Unable to import necessary modules within my React TypeScript project

I am currently building a React/Express application with TypeScript. While I'm not very familiar with it, I've decided to use it to expand my knowledge. However, I've encountered an issue when trying to import one component into another comp ...

Connect the plotly library to interact with the data in a Vue.js application through

I'm currently working on a project that involves using vue.js and the plot.ly JavaScript graph library. I am trying to figure out how to bind "pts" to the data's "TestSentences" in Vue. Below is my code snippet, thanks to everyone who has provide ...

Unable to successfully change the Span Class

My webpage has the code snippet below, but it's not functioning as expected. I have tried two methods to change the span's class attribute, but they aren't working. Could someone please help me identify where the issue lies? :) <script l ...

Tips for making an AJAX request using jQuery

I have a new project that involves collecting user data and displaying it on the same page. I was able to successfully implement an Ajax call using JavaScript, but now I want to transition to using jQuery. Below is my JavaScript code: var output1 = docum ...

What steps should be taken to validate a condition prior to launching a NextJS application?

In my NextJS project (version 13.2.4), I usually start the app by running: npm run dev But there's a new requirement where I need to check for a specific condition before starting the app. If this condition is not met, the app should exit. The condi ...

Determine the number of elements chosen within a complex JSON structure

I need to figure out how to count the length of a jsonArray, but I'm stuck. Here's an example to start with: https://jsfiddle.net/vuqcopm7/13/ In summary, if you click on an item in the list, such as "asd1", it will create an input text every t ...

Mutex in node.js(javascript) for controlling system-wide resources

Is there a way to implement a System wide mutex in JavaScript that goes beyond the usual mutex concept? I am dealing with multiple instances of a node.js cmd running simultaneously. These instances are accessing the same file for reading and writing, and ...

Choosing nested JSON elements to populate selection menus

Here is an example of JSON data format: "games": [{ "gameType": "RPG", "publishers": [{ "publisher": "Square", "titles": [{ "title": "Final Fantasy", "gameReleases": [ 2006, 2008, 2010, 2012, 2013, 2014 ] ...

Having trouble getting my list items to display on individual lines within the foreach loop. It just doesn't seem to be working as expected

In the event listener, I need to ensure that my list items within the forEach loop are not displaying on separate lines. This issue is causing a problem in a lengthy section of code. The goal is to update questions when an answer is clicked from a list. B ...

Tips for confirming schedule accuracy

Trying to determine if a specific time falls between two others is the task at hand. Allow me to illustrate: Presently, it's Thursday, and the time reads 11:39 PM. Establishment X operates from 12:00 AM to 11:59 PM on Thursdays (a regular occurrence ...

At what point is the args variable required by Dojo?

There comes a point when passing the args variable to an anonymous function in Dojo becomes necessary, even though the function itself may not visibly need it. This can lead to confusion as there is no clear indication on the Dojo help page regarding whe ...

Using jQuery and Laravel framework to handle a POST request

Could someone assist me with troubleshooting a jQuery post request issue? I suspect there may be an error with the routes or controller. Here is my JavaScript code for the post request: $.post('http://localhost:8000/ajax', { ...