A JSON array must include a mandatory field

I have created a JSON schema to register multiple users at once. Each object in the JSON should contain at least one property for registration. Here is the schema I am using:

{
   "description":"Schema definition for user registration request",
   "type":"object",
   "properties":{
      "registerRequests":{
         "type":"array",
         "items":{
            "type":"object",
            "properties":{
               "user":{
                  "type":"object",
                  "properties":{
                     "age":{
                        "type":"integer",
                        "optional":true,
                        "minimum":1950,
                        "maximum":2100
                     },
                     "name":{
                        "type":"object",
                        "properties":{
                           "code":{
                              "type":"string",
                              "pattern":"^[\\w\\s!@#\\$%&\\-\\+=;:'\",\\.\\?\\(\\)\\\\/]{1,500}$"
                           },
                           "Desc":{
                              "type":"string",
                              "pattern":"^[\\w\\s!@#\\$%&\\-\\+=;:'\",\\.\\?\\(\\)\\\\/]{1,500}$"
                           }
                        },
                        "optional":true
                     }
                 }
               }
            }
         }
      }
   },
   "additionalProperties":false
}

When sending a request to register two users, each object must include at least one property as shown below:

{
   "registerRequests": [
     {
           "user": {
               "age": "25",
               "Desc": "Test"
           }
       },

       {
           "user": {
               "age": "20"
        }
     }
   ]
}

If I try to add a third object with no properties, I want to restrict it within the JSON itself. Is there an attribute similar to 'pattern' or 'optional' that can enforce this requirement?

Answer №1

Apologies for the delay in response, but I have some valuable information to share.

Prior to draft v3, the answer was no.

However, with the upcoming release of draft v4, the answer is now yes: "minProperties": 1.

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 error message "TypeError: window.Razorpay is not a constructor" indicates that

I am attempting to integrate RazorPay into my React app, but I am encountering the following error: https://i.stack.imgur.com/iFvdz.png Here is the code snippet: import TshirtImg from "./tshirt.jpg"; // import Razorpay from 'razorpay' ...

Tips for bypassing an argument using the POST method in NodeJS

Hey there! I started off by creating a question about passing over data using the GET method, but now I'm facing a new problem when trying to pass over data with the POST method. Below is my code snippet where things seem to be going wrong. My goal is ...

What could be the reason this is not getting saved in the public directory and causing a 404 error on the network

Within connection.js on the backend, there is a 2nd code snippet located inside post.jsx on the front end. router.post("/upload", (req, res) => { if (req.file === null) { return res.status(400).json({ msg: "No file uploaded" }); } const f ...

KendoGrid encountered a JavaScript runtime error due to an invalid template

Having some trouble navigating the Kendo world and encountering an issue with a grid that is set to a JSON array data source. An error message stating "JavaScript runtime error: Invalid template" is displayed, specifically referencing null values in the d ...

Nested checkbox table toggle/Select-Deselect

I have created a page that includes a nested table with checkboxes for selecting all the table cells <td>, as well as checkboxes for each individual table cell <td>. My goal is to: CheckAll checkbox should be able to check/uncheck all the pa ...

I'm looking to filter this array based on the value of a subarray that the user will specify the key and value for. How can I accomplish

Given the input var key="value_0" and var input="hello", I need to filter the array in TypeScript based on these values. The filtering criteria involve checking if the array elements contain a subarray with key="value_0" and if the value of this key includ ...

Utilizing jQuery to dynamically convert a dropdown into a multiselect when triggered by the selection of another dropdown within a form containing cloned

After coming across discussions on using a multiselect based on another multiselect, cloning a form row, and dynamically updating select options, it seems there are no answers detailing how to combine all three concepts together. In my particular scenario ...

The window resizing function continues on an endless loop

I could really use some assistance in pinpointing the issue at hand! My script is set up to display a banner at the top of a page only if the window width is 600px or less and the scroll position is within 34px from the top. It hides the banner when the u ...

Adding extra parentheses to the JSON object when posting to a node

As I send post requests from a .NET application to a node REST api with a controller named 'createPdf', everything seems to be working fine. I am using the .NET code to create a json object using SerializeObject and then encoding it with UrlEncod ...

Using mouse movements to adjust position in three.js with a fragment shader

After reading through this post, I'm attempting to modify a Voronoi shader from the Book of Shaders based on mouse cursor movements. However, I seem to be encountering issues with the offsets. Currently, my mouse cursor appears slightly off to the ri ...

Displaying the votes through an advanced system called Ajax voting system

I've encountered a challenge while using an ajax voting system in my project. The issue is that I'm utilizing a div with an id to showcase the votes, but whenever someone clicks on vote up or down in any of the posts (which are generated through ...

Verification upon button press for a form

Currently, I have a form with multiple textboxes that are being validated using the Required Field Validator. However, there is also a captcha control that is not getting validated. I can validate it using JavaScript though. At the moment, an alert pops u ...

The WebSocket function is returning undefined even though it successfully fetches the user; however, the user is

I've been experimenting with a websocket to retrieve user information. When I establish the connection and send messages to receive the data, it returns undefined when I try to use that information elsewhere. However, if I run console.log within the ...

Extract Information from a Website

Is there a way to extract data from another website using JavaScript and save it into a .TXT or possibly an XML file? If JavaScript is not the best solution, I am open to other suggestions. I am specifically interested in extracting the price and item na ...

Bring in Bootstrap and the Carousel plugin using Webpack Encore

Currently, I am tackling an issue within my Symfony project that involves Webpack Encore and the loading of Bootstrap and the Carousel plugin. The problem may stem from how I import Bootstrap, as it seems to partially work when I import the file like this ...

What is the best way to display JSON HTTP 422 responses to the user in a jQuery ajax request?

Hi, I've encountered a strange problem with my JSON. I'm trying to display error messages on my page, but despite trying numerous solutions found online, I keep receiving either 'undefined', 'null', or no message at all. Here& ...

Guide to Inputting Numbers in a Form Field using a Pop-up Keypad (with Javascript/AJAX)

I am working on a small project that involves creating a keypad with buttons for all the digits, backspace, and decimal. When these buttons are clicked, they should populate a form field like a text box. The keypad will be located next to the form field as ...

Special effects for the images动画效果。

Is there a way to add animation effects to images in the about section using this code: <div id="about" class="row section bgimg3"> <div class="col-sm-8"> <h2 style="color:black;">Want to Know More About me?</h2> ...

What is the best way to organize objects based on their keys?

Looking to iterate through object values and append text if the key matches in JavaScript. The object is as follows: { "id": "n27", "name": "Thomas More", "className": "level-1", & ...

When utilizing the dispatch function with UseReducer, an unexpected error is triggered: Anticipated 0 arguments were provided,

Having trouble finding a relevant answer, the only one I came across was related to Redux directly. So here's my question that might be obvious to some of you. In my code, everything appears to be correct but I'm facing an error that says: Expect ...