Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] JsonSchema Validation #26

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
213 changes: 212 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"moment": "^2.27.0",
"prettier": "^2.0.5",
"shelljs": "^0.8.4",
"simple-git": "^2.31.0"
"simple-git": "^2.31.0",
"yargs": "^17.0.1"
},
"description": "",
"main": "main.js",
"scripts": {
"labgen": "node main.js",
"validate-lab-descriptor": "node validateDescriptor.js",
"validate-schema": "node validation/validate.js",
"build-ds-exp": "node exp.js --env=testing ../",
"build-exp": "node exp.js --env=testing ../"
},
Expand Down
4 changes: 4 additions & 0 deletions validation/schema-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"posttest.json": "./schemas/quizv2Schema.json",
"pretest.json" : "./schemas/quizv2Schema.json"
}
65 changes: 65 additions & 0 deletions validation/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const Ajv = require("ajv");
const path = require("path");
const schemaMap = require("./schema-map.json");

const argv = require("yargs")(process.argv.slice(2))
.scriptName("validate")
.usage("$0 [args]")
.option("files", {
type: "array",
desc: "One or more files for validation",
demandOption: true,
}).check((argv) => {
if(argv.files.length === 0){
throw new Error('Empty Argument: Filenames cannot be empty');
}
})
.option("schema-map", {
type: "string",
desc: "The schema map file",
}).alias({
help: 'h',
version: 'v',
files: 'f',
schemaMap: 's',
}).argv;
raj-vlabs marked this conversation as resolved.
Show resolved Hide resolved
console.log(argv);

let validateSchema = (input = "1", schema = "1") => {
console.log("validating " + input + " against " + schema);
console.log("You got validated");
};

module.exports.validateSchema = validateSchema;

/**
* node validation/validate.js <jsonpath> <schema path>
*/
if (require.main === module) {
let json = "";
let schema = "";
console.log(process.argv.length);
switch (process.argv.length) {
case 2:
console.log("validating all");
validateSchema();
break;
case 3:
json = process.argv[2];
json = json.replace("/", "\\");
let n = json.lastIndexOf("\\");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use path or some function in fsx module to get the filename from a pathname.

let jsonKey = json.substring(n + 1);
console.log(json, n);
schema = schemaMap[jsonKey];
console.log(schema);
validateSchema(json, schema);
break;
case 4:
json = path.resolve(process.argv[2]);
schema = path.resolve(process.argv[3]);
validateSchema(json, schema);
break;
default:
console.log("Error");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default case should print the usage options

}
}