What is preferable: defining JSON schema properties or utilizing oneOf conditions within an array of objects

I am looking to represent a variety of objects in a schema through an array called contents. This array can include any number of elements, but they must fall into one of two categories: one type represents text while the other type represents images.

Up until now, I have struggled to correctly enforce the validation. It appears that using (nested) required within a oneOf does not work as expected. I have also experimented with using definitions, but it seems that approach is not quite suitable.

Despite trying out several online validators, they seem to permit me to include invalid values in the item-2 value object. The value property appears to be the main hindrance here. Unfortunately, due to legacy constraints, I am constrained to maintaining this as an object within an array.

Is there a way to validate and uphold the correct type and requirements for this object?

(this is not the schema, but the data itself. Unfortunately, the original json layout was designed using the keyword type!)

{
  "uuid":"780aa509-6b40-4cfe-9620-74a9659bfd59",
  "contents":
    [
      {
        "name":"item-1",
        "label":"My editable text Label",
        "value":"This text is editable",
        "type":"text"
      },
      {
        "name":"item-2",
        "label":"My editable image label",
        "index":0,
        "type":"image",
        "value":
        [
          {
            "name":"1542293213356.png",
            "rect":[0,0,286,286]
          }
        ]
      }
    ],
  "version":"2.0"
}

Answer №1

It seems like this is the final version, even though the online validator may not always be completely reliable. Changing the values doesn't consistently invalidate the object.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",

    "properties": {
        "uuid": { "type": "string" },
        "version": { "type": "string" },
        "contents": {
            "$ref": "#/definitions/contents" 
        },
    },
    "required": ["uuid", "version"],  

  "definitions": {

    "image": {
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "label": { "type": "string" },
            "type": { "enum": ["image", "text"] },
            "value": { "type": "object" }
        },
        "required": ["name", "label", "type", "value"]
    },

    "text": {
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "label": { "type": "string" },
            "type": { "enum": ["image", "text"] },
            "value": { "type": "string" }
        },
        "required": ["name", "label", "type", "value"]
    },

    "contents": {
        "type": "array",
        "contains": { 
            "oneOf": [
                { "$ref": "#/definitions/image" },
                { "$ref": "#/definitions/text" }
            ]
        }, 
    },

  },
}

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

Unable to display modal pop-up in ASP.NET Core MVC web application

I have developed a web application using ASP.NET CORE MVC. I encountered an unusual issue while trying to display a modal popup using JQuery. The div structure I am working with is as follows: <div class="modal fade" tabindex="-1" r ...

AngularJS views malfunctioning following oauth redirect

I am in the process of creating a web application using AngularJS and Firebase. Recently, I added a second page along with an ng-view to my index file. In order to facilitate login via Facebook or Google, I am utilizing the $firebaseAuth service. However, ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...

Understanding how to extract inner text and splitting it can be a challenging task for developers when working with Javascript

Exploring the following basic html code: <div id="content"> This is a test </div> I am puzzled as to why this works: $(function(){ text = 'this is a text'; word = text.split(' '); alert(word[1]) }) while this does not: ...

Is there a way to verify if an element possesses a specific style, and if so, alter the style of a different element accordingly?

Could you assist me in achieving a similar effect as demonstrated below: I have two menus (http://osiyo-nur.uz/osiyo-nur.uz/test6/), one of which is initially invisible. When I hover over the first menu, the second menu becomes visible with an overlay eff ...

Creating input fields in Vue 3: Best practices

I am looking to create an input field that automatically removes entered characters if they do not match a specific pattern. Here is the template: <input type="text" :value="val" @input="input" /> And here is the ...

The angular.json file contains a script and a styles property

After encountering issues with adding styles and scripts to my angular.json file, I discovered that neither Bootstrap nor other scripts were taking effect. It turns out there are two places where you can define scripts and styles in the angular.json file a ...

Changing the application's state from within a child component using React and Flux

UPDATE It seems that my initial approach was completely off base. According to the accepted answer, a good starting point is the TodoMVC app built with React + Flux and available on GitHub. I am currently working on a small React + Flux application for ed ...

Performing an Ajax request using MooTools when my button is clicked

After clicking a button, I want to initiate an ajax call. There are more than 14 buttons on my site that make ajax requests to fetch elements from various parts of the site. Button, Button1, Button2, Button3 | | | load content | | ...

Having trouble retrieving the value of a custom directive attribute

My custom directive, named "mycomponent", has the following configuration: restrict: 'AE', templateUrl: 'template.html', scope:{ sTransactionType: '=transactionType', sStorageVariable: '=storageVariable&apos ...

Tips for efficiently storing and accessing data obtained from the "RESTBuilder" tool on the Total.js platform

After stumbling upon this informative tutorial on how to build a cryptocurrency comparison site with VueJs, I was inspired to create a single-page application using the total.js platform. My goal is to fetch the list of coins from the "coinmarketcap.com" A ...

The conventional method for including React import statements

I'm curious if there is a standard convention for writing import statements in React. For instance, I currently have the following: import React, { useState, FormEvent } from 'react'; import Avatar from '@material-ui/core/Avatar'; ...

Concealing the flexslider in Angular when accessing the main URL

I need to hide a div with flexslider in it on the root page using ng-hide. The issue is that the images do not load when navigating to another path. Here is how my index.html is structured: <ul> <li><a href="#/">Root</a> ...

Locate the nearest index within the array

I'm currently working with an array of "events" where the key assigned to each event corresponds to the Unix Timestamp of that particular event. To illustrate, consider the following array of event objects in JS: var MyEventsArray=[]; MyEventsArray[1 ...

Executing a series of HTTP requests sequentially using Angular 5

I need some guidance on sending an array of HTTP requests in sequential order within my application. Here are the details: Application Entities : Location - an entity with attributes: FanZone fanZone, and List<LocationAdministrator> locationAdmins ...

Improving performance of a lengthy select query to generate JSON output in a Rails application

Trying to efficiently retrieve and format a significant amount of data from various database tables into nested JSON for quick output on the browser has been a top priority. Experimenting with different methods to streamline this process, I've experim ...

What causes an array to accumulate duplicate objects when they are added in a loop?

I am currently developing a calendar application using ExpressJS and TypeScript. Within this project, I have implemented a function that manages recurring events and returns an array of events for a specific month upon request. let response: TEventResponse ...

sort the array based on its data type

Recently diving into typescript... I have an array that is a union of typeA[] | typeB[] but I am looking to filter based on the object's type interface TypeA { attribute1: string attribute2: string } interface TypeB { attribute3: string attri ...

Looking to create a pop-up using javascript, css, or jQuery?

When visiting digg.com and clicking the login button, a sleek in-screen popup appears to input user data. I'm curious about the best way to achieve this on my own site. It is built with RoR and includes some Javascript elements. Searching for "javasc ...

Utilizing a plugin to execute a function in Wordpress

I'm currently facing the challenge of combining two WordPress plugins without the need to modify one to fit into the other seamlessly. My main question is: How can I call a function from one plugin that exists outside of another plugin? For example, ...