Skip to content

Commit

Permalink
validate command JSON files
Browse files Browse the repository at this point in the history
Signed-off-by: pogi7 <[email protected]>
  • Loading branch information
pogi7 committed May 16, 2024
1 parent 3e352b5 commit b52bcaf
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
24 changes: 24 additions & 0 deletions controller/src/interfaces/ICommandSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Defines the structure of the JSON object that is received from the JSON files in the commands directory.
*
* @field name - The name of the command
* @field id - The id of the command
* @field command - The command object
*
*/
export default interface ICommandSchema {
name: string;
id: string;
command: Command;
}

/**
* Defines the structure of the command
*
* @field type - CRUD command
*
*/
interface Command {
type: string;
}

3 changes: 1 addition & 2 deletions controller/src/interfaces/ISparqlConfigSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Defines the structure of the object that is received from the sparqlConfig.json file.
* Defines the structure of the JSON object that is received from the sparqlConfig.json file.
*
* @remarks
* This interface relates to {@link https://jena.apache.org/documentation/fuseki2/fuseki-server-info.html | Fuseki endpoints}.
Expand All @@ -9,7 +9,6 @@
* @field updateInferenceEndpoint - The update endpoint for inferences
* @field pingEndpoint - The ping endpoint
*
*
*/
export default interface ISparqlConfigSchema {
queryEndpoint: string;
Expand Down
38 changes: 38 additions & 0 deletions controller/src/schemas/commandSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ICommandSchema from '../interfaces/ICommandSchema';
import { JSONSchemaType } from "ajv";

/**
* This JSON schema is used with AJV to validate JSON files stored in the commands directory that is stored within OML models.
*
* @remarks
* This constant will help validate JSON data using {@link https://ajv.js.org | AJV}.
*
* The data within the constant was generated with {@link https://www.jsonschema.net | JSON Schema}.
*
*/

export const commandSchema: JSONSchemaType<ICommandSchema[]> = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "string"
},
"command": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"required": ["type"]
}
},
"required": ["name", "id", "command"]
}
}
8 changes: 6 additions & 2 deletions controller/src/schemas/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ajv, { JSONSchemaType } from "ajv";
import ISparqlConfigSchema from "../interfaces/ISparqlConfigSchema";
import ICommandSchema from "../interfaces/ICommandSchema";

/**
* This schema validator uses AJV to validate JSON data based on a schema interface.
Expand All @@ -11,9 +12,12 @@ import ISparqlConfigSchema from "../interfaces/ISparqlConfigSchema";
*
* @param schema: The JSON schema that will be used to validate the data
* @param data: The JSON data that will be validated against a schema
*
*
*/
export const validateSchema = (schema: JSONSchemaType<ISparqlConfigSchema>, data: Object) => {
export const validateSchema = (
schema: JSONSchemaType<ISparqlConfigSchema | ICommandSchema[]>,
data: Object
) => {
const ajv = new Ajv();

// Validates the provided schema
Expand Down
17 changes: 10 additions & 7 deletions controller/src/utilities/loaders/loadCommandFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { workspace, Uri, commands, window, FileType } from "vscode";
import { TablePanel } from "../../panels/TablePanel";
import { PropertyPanelProvider } from "../../panels/PropertyPanelProvider";
import { validateSchema } from "../../schemas/validator";
import { commandSchema } from "../../schemas/commandSchema";
// TODO: handle multiple workspaces (currently assumes model is in the 1st)

/**
Expand Down Expand Up @@ -35,7 +37,14 @@ export const loadCommandFiles = async (
const fileUri = Uri.joinPath(commandFolderUri, file);
const buffer = await workspace.fs.readFile(fileUri);
const content = JSON.parse(buffer.toString());

// Validate if the content matches the JSON Command Schema
const validate = validateSchema(commandSchema, content);
if (files.length > 0 && validate) {
commands.executeCommand("setContext", "vision:hasCommand", true);
window.showInformationMessage("Command files loaded successfully.");
} else {
window.showErrorMessage(`Invalid or missing ${file}.`);
}
try {
TablePanel.updateCommands();
PropertyPanelProvider.updateCommands();
Expand All @@ -46,12 +55,6 @@ export const loadCommandFiles = async (
}
}
}
if (files.length > 0) {
commands.executeCommand("setContext", "vision:hasCommand", true);
window.showInformationMessage("Command files loaded successfully.");
} else {
window.showWarningMessage("Command files not found.");
}
} catch (err) {
if (
err instanceof Error &&
Expand Down
10 changes: 9 additions & 1 deletion controller/src/utilities/loaders/loadSparqlConfigFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export let globalPingEndpoint: string = "";
export let globalUpdateAssertionEndpoint: string = "";
export let globalUpdateInferenceEndpoint: string = "";

/**
* Loads JSON files that are stored in the config folder of the model.
*
* @remarks
* This method uses the workspace class from the {@link https://code.visualstudio.com/api/references/vscode-api | VSCode API}.
*
*
*/
export async function loadSparqlConfigFiles() {
commands.executeCommand("setContext", "vision:hasSparqlConfig", false);
TreeDataProvider.getInstance().updateHasSparqlConfig(false);
Expand Down Expand Up @@ -45,7 +53,7 @@ export async function loadSparqlConfigFiles() {
);
TreeDataProvider.getInstance().updateHasSparqlConfig(true);
} else {
window.showErrorMessage("Invalid or missing sparqlConfig.json.");
window.showErrorMessage(`Invalid or missing ${file}.`);
}
} catch (parseErr) {
throw new Error(`Error parsing ${file}: ${parseErr}`);
Expand Down
11 changes: 11 additions & 0 deletions controller/src/utilities/loaders/loadSparqlFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { workspace, Uri, window, FileType } from "vscode";
// TODO: handle multiple workspaces (currently assumes model is in the 1st)

/**
* Loads SPARQL files that are stored in the sparql folder of the model.
*
* @remarks
* This method uses the workspace class from the {@link https://code.visualstudio.com/api/references/vscode-api | VSCode API}.
*
* @param uri - A universal resource identifier representing either a file on disk or another resource, like untitled resources.
* @param globalSparqlContents - content of the sparql contents object
*
*/
export async function loadSparqlFiles(globalSparqlContents: { [file: string]: string; }) {

const workspaceFolders = workspace.workspaceFolders;
Expand Down

0 comments on commit b52bcaf

Please sign in to comment.