Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce schema validation on pre-commit. #32

Merged
merged 7 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Git client-side hooks

This directory contains client-side Git hooks to ensure schema
validation. To set them up run

git config core.hooksPath .githooks

in this repo checkout. This will only apply to this checkout.

**BE CAREFUL**. This will set git up to automatically run code checked
out from this repo. Make sure you know what you're pulling!
7 changes: 7 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

set -e

cd validate
npm install --save=false
node validate.js
16 changes: 16 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Validate

on:
pull_request:
branches:
- 'main'

jobs:
# Check
validate:
name: Validate
runs-on: ubuntu-latest
container: node:16
steps:
- uses: actions/checkout@v3
- run: cd validate && npm install --save=false && node validate.js
1 change: 1 addition & 0 deletions validate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 18 additions & 0 deletions validate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "validate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@root/walk": "^1.1.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1"
}
}
88 changes: 88 additions & 0 deletions validate/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Validate JSON schemas */

import $fsp from "fs/promises";
import $path from "path";
import $process from "process";

import Ajv2020 from "ajv/dist/2020.js";
import formats from "ajv-formats";
import Walk from "@root/walk";

const baseURL = "https://raw.githubusercontent.com/AMRC-FactoryPlus/schemas/main/";

let exit = 0;
function error (...args) {
console.log(...args);
exit = 1;
}

function loadJSON (path) {
return $fsp.readFile(path, { encoding: "utf-8" })
.then(JSON.parse);
}

function pathFor (id) {
if (!id.startsWith(baseURL))
throw `Incorrect schema $id: ${id}`;

return $path.resolve("..", id.slice(baseURL.length));
}

function loadSchema (id) {
const path = pathFor(id);
//console.log(`Loading ${id} from ${path}`);
return loadJSON(pathFor(id))
.catch(err => ({}));
}

function schemaWalker (schemas) {
const cwd = $path.resolve();

return async (err, path, dirent) => {
if (err) throw err;
const abs = $path.resolve(path);

/* Skip this directory (lots of schema under node_modules) */
if (dirent.isDirectory())
return abs != cwd;

if (!path.endsWith(".json")) return;

const json = await loadJSON(path)
.catch(err => {
error("JSON error in %s: %s",
abs, err.message);
});
if (!(typeof json == "object" && "$id" in json)) return;

const id = json.$id;
if (pathFor(id) != abs)
error(`Incorrect $id in ${abs}`);

schemas.push(json);
};
}

const ajv = new Ajv2020({
loadSchema,
addUsedSchema: false,
/* XXX We may want to revist these exceptions. Currently they are
* violated everywhere. */
strictTypes: false,
allowMatchingProperties: true,
});
formats(ajv);

const schemas = [];
await Walk.walk("..", schemaWalker(schemas));

for (const sch of schemas) {
//console.log(`Checking ${sch.$id}`);
await ajv.compileAsync(sch)
.catch(err => {
const path = pathFor(sch.$id);
error(`Error in ${path}: ${err}`);
});
}

$process.exit(exit);