import type Ajv from "../dist/core" ; import type { SchemaObject } from ".." ; import _Ajv from "./ajv2019" ; import getAjvInstances from "./ajv_instances" ; import options from "./ajv_options" ; import assert = require ( "assert" ) ; describe ( "recursiveRef and dynamicRef" , ( ) => { let ajvs ; beforeEach ( ( ) => { ajvs = getAjvInstances ( _Ajv , options ) ; } ) ; describe ( "recursiveRef" , ( ) => { it ( "should allow extending recursive schema with recursiveRef (draft2019-09)" , ( ) => { const treeSchema = { $id : "https://example.com/tree" , $recursiveAnchor : true , type : "object" , required : [ "data" ] , properties : { data : true , children : { type : "array" , items : { $recursiveRef : "#" , } , } , } , } ; const strictTreeSchema = { $id : "https://example.com/strict-tree" , $recursiveAnchor : true , $ref : "tree" , type : "object" , unevaluatedProperties : false , } ; testTree ( treeSchema , strictTreeSchema ) ; } ) ; } ) ; describe ( "dynamicRef" , ( ) => { it ( "should allow extending recursive schema with dynamicRef (future draft2020)" , ( ) => { const treeSchema = { $id : "https://example.com/tree" , $dynamicAnchor : "node" , type : "object" , required : [ "data" ] , properties : { data : true , children : { type : "array" , items : { $dynamicRef : "#node" , } , } , } , } ; const strictTreeSchema = { $id : "https://example.com/strict-tree" , $dynamicAnchor : "node" , $ref : "tree" , type : "object" , unevaluatedProperties : false , } ; testTree ( treeSchema , strictTreeSchema ) ; } ) ; } ) ; function testTree ( treeSchema , strictTreeSchema ) { const validTree = { data : 1 , children : [ { data : 2 , children : [ { data : 3 } ] , } , ] , } ; const invalidTree = { data : 1 , children : [ { data : 2 , children : { } , } , ] , } ; const treeWithExtra = { data : 1 , children : [ { data : 2 , extra : 2 } ] , } ; const treeWithDeepExtra = { data : 1 , children : [ { data : 2 , children : [ { data : 3 , extra : 3 } ] , } , ] , } ; ajvs . forEach ( ( ajv ) => { const validate = ajv . compile ( treeSchema ) ; assert . strictEqual ( validate ( validTree ) , true ) ; assert . strictEqual ( validate ( invalidTree ) , false ) ; assert . strictEqual ( validate ( treeWithExtra ) , true ) ; assert . strictEqual ( validate ( treeWithDeepExtra ) , true ) ; const validateStrict = ajv . compile ( strictTreeSchema ) ; assert . strictEqual ( validateStrict ( validTree ) , true ) ; assert . strictEqual ( validateStrict ( invalidTree ) , false ) ; assert . strictEqual ( validateStrict ( treeWithExtra ) , false ) ; assert . strictEqual ( validateStrict ( treeWithDeepExtra ) , false ) ; } ) ; } } ) ;