Skip to content

Commit

Permalink
#4 - Add JSON Schema validator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoker committed Nov 19, 2018
1 parent c8169df commit 8077ea5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
4 changes: 3 additions & 1 deletion tooling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src -d out --extensions \".ts,.tsx\"",
"compile": "rimraf out && tsc -p ./ --declaration"
"compile": "rimraf out && tsc -p ./ --declaration",
"watch": "tsc -watch -p ./ --declaration"
},
"dependencies": {
"@jsonforms/core": "^2.0.12-rc.3",
"ajv": "^6.5.5",
"npm": "^6.4.1",
"simple-git": "^1.107.0"
},
Expand Down
56 changes: 46 additions & 10 deletions tooling/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as simplegit from 'simple-git/promise';
import * as jsonforms from '@jsonforms/core';
import * as cp from 'child_process';
import { writeFile, readFile } from 'fs';
import * as Ajv from 'ajv';
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';

/*
Expand Down Expand Up @@ -40,18 +41,53 @@ export function cloneAndInstall(repo: String, path: string, callback: (result: s
* @param {function} callback forwards the current status to the caller
*/
export function generateUISchema(path: string, callback: (result: string, type?: string) => void) {
// Read JSON Schema file
readFile(path, 'utf8', (err, data) => {
if (err) callback(err.message, 'err');
var jsonSchema = JSON.parse(data);
var jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);
if(process.platform === 'win32') {
var newPath = path.substring(0, path.lastIndexOf("\\"))+'\\ui-schema.json';
} else {
var newPath = path.substring(0, path.lastIndexOf("/"))+'/ui-schema.json';
if (err) {
callback(err.message, 'err');
return;
}
writeFile(newPath, JSON.stringify(jsonUISchema,null, 2), (err) => {
if (err) callback(err.message, 'err');
callback('Successfully generated UI schema');

var jsonSchema = JSON.parse(data);
validateJSONSchema(jsonSchema, function(err?: string) {
if (err) {
callback(err, 'err');
return;
}

var jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);

// Check if windows or linux filesystem
if(process.platform === 'win32') {
var newPath = path.substring(0, path.lastIndexOf("\\"))+'\\ui-schema.json';
} else {
var newPath = path.substring(0, path.lastIndexOf("/"))+'/ui-schema.json';
}

// Write UI Schema file
writeFile(newPath, JSON.stringify(jsonUISchema,null, 2), (err) => {
if (err) {
callback(err.message, 'err');
return;
}

callback('Successfully generated UI schema');
});
});
});
}

/**
* Validate a given JSON Schema
* @param {string} path path to the json schema file
* @param {function} callback forwards the current status to the caller
*/
function validateJSONSchema(schema: Object, callback: (err?: string) => void) {
var ajv = new Ajv();
try {
ajv.compile(schema);
callback();
} catch (error) {
callback(error.message);
}
}

0 comments on commit 8077ea5

Please sign in to comment.