Invalid: AJV detected an array of objects that does not comply with the JSON

I've been working on creating a simple schema for an array of objects, but TypeScript gave me some trouble with the following code:

import { JSONSchemaType } from 'ajv';

const schema: JSONSchemaType<{ label: string; value: string }[]> = {
  type: 'array',
  items: {
    type: 'object',
    required: ['label', 'value'], // Type 'string' is not assignable to type 'never'
    properties: {
      label: { type: 'string' }, // Property 'nullable' is missing in type '{ type: "string"; }' but required in type '{ nullable: true; const?: never; enum?: readonly string[]; default?: string; }'
      value: { type: 'string' }, // Property 'nullable' is missing in type '{ type: "string"; }' but required in type '{ nullable: true; const?: never; enum?: readonly string[]; default?: string; }'
    },
  },
};

I'm trying to figure out what's causing these errors. If I change the properties to

label: { type: 'string', nullable: true }
, it resolves the second error, but ideally neither property should be nullable.

Thank you!

UPDATE:

TypeScript version: 4.0.3

AJV version: 7.2.3

Answer №1

Experiment with release 8.12. Encountered an issue while using JSONSchemaType in AJV v6.

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

What is the process for importing a component at a later time?

I am attempting to import components with a delay in a seamless manner. My goal is to import the components discreetly so that they load smoothly in the background while viewing the homepage. I experimented with lazy loading, but found that it caused dela ...

Utilizing Logical Operators in Typescript Typing

What is the preferred method for boolean comparisons in Typescript types? I have devised the following types for this purpose, but I am curious if there is a more standard or efficient approach: type And<T1 extends boolean, T2 extends boolean> = T1 ...

How can we update a boolean value in an Angular service using a set function?

Hey there! I'm currently working on updating a boolean value in my service when a button is clicked within my component. My goal is to trigger the setfunction and toggle the boolean value from true to false, and vice versa when the button is clicked a ...

Ensuring the validity of various data types using typeguard

How can I create a type guard that validates multiple types? Here is what I have in mind: interface A { } interface B extends A { foo: number; } interface C extends A { bar: number; } function checkType<T extends A>(item: A): item is T { if( ...

I need to find a way to dynamically filter a Json object, taking into account that my filter condition may vary. The number of possible scenarios

I need to dynamically filter my data based on changing conditions. For example, I want to call a method on the <select (change)="filterData($event.target.value,'jobStatusId')" >, but the condition to filter by can be dynamic, such ...

TypeScript sometimes struggles to accurately deduce the precise return type of a function

I have a function called myCallback const myCallback = (param: number) => { // doSomething }; Now, I want to create another function, useMyCallback, that may or may not receive a parameter and will return myCallback either bound or unmodified: const ...

Creating an array in Angular is done by using the syntax []

I encountered an error and I'm not sure how to fix it. Can someone assist me? The error message reads: "Type '{ animal:[{ id : 1,name: "Elephant"},{id : 2, name: "Horse"} []; }' is not assignable to type 'string[]'. Property & ...

Limit the utilization of toString through a TypeScript interface or type

Here's the interface I'm aiming for: export interface Point { readonly x: number; readonly y: number; readonly toString: never; } I initially expected it to function this way: const p: Point = {x: 4, y: 5}; // This should work fine p.toStr ...

A guide to implementing $scope.$watch in TypeScript

I am looking for a way to track changes in an element that is bound to my component. This element's value might change due to asynchronous calls, such as promises. In JavaScript, I typically use $scope.$watch to monitor the changes in the binding elem ...

The local storage gets wiped clean whenever I am using this.router.navigate

I am in the process of building a website using Angular 5 and Typescript. One important aspect of my implementation is utilizing localStorage to store the JWT Token for user login. Whenever I click on a link (either Home or any other link), I implement a ...

What could be the reason for the failure of my class isInstance() check

Do you see any issues with the object being an instance of ChatRoom? Let me know your thoughts. Class: export class ChatRoom { public id?: number; public name_of_chat_room: string; public chat_creator_user_id: number; public chat_room_is_active: 0 ...

Is there a simple method to eliminate devDependencies from the ultimate package using esbuild?

My current challenge involves using esbuild to package my lambda functions. However, during the build generation for deployment, I encounter an alert indicating that the package size exceeds the limit, as shown in the image below. File too large In explo ...

unable to automatically import React components from node modules

Recently, I began working with VS Code, utilizing Material UI with React and TypeScript. However, I am facing an issue where I am unable to import the components of Material UI using the alt(option) + enter shortcut on my Mac. The TypeScript version I am w ...

Incorporate a random number generation within a specified index range while looping in Angular

I am working with an array that contains static images: public ImageList: Array<any> = [ {"id" : 1, "Url" :"assets/images/...svg"}, {"id" : 2, "Url" :"assets/images/...svg"}, { ...

I'm interested in developing a feature that monitors a specific attribute and triggers a function once that attribute hits the value of 100

I am working on a function that will refresh the view once the percentage changes reaches 100: The value is stored in this variable: this.uploadPercent = task.percentageChanges(); This is the function I plan to implement : refreshView(){ Once this.uplo ...

I am trying to replace the buttons with a dropdown menu for changing graphs, but unfortunately my function does not seem to work with the <select> element. It works perfectly fine with buttons though

I am currently working on my html and ts code, aiming to implement a dropdown feature for switching between different graphs via the chartType function. The issue I am facing is that an error keeps popping up stating that chartType is not recognized as a ...

Move to the first item on the list without any unnecessary scrolling of the view - ReactJS

Whenever I try to scroll to a specific item in the list, clicking on it causes the entire view to scroll instead. Is there a way to ensure that only the div containing the list scrolls? <AlertsList> {productInventoryAlert?.map((prod ...

Angular 7's StyleUrl that Adapts to Your Needs

Is there a better way to dynamically load styles in Angular 7? I have tried the example below but it's not working in version 7 of Angular. This code worked fine in earlier versions before Angular 7. Can someone offer some help or advice please? Thank ...

Is it possible to provide unrestricted support for an infinite number of parameters in the typing of the extend function from Lodash

I am utilizing the "extend" function from lodash to combine the objects in the arguments as follows: import { extend } from 'lodash'; const foo1 = { item: 1 }; const foo2 = { item: 1 }; const foo3 = { item: 1 }; const foo4 = { item: 1 }; const f ...

What could have caused this issue to appear when I tried running ng build --prod?

Issue encountered while trying to build the ng2-pdf-viewer module: An error occurred in the webpack loader (from @angular-devkit/build-optimizer) with the message: TypeError: Cannot read property 'kind' of undefined. This error is related to A ...