Skip to content

Commit

Permalink
Add json validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-oswald committed Aug 20, 2024
1 parent aceefdb commit 2569569
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/vehicle_profiles_validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Validate Vehicle Profiles

on: [pull_request]

jobs:
validate-vehicle-profiles:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: |
cd .vehicle_profiles
npm ci
npm run validate
39 changes: 39 additions & 0 deletions .vehicle_profiles/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .vehicle_profiles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"main": "merge.js",
"scripts": {
"build": "node merge.js",
"validate": "node validate.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "jay-oswald",
"type": "module",
"dependencies": {
"ajv": "^8.17.1",
"glob": "^11.0.0"
}
}
53 changes: 53 additions & 0 deletions .vehicle_profiles/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"title": "WiCan Vehicle Profile",
"description": "Data about various models of cars and their settings",
"type": "object",
"properties": {
"car_model": {
"description": "Name of car model, should include Make and Model(s)",
"type": "string"
},
"init": {
"description": "Init String, don't forget the final ;",
"type": "string"
},
"pids": {
"description": "Array of PID's and their parameters",
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"pid": {
"description": "The PID for this group of parameters",
"type": "string"
},
"parameters": {
"description": "Array of Parameters for this PID",
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"name": {
"description": "Used in MQTT JSON, no spaces",
"type": "string"
},
"expression": {
"description": "Expression to calculate value",
"type": "string"
},
"unit": {
"description": "Unit for this parameter",
"type": "string"
}
}
}
}
}
}
}
},
"additionalProperties": false,
"required": [ "car_model", "init", "pids"]
}
34 changes: 34 additions & 0 deletions .vehicle_profiles/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Ajv from "ajv"
import { readFile } from 'fs/promises';
import { glob } from 'glob'

const ajv = new Ajv()
const schema = JSON.parse(await readFile("schema.json", "utf-8"))
const validate = ajv.compile(schema)

const source_folder = '../vehicle_profiles';
const files = await glob(source_folder + '/**/*.json')

let errors = 0;

async function validate_profile(path){
let file = await readFile(path, "utf-8");
let data = JSON.parse(file);
let valid = validate(data);
if(valid) return

console.log(path)
console.log(validate.errors)
errors++;
}

let promises = [];
files.forEach(file => {
promises.push(validate_profile(file));
});

await Promise.all(promises);

if(errors > 0){
throw new Error('Validation issues found, see log for details');
}

0 comments on commit 2569569

Please sign in to comment.