@@ -67,3 +67,76 @@ test("validateSchema - optional properties are optional", async (t) => {
6767 t . true ( json . validateSchema ( optionalSchema , { optionalKey : "" } ) ) ;
6868 t . true ( json . validateSchema ( optionalSchema , { optionalKey : "foo" } ) ) ;
6969} ) ;
70+
71+ const arraySchema = {
72+ arrayKey : json . array ( json . number ) ,
73+ } ;
74+
75+ test ( "validateSchema - validates arrays" , async ( t ) => {
76+ // Arrays of numeric elements are accepted.
77+ t . true ( json . validateSchema ( arraySchema , { arrayKey : [ ] } ) ) ;
78+ t . true ( json . validateSchema ( arraySchema , { arrayKey : [ 4 ] } ) ) ;
79+ t . true ( json . validateSchema ( arraySchema , { arrayKey : [ 4 , 8 ] } ) ) ;
80+ t . true ( json . validateSchema ( arraySchema , { arrayKey : [ 4 , 8 , 15 ] } ) ) ;
81+
82+ // Other array elements are not accepted.
83+ t . false ( json . validateSchema ( arraySchema , { arrayKey : [ 4 , 8 , 15 , "bar" ] } ) ) ;
84+ t . false ( json . validateSchema ( arraySchema , { arrayKey : [ 4 , 8 , undefined ] } ) ) ;
85+ t . false ( json . validateSchema ( arraySchema , { arrayKey : [ 4 , 8 , 15 , null ] } ) ) ;
86+ } ) ;
87+
88+ const objectSchema = {
89+ objectKey : json . object ( arraySchema ) ,
90+ } ;
91+
92+ test ( "validateSchema - validates objects" , async ( t ) => {
93+ // Objects of the given schema are accepted.
94+ t . true ( json . validateSchema ( objectSchema , { objectKey : { arrayKey : [ ] } } ) ) ;
95+ t . true ( json . validateSchema ( objectSchema , { objectKey : { arrayKey : [ 4 ] } } ) ) ;
96+
97+ // Other values are not accepted.
98+ t . false ( json . validateSchema ( objectSchema , { } ) ) ;
99+ t . false ( json . validateSchema ( objectSchema , { objectKey : [ ] } ) ) ;
100+ t . false ( json . validateSchema ( objectSchema , { objectKey : undefined } ) ) ;
101+ t . false ( json . validateSchema ( objectSchema , { objectKey : null } ) ) ;
102+ t . false ( json . validateSchema ( objectSchema , { objectKey : "foo" } ) ) ;
103+ t . false ( json . validateSchema ( objectSchema , { objectKey : 123 } ) ) ;
104+ } ) ;
105+
106+ const checkSchemaTestSchema = {
107+ rootKey : json . object ( objectSchema ) ,
108+ } ;
109+
110+ test ( "checkSchema - reports unknown keys" , async ( t ) => {
111+ const result = json . checkSchema ( checkSchemaTestSchema , {
112+ rootKey : {
113+ objectKey : {
114+ arrayKey : [ ] ,
115+ } ,
116+ nestedExtraKey : "foo" ,
117+ } ,
118+ extraKey : "bar" ,
119+ } ) ;
120+
121+ t . true ( result . valid ) ;
122+ t . deepEqual (
123+ result . unknownKeys . sort ( ) ,
124+ [ ".extraKey" , ".rootKey.nestedExtraKey" ] . sort ( ) ,
125+ ) ;
126+ } ) ;
127+
128+ test ( "checkSchema - reports invalid keys" , async ( t ) => {
129+ const result = json . checkSchema ( checkSchemaTestSchema , {
130+ rootKey : {
131+ objectKey : {
132+ arrayKey : [ "foo" ] ,
133+ } ,
134+ } ,
135+ } ) ;
136+
137+ t . false ( result . valid ) ;
138+ t . deepEqual (
139+ result . invalidKeys . sort ( ) ,
140+ [ ".rootKey.objectKey.arrayKey[0]" ] . sort ( ) ,
141+ ) ;
142+ } ) ;
0 commit comments