Skip to content

Commit

Permalink
Use ESM in validate-json-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Feb 5, 2025
1 parent 70ba048 commit f5805e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint": "eslint . && prettier . --check && npm run rule-syntax-check",
"bundle": "./build.sh",
"generate-rule-schema": "node scripts/generate-rule-schema.mjs",
"rule-syntax-check": "npm run generate-rule-schema && node scripts/validate-json-rules.js",
"rule-syntax-check": "npm run generate-rule-schema && node scripts/validate-json-rules.mjs",
"watch": "npm run prepublish && chokidar \"lib\" \"addon\" \"rules/autoconsent\" \"rules/filterlist.txt\" --ignore 'lib/filterlist-engine.ts' -c \"npm run prepublish\"",
"create-rule": "node rules/create-rule.mjs",
"test": "playwright test",
Expand Down
21 changes: 12 additions & 9 deletions scripts/validate-json-rules.js → scripts/validate-json-rules.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const Ajv = require('ajv').default;
const fs = require('fs');
const path = require('path');
import Ajv from 'ajv';
import { existsSync, readFileSync, readdirSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const rootDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');

function formatErrors(errors) {
if (!Array.isArray(errors)) {
Expand All @@ -10,22 +13,22 @@ function formatErrors(errors) {
return errors.map((item) => `${item.instancePath}: ${item.message}`).join(', ');
}

const schemaPath = path.join(__dirname, '../rules/schema.json');
if (!fs.existsSync(schemaPath)) {
const schemaPath = path.join(rootDir, 'rules/schema.json');
if (!existsSync(schemaPath)) {
console.error(`Schema file does not exist at: ${schemaPath}`);
process.exit(1);
}

const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
const schema = JSON.parse(readFileSync(schemaPath, 'utf8'));
const ajv = new Ajv({ allowUnionTypes: true });
const validator = ajv.compile(schema);

const rulesDir = path.join(__dirname, '../rules/autoconsent');
const ruleFiles = fs.readdirSync(rulesDir).filter((f) => f.endsWith('.json'));
const rulesDir = path.join(rootDir, 'rules/autoconsent');
const ruleFiles = readdirSync(rulesDir).filter((f) => f.endsWith('.json'));

for (const ruleFile of ruleFiles) {
const rulePath = path.join(rulesDir, ruleFile);
const ruleJson = JSON.parse(fs.readFileSync(rulePath, 'utf8'));
const ruleJson = JSON.parse(readFileSync(rulePath, 'utf8'));

const valid = validator(ruleJson);
if (!valid) {
Expand Down

0 comments on commit f5805e8

Please sign in to comment.