All files / packages/sds-metadata/scripts validate.js

0% Statements 0/23
0% Branches 0/8
0% Functions 0/2
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                 
import Ajv from 'ajv';
import { readFile } from 'fs/promises';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
 
const distDir = resolve(__dirname, '../dist');
const schemasDir = resolve(__dirname, '../schemas');
 
const validations = [
    {
        name: 'Global Styling Hooks',
        schema: 'globalStylingHooks.schema.json',
        data: 'globalStylingHooks.metadata.json'
    },
    {
        name: 'Icons',
        schema: 'icons.schema.json',
        data: 'icons.json'
    }
];
 
async function validate() {
    console.log('Validating generated metadata:');
 
    const ajv = new Ajv({ allErrors: true });
    let hasWarnings = false;
 
    for (const { name, schema: schemaFile, data: dataFile } of validations) {
        const schema = JSON.parse(await readFile(resolve(schemasDir, schemaFile), 'utf8'));
        const data = JSON.parse(await readFile(resolve(distDir, dataFile), 'utf8'));
 
        const validateFn = ajv.compile(schema);
        const valid = validateFn(data);
 
        if (!valid && validateFn.errors) {
            hasWarnings = true;
            console.log(`⚠️  ${name}`);
            validateFn.errors.forEach(err => {
                console.warn(`   - ${err.instancePath || '/'}: ${err.message}`);
            });
        } else {
            console.log(`\x1b[32m✔\x1b[0m ${name}`);
        }
    }
 
    if (hasWarnings) {
        console.log('⚠️  Validation complete with warnings.');
    } else {
        console.log('✅ Validation complete with no warnings.');
    }
}
 
validate();