Skip to content

Commit b1c6ce6

Browse files
authored
Merge pull request #180 from jay-oswald/profile_validate
Add json validation to vehicle profiles
2 parents aceefdb + 5db6bd2 commit b1c6ce6

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# @jay-oswald owns the vehicle_profiles JS + Action
55
.vehicle_profiles/ @jay-oswald
66
.github/workflows/vehicle_profiles.yml @jay-oswald
7+
.github/workflows/vehicle_profiles_validate.yml @jay-oswald
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Validate Vehicle Profiles
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
validate-vehicle-profiles:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- name: Setup Node
11+
uses: actions/setup-node@v4
12+
with:
13+
node-version: '20.x'
14+
- run: |
15+
cd .vehicle_profiles
16+
npm ci
17+
npm run validate

.vehicle_profiles/package-lock.json

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vehicle_profiles/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"main": "merge.js",
66
"scripts": {
77
"build": "node merge.js",
8+
"validate": "node validate.js",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
1011
"author": "jay-oswald",
1112
"type": "module",
1213
"dependencies": {
14+
"ajv": "^8.17.1",
1315
"glob": "^11.0.0"
1416
}
1517
}

.vehicle_profiles/schema.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"title": "WiCan Vehicle Profile",
3+
"description": "Data about various models of cars and their settings",
4+
"type": "object",
5+
"properties": {
6+
"car_model": {
7+
"description": "Name of car model, should include Make and Model(s)",
8+
"type": "string"
9+
},
10+
"init": {
11+
"description": "Init String, don't forget the final ;",
12+
"type": "string"
13+
},
14+
"pids": {
15+
"description": "Array of PID's and their parameters",
16+
"type": "array",
17+
"minItems": 1,
18+
"items": {
19+
"type": "object",
20+
"properties": {
21+
"pid": {
22+
"description": "The PID for this group of parameters",
23+
"type": "string"
24+
},
25+
"parameters": {
26+
"description": "Array of Parameters for this PID",
27+
"type": "array",
28+
"minItems": 1,
29+
"items": {
30+
"type": "object",
31+
"properties": {
32+
"name": {
33+
"description": "Used in MQTT JSON, no spaces",
34+
"type": "string"
35+
},
36+
"expression": {
37+
"description": "Expression to calculate value",
38+
"type": "string"
39+
},
40+
"unit": {
41+
"description": "Unit for this parameter",
42+
"type": "string"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"additionalProperties": false,
52+
"required": [ "car_model", "init", "pids"]
53+
}

.vehicle_profiles/validate.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Ajv from "ajv"
2+
import { readFile } from 'fs/promises';
3+
import { glob } from 'glob'
4+
5+
const ajv = new Ajv()
6+
const schema = JSON.parse(await readFile("schema.json", "utf-8"))
7+
const validate = ajv.compile(schema)
8+
9+
const source_folder = '../vehicle_profiles';
10+
const files = await glob(source_folder + '/**/*.json')
11+
12+
let errors = 0;
13+
14+
async function validate_profile(path){
15+
let file = await readFile(path, "utf-8");
16+
let data = JSON.parse(file);
17+
let valid = validate(data);
18+
if(valid) return
19+
20+
console.log(path)
21+
console.log(validate.errors)
22+
errors++;
23+
}
24+
25+
let promises = [];
26+
files.forEach(file => {
27+
promises.push(validate_profile(file));
28+
});
29+
30+
await Promise.all(promises);
31+
32+
if(errors > 0){
33+
throw new Error('Validation issues found, see log for details');
34+
}

0 commit comments

Comments
 (0)