33 * @param {Array } schema - The schema to validate.
44 * @returns {Object } - { isValid: boolean, errors: Array<string> }
55 */
6+
67export function validateSchema ( schema ) {
7- const validTypes = [ "string" , "number" , "array" , "object" , "boolean" , "enum" ] ;
8- let errors = [ ] ;
9-
10- if ( ! Array . isArray ( schema ) ) {
11- errors . push ( "Schema must be an array." ) ;
12- return { isValid : false , errors } ;
8+ const validTypes = [ "string" , "number" , "array" , "object" , "boolean" , "enum" ] ;
9+ let errors = [ ] ;
10+
11+ if ( ! Array . isArray ( schema ) ) {
12+ errors . push ( "Schema must be an array." ) ;
13+ return { isValid : false , errors } ;
14+ }
15+
16+ function validateField ( field , path ) {
17+ if ( ! field . hasOwnProperty ( "name" ) || typeof field . name !== "string" || field . name . trim ( ) === "" ) {
18+ errors . push ( `"name" is required and should be a non-empty string at ${ path } ` ) ;
19+ }
20+
21+ if ( ! field . hasOwnProperty ( "type" ) || ! validTypes . includes ( field . type ) ) {
22+ errors . push ( `"type" is required and must be one of ${ validTypes . join ( ", " ) } at ${ path } ` ) ;
23+ }
24+
25+ if ( ! field . hasOwnProperty ( "description" ) || typeof field . description !== "string" || field . description . trim ( ) === "" ) {
26+ errors . push ( `"description" is required and should be a non-empty string at ${ path } ` ) ;
1327 }
14-
15- function validateField ( field , path ) {
16- if ( ! field . hasOwnProperty ( "name" ) || typeof field . name !== "string" || field . name . trim ( ) === "" ) {
17- errors . push ( `"name" is required and should be a non-empty string at ${ path } ` ) ;
18- }
19-
20- if ( ! field . hasOwnProperty ( "type" ) || ! validTypes . includes ( field . type ) ) {
21- errors . push ( `"type" is required and must be one of ${ validTypes . join ( ", " ) } at ${ path } ` ) ;
22- }
23-
24- if ( ! field . hasOwnProperty ( "description" ) || typeof field . description !== "string" || field . description . trim ( ) === "" ) {
25- errors . push ( `"description" is required and should be a non-empty string at ${ path } ` ) ;
26- }
27-
28- // Additional checks for arrays
29- if ( field . type === "array" ) {
30- if ( ! field . hasOwnProperty ( "children" ) ) {
31- errors . push ( `"children" property is required for arrays at ${ path } ` ) ;
32- } else if ( ! Array . isArray ( field . children ) || field . children . length === 0 ) {
33- errors . push ( `"children" must be a non-empty array at ${ path } ` ) ;
34- } else {
35- // Recursively validate each child
36- field . children . forEach ( ( child , index ) => validateField ( child , `${ path } .children[${ index } ]` ) ) ;
37- }
38- }
3928
40- // Additional checks for enum
4129 if ( field . type === "enum" ) {
4230 if ( ! field . hasOwnProperty ( "values" ) || ! Array . isArray ( field . values ) || field . values . length === 0 ) {
43- errors . push ( `"values" is required and must be a non-empty array for enum at ${ path } ` ) ;
31+ errors . push ( `"values" is required and must be a non-empty array for enum " ${ field . name } " at ${ path } ` ) ;
4432 } else if ( ! field . values . every ( ( value ) => typeof value === "string" ) ) {
45- errors . push ( `"values" for enum at ${ path } must be an array of strings` ) ;
33+ errors . push ( `"values" for enum " ${ field . name } " at ${ path } must be an array of strings` ) ;
4634 }
4735 }
4836
37+ if ( field . type === "array" ) {
38+ if ( ! field . hasOwnProperty ( "children" ) ) {
39+ errors . push ( `"children" property is required for array "${ field . name } " at ${ path } ` ) ;
40+ } else if ( ! Array . isArray ( field . children ) || field . children . length === 0 ) {
41+ errors . push ( `"children" must be a non-empty array for "${ field . name } " at ${ path } ` ) ;
42+ } else {
43+ field . children . forEach ( ( child , index ) => validateField ( child , `${ path } .children[${ index } ]` ) ) ;
44+ }
45+ }
46+
47+ if ( field . type === "object" ) {
48+ if ( ! field . hasOwnProperty ( "children" ) ) {
49+ errors . push ( `"children" property is required for object "${ field . name } " at ${ path } ` ) ;
50+ } else if ( ! Array . isArray ( field . children ) || field . children . length === 0 ) {
51+ errors . push ( `"children" must be a non-empty array for "${ field . name } " at ${ path } ` ) ;
52+ } else {
53+ field . children . forEach ( ( child , index ) => validateField ( child , `${ path } .children[${ index } ]` ) ) ;
54+ }
55+ }
4956 }
50-
51- schema . forEach ( ( field , index ) => validateField ( field , `schema[${ index } ]` ) ) ;
52-
53- return {
54- isValid : errors . length === 0 ,
55- errors,
56- } ;
57- }
58-
57+
58+ schema . forEach ( ( field , index ) => validateField ( field , `schema[${ index } ]` ) ) ;
59+
60+ return {
61+ isValid : errors . length === 0 ,
62+ errors,
63+ } ;
64+ }
0 commit comments