1+ import Ajv2020 from "ajv/dist/2020" ;
2+ import { describe , it } from 'mocha' ;
3+ import schema from './resource-meta-schema.json' ;
4+ import resourceSchema from './resource-schema.json' ;
5+ import assert from "node:assert" ;
6+
7+ const ajv = new Ajv2020 ( {
8+ strict : true ,
9+ } )
10+
11+ describe ( "Resource meta schema tests" , ( ) => {
12+ it ( "compiles" , ( ) => {
13+ ajv . compile ( schema ) ;
14+ } )
15+
16+ it ( "must have additional properties and $ref to resource-schema specified" , ( ) => {
17+ const validate = ajv . compile ( schema ) ;
18+
19+ assert . equal ( validate ( {
20+ "$schema" : "https://json-schema.org/draft/2020-12/schema" ,
21+ "type" : "object" ,
22+ "properties" : {
23+ "prop1" : {
24+ "type" : "string"
25+ }
26+ }
27+ } ) , false ) ;
28+
29+ assert . equal ( validate ( {
30+ "$schema" : "https://json-schema.org/draft/2020-12/schema" ,
31+ "type" : "object" ,
32+ "properties" : {
33+ "prop1" : {
34+ "type" : "string"
35+ }
36+ } ,
37+ "additionalProperties" : false ,
38+ "$ref" : "https://www.codify.com/resource-schema.json"
39+ } ) , true ) ;
40+ } )
41+
42+ it ( "does not allow resource keywords to be specified" , ( ) => {
43+ const validate = ajv . compile ( schema ) ;
44+
45+ assert . equal ( validate ( {
46+ "$schema" : "https://json-schema.org/draft/2020-12/schema" ,
47+ "type" : "object" ,
48+ "properties" : {
49+ "type" : {
50+ "type" : "string"
51+ }
52+ } ,
53+ "additionalProperties" : false ,
54+ "$ref" : "https://www.codify.com/resource-schema.json"
55+ } ) , false ) ;
56+
57+ assert . equal ( validate ( {
58+ "$schema" : "https://json-schema.org/draft/2020-12/schema" ,
59+ "type" : "object" ,
60+ "properties" : {
61+ "name" : {
62+ "type" : "string"
63+ }
64+ } ,
65+ "additionalProperties" : false ,
66+ "$ref" : "https://www.codify.com/resource-schema.json"
67+ } ) , false ) ;
68+
69+ assert . equal ( validate ( {
70+ "$schema" : "https://json-schema.org/draft/2020-12/schema" ,
71+ "type" : "object" ,
72+ "properties" : {
73+ "dependsOn" : {
74+ "type" : "array"
75+ }
76+ } ,
77+ "additionalProperties" : false ,
78+ "$ref" : "https://www.codify.com/resource-schema.json"
79+ } ) , false ) ;
80+ } )
81+
82+ it ( "allows for schema composition with base resource schema" , ( ) => {
83+ const resourceMetaSchema = schema ;
84+ const propertySchema = {
85+ $id : "https://www.codify.com/property-schema.json" ,
86+ type : "object" ,
87+ properties : {
88+ prop1 : {
89+ type : "string"
90+ } ,
91+ prop2 : {
92+ type : "string"
93+ } ,
94+ } ,
95+ $ref : "https://www.codify.com/resource-schema.json" ,
96+ unevaluatedProperties : false ,
97+ }
98+
99+ const resourceConfigInvalid = {
100+ prop1 : "a" ,
101+ }
102+
103+ const resourceConfigValid = {
104+ type : "resource-config" ,
105+ prop1 : "a" ,
106+ dependsOn : [ "resource-config-2" ]
107+ }
108+
109+ const ajv = new Ajv2020 ( {
110+ strict : true ,
111+ schemas : [ resourceSchema ]
112+ } ) ;
113+
114+ const metaSchemaValidator = ajv . compile ( resourceMetaSchema ) ;
115+ assert . equal ( metaSchemaValidator ( propertySchema ) , true )
116+
117+ const resourceValidator = ajv . compile ( propertySchema ) ;
118+ assert . equal ( resourceValidator ( resourceConfigValid ) , true ) ;
119+ assert . equal ( resourceValidator ( resourceConfigInvalid ) , false ) ;
120+ } )
121+ } ) ;
0 commit comments