-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathvalidate-json.js
More file actions
executable file
·279 lines (252 loc) · 6.93 KB
/
Copy pathvalidate-json.js
File metadata and controls
executable file
·279 lines (252 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node
/**
* External dependencies
*/
const Ajv4 = require( 'ajv-draft-04' ).default;
const Ajv = require( 'ajv' ).default;
const addFormats = require( 'ajv-formats' ).default;
const fg = require( 'fast-glob' );
const fs = require( 'fs' );
const path = require( 'path' );
/**
* @typedef {Object} JSONSchema
* @property {string} $schema Schema URL.
*/
/**
* @type {Map<string, JSONSchema>}
*/
const schemaCache = new Map();
/**
* @template {Ajv} T
* @typedef {{ new (options: Object): T }} AjvConstructorType
*/
/**
* Creates an Ajv instance.
*
* @template {Ajv} T
* @param {AjvConstructorType<T>} AjvConstructor Ajv constructor.
* @return {T} Ajv instance.
*/
function createAjv( AjvConstructor ) {
const ajv = new AjvConstructor( {
allErrors: true,
strict: false, // See <https://github.com/WordPress/wordpress-playground/issues/3178>.
loadSchema: fetchSchema,
} );
addFormats( ajv );
ajv.removeKeyword( 'deprecated' );
ajv.addKeyword( {
keyword: 'deprecated',
validate: ( /** @type {string|boolean} */ deprecation ) =>
! deprecation,
error: {
/**
* @param {Object} cxt
* @param {string|Object} [cxt.schema]
*/
message: ( cxt ) => {
return cxt.schema && typeof cxt.schema === 'string'
? `is deprecated: ${ cxt.schema }`
: 'is deprecated';
},
},
} );
return ajv;
}
const ajv = createAjv( Ajv );
const ajv4 = createAjv( Ajv4 );
/**
* Fetches a JSON schema from a URL.
*
* @param {string} schemaUrl URL of the JSON schema.
* @return {Promise<JSONSchema>} The JSON schema object.
*/
async function fetchSchema( schemaUrl ) {
const cachedSchema = schemaCache.get( schemaUrl );
if ( cachedSchema ) {
return cachedSchema;
}
if (
! schemaUrl.startsWith( 'https://' ) &&
! schemaUrl.startsWith( 'http://json-schema.org/' )
) {
throw new Error(
`Schema URL must start with https:// (or be http://json-schema.org/): ${ schemaUrl }`
);
}
const controller = new AbortController();
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const timeout = setTimeout( () => controller.abort(), 5000 );
try {
const response = await fetch( schemaUrl, {
signal: controller.signal,
} );
if ( ! response.ok ) {
throw new Error(
`Failed to fetch schema from ${ schemaUrl }: ${ response.statusText }`
);
}
const schema = await response.json();
if (
typeof schema !== 'object' ||
schema === null ||
Array.isArray( schema ) ||
typeof schema.$schema !== 'string'
) {
throw new Error(
`Schema from ${ schemaUrl } is not a JSON Schema object.`
);
}
schemaCache.set( schemaUrl, schema );
return schema;
} finally {
clearTimeout( timeout );
}
}
/**
* Fetches a JSON schema and determines its draft version.
*
* @param {string} schemaUrl URL of the JSON schema.
* @return {Promise<'draft-04'|'default'>} The draft version ('draft-04' or 'default').
*/
async function getSchemaDraft( schemaUrl ) {
const schema = await fetchSchema( schemaUrl );
const draft = typeof schema.$schema === 'string' ? schema.$schema : '';
// Default to 'default' (modern Ajv) for other cases.
return /^https?:\/\/json-schema\.org\/draft-04\/schema#?$/.test( draft )
? 'draft-04'
: 'default';
}
/**
* Validates a JSON file against its schema.
*
* @param {string} filePath Path to the JSON file.
* @return {Promise<boolean>} Whether the file is valid.
*/
async function validateFile( filePath ) {
const absolutePath = path.resolve( process.cwd(), filePath );
const maxBlueprintSizeKB = 100; // See <https://github.com/WordPress/wordpress.org/blob/e76f2913139cd2c7d9fd26895dda58685d16aa81/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php#L809>.
if (
// See <https://github.com/WordPress/wordpress.org/blob/e76f2913139cd2c7d9fd26895dda58685d16aa81/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php#L860> for the pattern of which blueprints are considered.
/^\.wordpress-org\/blueprints\/blueprint[\w-]*\.json$/.test( filePath )
) {
try {
const stats = await fs.promises.stat( absolutePath );
if ( stats.size > maxBlueprintSizeKB * 1024 ) {
const sizeKB = stats.size / 1024;
console.error(
`${ filePath }: Blueprint is too large at ${ sizeKB.toFixed(
2
) } KB. Max allowed is ${ maxBlueprintSizeKB } KB. ❌`
);
return false;
}
} catch ( error ) {
if ( error instanceof Error ) {
console.error(
`${ filePath }: ❌ Unable to get file size: ${ error.message }`
);
} else {
console.error(
`${ filePath }: ❌ Unable to get file size:`,
error
);
}
return false;
}
}
let data;
try {
const content = await fs.promises.readFile( absolutePath, 'utf8' );
data = JSON.parse( content );
} catch ( error ) {
if ( error instanceof Error ) {
console.error(
`${ filePath }: ❌ Error parsing JSON: ${ error.message }`
);
} else {
console.error(
`${ filePath }: ❌ Unknown JSON parsing error:`,
error
);
}
return false;
}
if ( data.$schema ) {
try {
const draft = await getSchemaDraft( data.$schema );
const ajvInstance = draft === 'draft-04' ? ajv4 : ajv;
const validate = await ajvInstance.compileAsync( {
$ref: data.$schema,
} );
const valid = validate( data );
if ( ! valid ) {
if ( validate.errors ) {
validate.errors.forEach( ( error ) => {
console.error( `${ filePath }: ❌ Error:`, error );
} );
} else {
console.error(
`${ filePath }: ❌ Unknown validation error`
);
}
return false;
}
} catch ( error ) {
if ( error instanceof Error ) {
console.error(
`${ filePath }: ❌ Error validating JSON: ${ error.message }`
);
} else {
console.error( `${ filePath }: ❌ Unknown Ajv error:`, error );
}
return false;
}
console.log( `${ filePath }: Valid against <${ data.$schema }>. ✅` );
} else {
console.log(
`${ filePath }: Skipping schema validation since no $schema property found. Syntax validated only. ✅`
);
}
return true;
}
const args = process.argv.slice( 2 );
const patterns = args.length > 0 ? args : [ '**/*.json' ];
( async () => {
const files = await fg( patterns, {
dot: true,
ignore: [
'node_modules/**',
'vendor/**',
'build/**',
'plugins/*/build/**',
],
} );
if ( files.length === 0 ) {
if ( args.length > 0 ) {
console.error(
'No JSON files found matching the provided patterns.'
);
process.exit( 1 );
} else {
console.warn(
'No JSON files found using the default pattern "**/*.json". ' +
'Ensure you are running this script from the repository root, or provide explicit glob patterns.'
);
return;
}
}
let hasError = false;
for ( const file of files ) {
const isValid = await validateFile( file );
if ( ! isValid ) {
hasError = true;
}
}
if ( hasError ) {
process.exit( 1 );
}
} )().catch( ( error ) => {
console.error( 'Unexpected error during JSON validation:', error );
process.exit( 1 );
} );