Skip to content

Commit

Permalink
Merge pull request #276 from virtual-labs/lab-desc-upgrade
Browse files Browse the repository at this point in the history
Modified lab-descriptor schema
  • Loading branch information
raj-vlabs authored Oct 15, 2023
2 parents bbd6aa6 + 4657678 commit f5cec8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"description": "* Introduction",
"main": "main.js",
"scripts": {
"validate-lab-descriptor": "node validateDescriptor.js",
"validate-lab-descriptor": "node validation/validate_descriptor.js",
"build-exp": "node main.js build --validateEslint --validateExpdesc --src=../",
"clean-build-exp": "node main.js build --clean --validateEslint --validateExpdesc --src=../",
"build-exp-deploy": "node main.js build --validateEslint --validateExpdesc --deploy --src=../",
Expand Down
14 changes: 10 additions & 4 deletions validation/schemas/labDescSchema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "http://json-schema.org/schema#",
"$id": "https://schema.vlabs.ac.in/lab-descriptor.schema.json",
"title": "Lab Descriptor",
"description": "Schema for a Lab Descriptor defining the properties of a Virtual Lab",
"type": "object",
"required": ["lab", "broadArea", "phase", "collegeName",
"baseUrl", "introduction", "objective",
Expand All @@ -14,11 +17,14 @@
"type": "object",
"properties": {
"name": {"type": "string"},
"link": {"type": "string", "format": "uri"}
}
"link": {"type": "string", "format": "uri"},
"code": {"enum": ["BIO", "CHEMENG", "CHEMSCI", "CIVIL", "CSE",
"DESIGN", "ECE", "EE", "MECH", "PHYSCI"]}
},
"required": ["code"]
},
"phase": {"type": "number"},
"collegeName": {"type": "string"},
"phase": {"enum": [2, 3, "3-ext"]},
"collegeName": {"enum": ["AMRT", "COEP", "DLBG", "IIITH", "IITB", "IITD", "IITG", "IITK", "IITKGP", "IITR", "NITK"]},
"baseUrl": {
"type": "string",
"format": "hostname"
Expand Down
35 changes: 20 additions & 15 deletions validation/validate_descriptor.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
const Ajv = require('ajv');
const addFormats = require("ajv-formats")
const addFormats = require("ajv-formats");
const figures = require('figures');
const chalk = require('chalk');
const path = require('path');

function reportJSONError(error) {
const heading =
chalk`Error detected in {blue {italic lab-descriptor.json}}`;
chalk`Error detected in {blue {italic lab-descriptor.json}}`;
console.log(chalk`${heading}`);

if (error.keyword === 'additionalProperties') {
const msg = `please remove: ${error.params.additionalProperty}`;
logError(error.dataPath, msg);
logError(error.instancePath, msg);
}
else {
logError(error.dataPath, error.message);
logError(error.instancePath, error.message);
}
}

function logError(dataPath, textMsg) {
let delim =chalk` {yellow ${figures.pointerSmall}} `;
let msg = chalk` {yellow ${textMsg}}`;
dp = dataPath.replace(/\./g, delim);
console.log(`\n${dp}\n${msg}\n`);
let delim = chalk` {yellow ${figures.pointerSmall}} `;
let msg = chalk` {yellow ${textMsg}}`;
dp = dataPath && dataPath.replace(/\./g, delim);
dp && console.log(`\n${dp}`);
console.log(`${msg}\n`);
}


function validateLabDescriptor(lab_descriptor_path){
function validateLabDescriptor(lab_descriptor_path) {
const validator = new Ajv();
addFormats(validator);
const validate = validator.compile(require("./schemas/labDescSchema.json"));
const valid = validate(require(lab_descriptor_path));

if (!valid) {
if (validate.errors) {
reportJSONError(validate.errors[0]);
for (error of validate.errors) {
reportJSONError(error);
}
return false;
}
Expand All @@ -44,12 +45,16 @@ function validateLabDescriptor(lab_descriptor_path){

module.exports = {
validateLabDescriptor,
}
};


/*
node validateDescriptor.js <path/to/lab>
node validate_descriptor.js <path/to/lab>
*/
if (require.main === module) {
validateLabDescriptor(path.resolve(process.argv[2], "lab-descriptor.json"));
if (process.argv[2]) {
validateLabDescriptor(path.resolve(process.argv[2], "lab-descriptor.json"));
} else {
logError("", "Please provide the path of the Lab base directory");
}
}

0 comments on commit f5cec8e

Please sign in to comment.