Using Ajv on NodeJS, I can't seem to get a circular TypeScript structure to be defined in Ajv.
A simple circular structure works (see "parent"), but I can't get an array to work (see "children").
Uncomment the "children" lines to see the error.
'use strict'
import Ajv, {JSONSchemaType} from "ajv"
type MyType = {
name : string,
parent? : MyType,
//children? : Array<MyType>
};
const MySchema : JSONSchemaType<MyType> = {
type:"object",
properties:{
name:{type:"string"},
parent:{$ref:"#"},
//children:{type:"array",items:{$ref:"#"}}
},
required:["name"]
}
let pnt:MyType = { name : "Parent" }
let c1:MyType = { name : "Child1" }
let c2:MyType = { name : "Child2" }
let c3:MyType = { name : "Child3" }
let node:MyType = { name : "Node1", parent : pnt }
//node.children = [c1,c2,c3];
// Validate
const ajv = new Ajv({discriminator:true,allErrors:true,allowUnionTypes:true})
const validate = ajv.compile(MySchema)
console.log(validate(pnt));
console.log(validate(c1));
console.log(validate(c2));
console.log(validate(c3));
console.log(validate(node));
The error, when the "children" lines are uncommented
Type '{ type: "object"; properties: { name: { type: "string"; }; parent: { $ref: string; }; children: { type: "array"; items: { $ref: string; }; }; }; required: "name"[]; }' is not assignable to type 'UncheckedJSONSchemaType<MyType, false>'. The types of 'properties.children' are incompatible between these types. Type '{ type: "array"; items: { $ref: string; }; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<MyType[] | undefined, false> & { nullable: true; const?: null | undefined; enum?: readonly (MyType[] | null | undefined)[] | undefined; default?: MyType[] | ... 1 more ... | undefined; })'. Types of property 'items' are incompatible. Type '{ $ref: string; }' is not assignable to type 'UncheckedJSONSchemaType<MyType, false>'. Type '{ $ref: string; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'. Property 'type' is missing in type '{ $ref: string; }' but required in type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.