Skip to content

Commit 8077ea5

Browse files
committed
#4 - Add JSON Schema validator
1 parent c8169df commit 8077ea5

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

tooling/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"scripts": {
99
"test": "echo \"Error: no test specified\" && exit 1",
1010
"build": "babel src -d out --extensions \".ts,.tsx\"",
11-
"compile": "rimraf out && tsc -p ./ --declaration"
11+
"compile": "rimraf out && tsc -p ./ --declaration",
12+
"watch": "tsc -watch -p ./ --declaration"
1213
},
1314
"dependencies": {
1415
"@jsonforms/core": "^2.0.12-rc.3",
16+
"ajv": "^6.5.5",
1517
"npm": "^6.4.1",
1618
"simple-git": "^1.107.0"
1719
},

tooling/src/index.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as simplegit from 'simple-git/promise';
22
import * as jsonforms from '@jsonforms/core';
33
import * as cp from 'child_process';
44
import { writeFile, readFile } from 'fs';
5+
import * as Ajv from 'ajv';
56
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
67

78
/*
@@ -40,18 +41,53 @@ export function cloneAndInstall(repo: String, path: string, callback: (result: s
4041
* @param {function} callback forwards the current status to the caller
4142
*/
4243
export function generateUISchema(path: string, callback: (result: string, type?: string) => void) {
44+
// Read JSON Schema file
4345
readFile(path, 'utf8', (err, data) => {
44-
if (err) callback(err.message, 'err');
45-
var jsonSchema = JSON.parse(data);
46-
var jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);
47-
if(process.platform === 'win32') {
48-
var newPath = path.substring(0, path.lastIndexOf("\\"))+'\\ui-schema.json';
49-
} else {
50-
var newPath = path.substring(0, path.lastIndexOf("/"))+'/ui-schema.json';
46+
if (err) {
47+
callback(err.message, 'err');
48+
return;
5149
}
52-
writeFile(newPath, JSON.stringify(jsonUISchema,null, 2), (err) => {
53-
if (err) callback(err.message, 'err');
54-
callback('Successfully generated UI schema');
50+
51+
var jsonSchema = JSON.parse(data);
52+
validateJSONSchema(jsonSchema, function(err?: string) {
53+
if (err) {
54+
callback(err, 'err');
55+
return;
56+
}
57+
58+
var jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);
59+
60+
// Check if windows or linux filesystem
61+
if(process.platform === 'win32') {
62+
var newPath = path.substring(0, path.lastIndexOf("\\"))+'\\ui-schema.json';
63+
} else {
64+
var newPath = path.substring(0, path.lastIndexOf("/"))+'/ui-schema.json';
65+
}
66+
67+
// Write UI Schema file
68+
writeFile(newPath, JSON.stringify(jsonUISchema,null, 2), (err) => {
69+
if (err) {
70+
callback(err.message, 'err');
71+
return;
72+
}
73+
74+
callback('Successfully generated UI schema');
75+
});
5576
});
5677
});
5778
}
79+
80+
/**
81+
* Validate a given JSON Schema
82+
* @param {string} path path to the json schema file
83+
* @param {function} callback forwards the current status to the caller
84+
*/
85+
function validateJSONSchema(schema: Object, callback: (err?: string) => void) {
86+
var ajv = new Ajv();
87+
try {
88+
ajv.compile(schema);
89+
callback();
90+
} catch (error) {
91+
callback(error.message);
92+
}
93+
}

0 commit comments

Comments
 (0)