1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ const filePath = path . join ( __dirname , '../markdown/pulse/articles.json' ) ;
5+
6+ function validateJSONStructure ( json ) {
7+ if ( ! Array . isArray ( json ) ) {
8+ throw new Error ( 'Root element should be an array' ) ;
9+ }
10+
11+ json . forEach ( ( item , itemIndex ) => {
12+ if ( ! item . slug ) {
13+ throw new Error ( `Item at index ${ itemIndex } is missing 'slug'. Expected a string.` ) ;
14+ }
15+ if ( ! item . title ) {
16+ throw new Error ( `Item at index ${ itemIndex } is missing 'title'. Expected a string.` ) ;
17+ }
18+ if ( ! item . description ) {
19+ throw new Error ( `Item at index ${ itemIndex } is missing 'description'. Expected a string.` ) ;
20+ }
21+ if ( ! Array . isArray ( item . sections ) ) {
22+ throw new Error ( `Item at index ${ itemIndex } has invalid 'sections'. Expected an array.` ) ;
23+ }
24+
25+ item . sections . forEach ( ( section , sectionIndex ) => {
26+ if ( ! section . title ) {
27+ throw new Error ( `Section at index ${ sectionIndex } in item ${ itemIndex } is missing 'title'. Expected a string.` ) ;
28+ }
29+ if ( ! section . description ) {
30+ throw new Error ( `Section at index ${ sectionIndex } in item ${ itemIndex } is missing 'description'. Expected a string.` ) ;
31+ }
32+ if ( ! section . path ) {
33+ throw new Error ( `Section at index ${ sectionIndex } in item ${ itemIndex } is missing 'path'. Expected a string.` ) ;
34+ }
35+ if ( ! section . slug ) {
36+ throw new Error ( `Section at index ${ sectionIndex } in item ${ itemIndex } is missing 'slug'. Expected a string.` ) ;
37+ }
38+
39+ if ( section . data !== null && ! Array . isArray ( section . data ) ) {
40+ throw new Error ( `Section 'data' at index ${ sectionIndex } in item ${ itemIndex } should be an array or null.` ) ;
41+ }
42+
43+ if ( Array . isArray ( section . data ) ) {
44+ section . data . forEach ( ( dataItem , dataIndex ) => {
45+ if ( ! dataItem . title ) {
46+ throw new Error ( `Data item at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } is missing 'title'. Expected a string.` ) ;
47+ }
48+ if ( ! dataItem . description ) {
49+ throw new Error ( `Data item at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } is missing 'description'. Expected a string.` ) ;
50+ }
51+ if ( ! dataItem . date ) {
52+ throw new Error ( `Data item at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } is missing 'date'. Expected a string.` ) ;
53+ }
54+ if ( ! dataItem . path ) {
55+ throw new Error ( `Data item at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } is missing 'path'. Expected a string.` ) ;
56+ }
57+ if ( ! dataItem . slug ) {
58+ throw new Error ( `Data item at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } is missing 'slug'. Expected a string.` ) ;
59+ }
60+ if ( ! Array . isArray ( dataItem . tags ) ) {
61+ throw new Error ( `Data item 'tags' at index ${ dataIndex } in section ${ sectionIndex } of item ${ itemIndex } should be an array.` ) ;
62+ }
63+ } ) ;
64+ }
65+ } ) ;
66+ } ) ;
67+ }
68+
69+ try {
70+ const jsonData = JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ;
71+ validateJSONStructure ( jsonData ) ;
72+ console . log ( 'Validation passed' ) ;
73+ } catch ( error ) {
74+ console . error ( 'Validation failed:' , error . message ) ;
75+ process . exit ( 1 ) ;
76+ }
0 commit comments