Skip to content

Commit 36501a3

Browse files
committed
[validator] switch to deno
1 parent 265414f commit 36501a3

File tree

7 files changed

+64
-278
lines changed

7 files changed

+64
-278
lines changed

.github/workflows/validate.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ on:
88

99
jobs:
1010
validate:
11-
runs-on: ubuntu-22.04
11+
runs-on: ubuntu-24.04
1212
steps:
13-
- uses: actions/checkout@v4
14-
- uses: actions/setup-node@v4
13+
- uses: actions/checkout@v6
14+
- uses: denoland/setup-deno@v2
1515
with:
16-
node-version: '20.x'
17-
- run: cd ./validator && yarn install --frozen-lockfile
18-
- run: node ./validate.js --ci
16+
cache: true
17+
- run: deno -R -E ./validate.js --ci

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ Contributions are always welcome! Use the [Scraping Configuration](https://githu
8989

9090
The scrapers in this repository can be validated against a schema and checked for common errors.
9191

92-
First, install the validator's dependencies - inside the [`./validator`](./validator) folder, run: `yarn`.
92+
To run the validator, use `deno run -R=scrapers -R="validator\scraper.schema.json" validate.js` in the root of the repository.
93+
Specific scrapers can be checked using: `deno run -R=scrapers -R="validator\scraper.schema.json" scrapers/foo.yml scrapers/bar.yml`
9394

94-
Then, to run the validator, use `node validate.js` in the root of the repository.
95-
Specific scrapers can be checked using: `node validate.js scrapers/foo.yml scrapers/bar.yml`
95+
Deno asks for env and sys permissions, these seem to mostly be from [chalk](https://www.npmjs.com/package/chalk)
9696

9797
#### Docker option
98-
Instead of NodeJS being installed, Docker can be used to run the validator
98+
Instead of Deno being installed, Docker can be used to run the validator
9999

100100
```bash
101-
docker run --rm -v .:/app node:alpine /bin/sh -c "cd /app/validator && yarn install --silent && cd .. && node validate.js --ci"
101+
docker run --rm -v .:/app denoland/deno:distroless run -R=/app/ -E /app/validate.js --ci
102102
```

validate.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
#!/usr/bin/env node
2-
'use strict';
3-
require('./validator/index.js')();
1+
import "./validator/index.mjs"
Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
#!/usr/bin/env node
2-
'use strict';
3-
4-
const fs = require('fs');
5-
const path = require('path');
6-
7-
const safeRequire = (name) => {
8-
try {
9-
return require(name);
10-
} catch (error) {
11-
if (error && error.code === 'MODULE_NOT_FOUND') {
12-
console.log(`Error: Cannot find module '${name}', have you installed the dependencies?`);
13-
process.exit(1);
14-
}
15-
throw error;
16-
}
17-
};
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import Ajv from "npm:ajv@8"
185

19-
const Ajv = safeRequire('ajv').default;
20-
const betterAjvErrors = safeRequire('better-ajv-errors').default;
21-
const chalk = safeRequire('chalk');
22-
const YAML = safeRequire('yaml');
23-
const addFormats = safeRequire('ajv-formats');
6+
import betterAjvErrors from 'npm:better-ajv-errors@2';
7+
import chalk from 'npm:chalk@5';
8+
import { parse } from 'npm:yaml@2';
9+
import addFormats from "npm:ajv-formats@3"
2410

2511
// https://www.peterbe.com/plog/nodejs-fs-walk-or-glob-or-fast-glob
2612
function walk(directory, ext, filepaths = []) {
@@ -46,7 +32,7 @@ class Validator {
4632
this.sortedURLs = flags.includes('-s');
4733
this.verbose = flags.includes('-v');
4834

49-
const schemaPath = path.resolve(__dirname, './scraper.schema.json');
35+
const schemaPath = path.resolve(import.meta.dirname, './scraper.schema.json');
5036
this.schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
5137
this.ajv = new Ajv({
5238
// allErrors: true,
@@ -63,7 +49,7 @@ class Validator {
6349
if (files && Array.isArray(files) && files.length > 0) {
6450
scrapers = files.map(file => path.resolve(file));
6551
} else {
66-
const scrapersDir = path.resolve(__dirname, '../scrapers');
52+
const scrapersDir = path.resolve(import.meta.dirname, '../scrapers');
6753
scrapers = walk(scrapersDir, '.yml');
6854
}
6955

@@ -81,7 +67,7 @@ class Validator {
8167
let contents, data;
8268
try {
8369
contents = fs.readFileSync(file, 'utf8');
84-
data = YAML.parse(contents, yamlLoadOptions);
70+
data = parse(contents, yamlLoadOptions);
8571
} catch (error) {
8672
console.error(`${chalk.red(chalk.bold('ERROR'))} in: ${relPath}:`);
8773
error.stack = null;
@@ -337,7 +323,7 @@ class Validator {
337323
}
338324
}
339325

340-
function main(flags, files) {
326+
export function main(flags, files) {
341327
const args = process.argv.slice(2)
342328
flags = (flags === undefined) ? args.filter(arg => arg.startsWith('-')) : flags;
343329
files = (files === undefined) ? args.filter(arg => !arg.startsWith('-')) : files;
@@ -347,10 +333,6 @@ function main(flags, files) {
347333
process.exit(result ? 0 : 1);
348334
}
349335
}
336+
export default main
350337

351-
if (require.main === module) {
352-
main();
353-
}
354-
355-
module.exports = main;
356-
module.exports.Validator = Validator;
338+
main()

validator/package.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

validator/scraper.schema.json

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,14 @@
252252
}
253253
},
254254
"oneOf": [
255-
{ "required": ["ValueRandom"] },
256-
{ "required": ["Value"] }
255+
{
256+
"required": ["ValueRandom"],
257+
"properties": { "ValueRandom": { "type": "integer", "minimum": 1 } }
258+
},
259+
{
260+
"required": ["Value"],
261+
"properties": { "Value": { "type": "string" } }
262+
}
257263
]
258264
}
259265
}
@@ -348,15 +354,27 @@
348354
"action": { "enum": ["scrapeXPath", "scrapeJson"] }
349355
}
350356
},
351-
"then": { "required": ["scraper"] }
357+
"then": {
358+
"required": ["scraper"],
359+
"properties": { "scraper": { "type": "string" } }
360+
}
352361
},
353362
{
354363
"if": {
355364
"properties": {
356365
"action": { "const": "script" }
357366
}
358367
},
359-
"then": { "required": ["script"] }
368+
"then": {
369+
"required": ["script"],
370+
"properties": {
371+
"script": {
372+
"type": "array",
373+
"minItems": 1,
374+
"items": { "type": "string" }
375+
}
376+
}
377+
}
360378
}
361379
]
362380
}
@@ -411,15 +429,30 @@
411429
"action": { "enum": ["scrapeXPath", "scrapeJson"] }
412430
}
413431
},
414-
"then": { "required": ["scraper", "queryURL"] }
432+
"then": {
433+
"required": ["scraper", "queryURL"],
434+
"properties": {
435+
"scraper": { "type": "string" },
436+
"queryURL": { "type": "string" }
437+
}
438+
}
415439
},
416440
{
417441
"if": {
418442
"properties": {
419443
"action": { "const": "script" }
420444
}
421445
},
422-
"then": { "required": ["script"] }
446+
"then": {
447+
"required": ["script"],
448+
"properties": {
449+
"script": {
450+
"type": "array",
451+
"minItems": 1,
452+
"items": { "type": "string" }
453+
}
454+
}
455+
}
423456
}
424457
]
425458
},

0 commit comments

Comments
 (0)