|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | +import path from 'node:path'; |
| 5 | +import process from 'node:process'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +import Ajv from 'ajv'; |
| 9 | + |
| 10 | +const require = createRequire(import.meta.url); |
| 11 | +const draft07MetaSchema = require('ajv/dist/refs/json-schema-draft-07.json'); |
| 12 | +const rootDir = path.resolve( |
| 13 | + path.dirname(fileURLToPath(import.meta.url)), |
| 14 | + '..' |
| 15 | +); |
| 16 | +const packagesDir = path.join(rootDir, 'packages'); |
| 17 | +const args = new Set(process.argv.slice(2)); |
| 18 | +const markdown = args.has('--markdown'); |
| 19 | +const help = args.has('--help') || args.has('-h'); |
| 20 | + |
| 21 | +const pointerFor = ({ instancePath }) => |
| 22 | + instancePath ? `#${instancePath}` : '#'; |
| 23 | + |
| 24 | +const relativePath = file => |
| 25 | + path.relative(rootDir, file).split(path.sep).join('/'); |
| 26 | + |
| 27 | +const escapeMarkdown = value => |
| 28 | + String(value) |
| 29 | + .replace(/\\/g, '\\\\') |
| 30 | + .replace(/\|/g, '\\|') |
| 31 | + .replace(/\n/g, ' '); |
| 32 | + |
| 33 | +const formatReason = error => { |
| 34 | + if (!error) return 'Schema validation failed.'; |
| 35 | + |
| 36 | + const details = []; |
| 37 | + if (error.keyword === 'additionalProperties') { |
| 38 | + details.push(`additional property "${error.params.additionalProperty}"`); |
| 39 | + } |
| 40 | + if (error.keyword === 'enum' && error.params?.allowedValues) { |
| 41 | + details.push(`allowed values: ${error.params.allowedValues.join(', ')}`); |
| 42 | + } |
| 43 | + |
| 44 | + const message = error.message ?? 'Schema validation failed.'; |
| 45 | + return details.length ? `${message} (${details.join('; ')})` : message; |
| 46 | +}; |
| 47 | + |
| 48 | +const getUsefulErrors = errors => |
| 49 | + errors.filter(error => { |
| 50 | + const samePointerErrors = errors.filter( |
| 51 | + other => other.instancePath === error.instancePath |
| 52 | + ); |
| 53 | + |
| 54 | + if ( |
| 55 | + ['anyOf', 'oneOf', 'allOf'].includes(error.keyword) && |
| 56 | + samePointerErrors.length > 1 |
| 57 | + ) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + if ( |
| 62 | + error.keyword === 'type' && |
| 63 | + samePointerErrors.some(other => other.keyword === 'enum') |
| 64 | + ) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + }); |
| 70 | + |
| 71 | +const findSchemaFiles = async () => { |
| 72 | + const adaptors = await fs.readdir(packagesDir, { withFileTypes: true }); |
| 73 | + const schemaFiles = []; |
| 74 | + |
| 75 | + for (const adaptor of adaptors) { |
| 76 | + if (!adaptor.isDirectory()) continue; |
| 77 | + |
| 78 | + const adaptorDir = path.join(packagesDir, adaptor.name); |
| 79 | + const files = await fs.readdir(adaptorDir, { withFileTypes: true }); |
| 80 | + for (const file of files) { |
| 81 | + if (file.isFile() && file.name.endsWith('configuration-schema.json')) { |
| 82 | + schemaFiles.push(path.join(adaptorDir, file.name)); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return schemaFiles.sort((a, b) => |
| 88 | + relativePath(a).localeCompare(relativePath(b)) |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +const validateSchemaFile = async (file, ajv) => { |
| 93 | + const adaptor = path.basename(path.dirname(file)); |
| 94 | + const filePath = relativePath(file); |
| 95 | + let schema; |
| 96 | + |
| 97 | + try { |
| 98 | + schema = JSON.parse(await fs.readFile(file, 'utf8')); |
| 99 | + } catch (error) { |
| 100 | + return [ |
| 101 | + { |
| 102 | + adaptor, |
| 103 | + filePath, |
| 104 | + pointer: '#', |
| 105 | + reason: `Invalid JSON: ${error.message}`, |
| 106 | + }, |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + if (ajv.validateSchema(schema)) return []; |
| 112 | + } catch (error) { |
| 113 | + return [ |
| 114 | + { |
| 115 | + adaptor, |
| 116 | + filePath, |
| 117 | + pointer: '#', |
| 118 | + reason: error.message, |
| 119 | + }, |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + return getUsefulErrors(ajv.errors ?? []).map(error => ({ |
| 124 | + adaptor, |
| 125 | + filePath, |
| 126 | + pointer: pointerFor(error), |
| 127 | + reason: formatReason(error), |
| 128 | + })); |
| 129 | +}; |
| 130 | + |
| 131 | +const printMarkdown = (failures, checked) => { |
| 132 | + console.log('| Adaptor | File | JSON pointer | Failure reason |'); |
| 133 | + console.log('| --- | --- | --- | --- |'); |
| 134 | + |
| 135 | + if (!failures.length) { |
| 136 | + console.log( |
| 137 | + `| - | - | - | No schema validation failures (${checked} files checked). |` |
| 138 | + ); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + for (const failure of failures) { |
| 143 | + console.log( |
| 144 | + `| ${escapeMarkdown(failure.adaptor)} | ${escapeMarkdown( |
| 145 | + failure.filePath |
| 146 | + )} | \`${escapeMarkdown(failure.pointer)}\` | ${escapeMarkdown( |
| 147 | + failure.reason |
| 148 | + )} |` |
| 149 | + ); |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +const printText = (failures, checked) => { |
| 154 | + if (!failures.length) { |
| 155 | + console.log(`Validated ${checked} configuration schema files.`); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + console.error('Configuration schema validation failed:'); |
| 160 | + console.error(); |
| 161 | + |
| 162 | + for (const failure of failures) { |
| 163 | + console.error(`- ${failure.filePath}`); |
| 164 | + console.error(` adaptor: ${failure.adaptor}`); |
| 165 | + console.error(` pointer: ${failure.pointer}`); |
| 166 | + console.error(` reason: ${failure.reason}`); |
| 167 | + } |
| 168 | +}; |
| 169 | + |
| 170 | +const main = async () => { |
| 171 | + if (help) { |
| 172 | + console.log(`Usage: pnpm validate:schemas [-- --markdown] |
| 173 | +
|
| 174 | +Validates every packages/*/*configuration-schema.json file against the JSON |
| 175 | +Schema draft-07 meta-schema. Use --markdown to print the one-shot audit table.`); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + const ajv = new Ajv({ |
| 180 | + allErrors: true, |
| 181 | + strict: false, |
| 182 | + validateSchema: true, |
| 183 | + }); |
| 184 | + ajv.addMetaSchema( |
| 185 | + draft07MetaSchema, |
| 186 | + 'https://json-schema.org/draft-07/schema#' |
| 187 | + ); |
| 188 | + |
| 189 | + const schemaFiles = await findSchemaFiles(); |
| 190 | + |
| 191 | + if (!schemaFiles.length) { |
| 192 | + console.error('No configuration schema files found.'); |
| 193 | + process.exitCode = 1; |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + const failures = ( |
| 198 | + await Promise.all(schemaFiles.map(file => validateSchemaFile(file, ajv))) |
| 199 | + ).flat(); |
| 200 | + |
| 201 | + if (markdown) { |
| 202 | + printMarkdown(failures, schemaFiles.length); |
| 203 | + } else { |
| 204 | + printText(failures, schemaFiles.length); |
| 205 | + } |
| 206 | + |
| 207 | + if (failures.length) { |
| 208 | + process.exitCode = 1; |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +main().catch(error => { |
| 213 | + console.error(error); |
| 214 | + process.exitCode = 1; |
| 215 | +}); |
0 commit comments