Skip to content

Commit

Permalink
validate 'pages.json' file
Browse files Browse the repository at this point in the history
Signed-off-by: pogi7 <[email protected]>
  • Loading branch information
pogi7 committed Jun 5, 2024
1 parent b52bcaf commit ffb9757
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 57 deletions.
2 changes: 1 addition & 1 deletion controller/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export function activate(context: vscode.ExtensionContext) {
"**/src/vision/sparql/*.sparql"
);
let viewpointsFolderWatcher = vscode.workspace.createFileSystemWatcher(
"**/src/vision/viewpoints/*.json"
"**/src/vision/viewpoints/**/*.json"
);
let commandsFolderWatcher = vscode.workspace.createFileSystemWatcher(
"**/src/vision/commands/*.json"
Expand Down
31 changes: 31 additions & 0 deletions controller/src/interfaces/IPagesSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Defines the structure of the JSON object that is received from the pages.json file.
*
* @field title - The title of the page item
* @field type - The type of the page item. Home = Home Page (there can only be one home page), Group = collection of pages, Diagram = Diagram Page, Tree = Tree Page, and Table = Table Page
* @field path - The path to the configuration of the page item
* @field iconUrl - The url to the icon that gets displayed in the home page. This is typically the url to a SVG published to the web
* @field children - The children of the group items
*
*/
export default interface IPagesSchema {
title: string;
type: string;
path?: string;
iconUrl?: string;
children?: Children[];
}

/**
* Defines the structure of the child pages
*
* @field title - The title of the child page item
* @field type - The type of the child page item. Home = Home Page (there can only be one home page), Group = collection of pages, Diagram = Diagram Page, Tree = Tree Page, and Table = Table Page
* @field path - The path to the configuration of the child page item
*
*/
interface Children {
title: string;
type: string;
path: string;
}
38 changes: 0 additions & 38 deletions controller/src/schemas/commandSchema.ts

This file was deleted.

38 changes: 38 additions & 0 deletions controller/src/schemas/commands/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"],
},
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ISparqlConfigSchema from '../interfaces/ISparqlConfigSchema';
import ISparqlConfigSchema from '../../interfaces/ISparqlConfigSchema';
import { JSONSchemaType } from "ajv";

/**
Expand Down
3 changes: 2 additions & 1 deletion controller/src/schemas/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ajv, { JSONSchemaType } from "ajv";
import ISparqlConfigSchema from "../interfaces/ISparqlConfigSchema";
import ICommandSchema from "../interfaces/ICommandSchema";
import IPagesSchema from "../interfaces/IPagesSchema";

/**
* This schema validator uses AJV to validate JSON data based on a schema interface.
Expand All @@ -15,7 +16,7 @@ import ICommandSchema from "../interfaces/ICommandSchema";
*
*/
export const validateSchema = (
schema: JSONSchemaType<ISparqlConfigSchema | ICommandSchema[]>,
schema: JSONSchemaType<ISparqlConfigSchema | ICommandSchema[] | IPagesSchema[]>,
data: Object
) => {
const ajv = new Ajv();
Expand Down
60 changes: 60 additions & 0 deletions controller/src/schemas/viewpoints/pagesSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import IPagesSchema from "../../interfaces/IPagesSchema";
import { JSONSchemaType } from "ajv";

/**
* This JSON schema is used with AJV to validate pages.json data that is stored within OML models.
*
* @remarks
* This constant will help validate JSON data using {@link https://ajv.js.org | AJV}.
*
* This schema has a recursive schema which you can read more {@link https://ajv.js.org/guide/combining-schemas.html | here}.
*
* This schema has nullable properties which you can read more {@link https://ajv.js.org/guide/typescript.html#utility-types-for-schemas | here}.
*
* The data within the constant was generated with {@link https://www.jsonschema.net | JSON Schema}.
*
*/

export const pagesSchema: JSONSchemaType<IPagesSchema[]> = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "array",
items: {
type: "object",
properties: {
title: {
type: "string",
},
type: {
type: "string",
},
path: {
type: "string",
nullable: true,
},
iconUrl: {
type: "string",
nullable: true,
},
children: {
type: "array",
nullable: true,
items: {
type: "object",
properties: {
title: {
type: "string",
},
type: {
type: "string",
},
path: {
type: "string",
},
},
required: ["title", "type", "path"],
},
},
},
required: ["title", "type"],
},
};
4 changes: 2 additions & 2 deletions controller/src/utilities/loaders/loadCommandFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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";
import { commandSchema } from "../../schemas/commands/commandSchema";
// TODO: handle multiple workspaces (currently assumes model is in the 1st)

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ export const loadCommandFiles = async (
const validate = validateSchema(commandSchema, content);
if (files.length > 0 && validate) {
commands.executeCommand("setContext", "vision:hasCommand", true);
window.showInformationMessage("Command files loaded successfully.");
window.showInformationMessage(`${file} loaded successfully.`);
} else {
window.showErrorMessage(`Invalid or missing ${file}.`);
}
Expand Down
4 changes: 2 additions & 2 deletions controller/src/utilities/loaders/loadSparqlConfigFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { workspace, Uri, commands, window, FileType } from "vscode";
import { TreeDataProvider } from "../../sidebar/TreeDataProvider";
import { validateSchema } from "../../schemas/validator";
import { sparqlConfigSchema } from "../../schemas/sparqlConfigSchema";
import { sparqlConfigSchema } from "../../schemas/config/sparqlConfigSchema";

export let globalQueryEndpoint: string = "";
export let globalPingEndpoint: string = "";
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function loadSparqlConfigFiles() {
true
);
window.showInformationMessage(
"Sparql Config files loaded successfully."
`${file} loaded successfully.`
);
TreeDataProvider.getInstance().updateHasSparqlConfig(true);
} else {
Expand Down
Loading

0 comments on commit ffb9757

Please sign in to comment.