diff --git a/.github/workflows/validation-json-schema.yml b/.github/workflows/validation-json-schema.yml new file mode 100644 index 0000000..0a1b69c --- /dev/null +++ b/.github/workflows/validation-json-schema.yml @@ -0,0 +1,17 @@ +# GitHub Action to validate schema and example files +# https://github.com/codespell-project/actions-codespell +name: Validation - JSON-Schema +on: [push, pull_request] +jobs: + validate-schema: + name: Validate our Schema + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Node + uses: actions/setup-node@v1 + with: + node-version: '12' + - name: Install dependencies + run: npm install @cfworker/json-schema + - run: node --unhandled-rejections=strict validate-schema.mjs diff --git a/.gitignore b/.gitignore index e43b0f9..9daa824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +node_modules diff --git a/rdm-schema.json b/rdm-schema.json index 259fd5f..6ffa4c0 100644 --- a/rdm-schema.json +++ b/rdm-schema.json @@ -3,7 +3,6 @@ "$schema": "https://json-schema.org/draft/2019-09/schema", "title": "Parameter Message", "description": "The schema for the Parameter Metadata Language from Section 5 of E1.37-5. This schema is subject to change.", - "type": "object", "$ref": "#/$defs/commonPropertiesForNamed", "properties": { "manufacturer_id": { diff --git a/validate-schema.mjs b/validate-schema.mjs new file mode 100644 index 0000000..6d6a7a6 --- /dev/null +++ b/validate-schema.mjs @@ -0,0 +1,64 @@ +import { promises as fs } from 'fs'; +import { Validator } from '@cfworker/json-schema'; +import * as path from "path" +// const path = require("path"); +// const JsonSchema = require("@cfworker/json-schema"); +//import RdmSchema from "./rdm-schema.json" +//const { RdmSchema } = JSON.parse(fs.readFile('./rdm-schema.json')); + +async function validate(validator, filename) { + // JsonSchema.setShouldMetaValidate(true); + // JsonSchema.setMetaOutputFormat(JsonSchema.VERBOSE); + + // Fetch from file + const fileContent = await fs.readFile(path.resolve() + filename) + console.log("Checking file " + path.resolve() + filename); + // console.log("Contents:"); + // console.log(JSON.parse(fileContent.toString())); + const output = validator.validate(JSON.parse(fileContent.toString())) + // const output = await JsonSchema.validate(schema, "file://" + filename, JsonSchema.VERBOSE); + // console.log(output); + if (output.valid) { + console.log("File " + filename + " is valid :-)"); + } else { + console.log("File " + filename + " is invalid :-(", output.errors); process.exitCode = 1 + } +} + +//From https://gist.github.com/lovasoa/8691344#file-node-walk-es6 +async function* walk(dir) { + for await (const d of await fs.opendir(path.resolve() + dir)) { + const entry = path.join(dir, d.name); + if (d.isDirectory()) yield* walk(entry); + else if (d.isFile()) yield entry; + } +} + +async function validateAllFiles(validator, exampleDir) { + for await (const file of walk(exampleDir)) { + if (/\.json$/.test(file)) { + await validate(validator, file); + } + } +} + +(async function() { + console.log("Loading schema"); + const rawSchema = await fs.readFile(path.resolve() + "/rdm-schema.json"); + //console.log("Got raw schema:"); + //console.log(rawSchema.toString()); + //console.log("Got parsed JSON:"); + //console.log(JSON.parse(rawSchema.toString())) + const RdmSchema = JSON.parse(rawSchema.toString()); + console.log("Got schema:"); + console.log(RdmSchema); + + console.log("Prepping validator"); + const validator = new Validator(RdmSchema, '2019-09'); + validator.addSchema("https://json-schema.org/draft/2019-09/schema") + + console.log("Validating single file"); + validate(validator, "/examples/e1.20/BOOT_SOFTWARE_VERSION_ID.json"); + console.log("Validating all examples"); + validateAllFiles(validator, "/examples/"); + })();