Skip to content

Commit

Permalink
update: full ts + run tests on lib/js folder
Browse files Browse the repository at this point in the history
  • Loading branch information
k0stik committed Feb 7, 2024
1 parent e0acf29 commit df9db6b
Show file tree
Hide file tree
Showing 22 changed files with 382 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extension": ["ts", "js"],
"spec": "tests/js/**/*.js",
"spec": "tests/js/**/*",
"require": "ts-node/register"
}
3 changes: 0 additions & 3 deletions compile_ts.mjs

This file was deleted.

3 changes: 3 additions & 0 deletions compile_ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import compileTS from "./src/js/scripts/compileTs";

compileTS("./lib/js/schema", `./lib/js/types.ts`);
5 changes: 5 additions & 0 deletions modules.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "json-schema-deref-sync" {
import { JSONSchema7 } from "json-schema";

export default function deref(schema: JSONSchema, options: object): JSONSchema7;
}
28 changes: 23 additions & 5 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"scripts": {
"prepublishOnly": "rm -rf lib; npm run transpile",
"transpile": "tsc; BUILD_ASSETS=true BUILD_PATH='./lib/js' node build_schemas.js",
"transpile-and-build-assets": "npm run transpile && node compile_ts.mjs",
"transpile-and-build-assets": "npm run transpile && ts-node compile_ts.ts",
"build:assets": "node build_schemas.js",
"build:js-and-python-modules": "BUILD_PYTHON_MODULES=true node build_schemas.js",
"build:assets-with-docs": "BUILD_ASSETS=true node build_schemas.js",
"test": "nyc --reporter=text mocha --bail",
"test": "npm run postinstall && nyc --reporter=text mocha --bail",
"lint": "eslint src/js && prettier --write src/js",
"lint:fix": "eslint --fix --cache src/js && prettier --write src/js",
"prettier": "prettier --check src/js",
Expand Down Expand Up @@ -70,9 +70,12 @@
"@babel/runtime-corejs3": "7.16.8",
"@tsconfig/node14": "^14.1.0",
"@tsconfig/node20": "^20.1.2",
"@types/chai": "^4.3.11",
"@types/json-schema-merge-allof": "^0.6.5",
"@types/mocha": "^10.0.6",
"ajv": "8.12.0",
"file": "^0.2.2",
"js-yaml": "^4.1.0",
"json-schema": "^0.4.0",
"json-schema-deref-sync": "0.14.0",
"json-schema-merge-allof": "^0.8.1",
"json-schema-to-typescript": "^13.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
"vdw_corr": {
"type": "string",
"enum": [
"none",
"grimme-d2",
"Grimme-D2",
"DFT-D",
Expand Down
2 changes: 1 addition & 1 deletion schema/workflow/subworkflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"description": "Contains the Units of the subworkflow",
"type": "array",
"items": {
"$ref": "unit/base.json"
"$ref": "./subworkflow/unit.json"
}
},
"model": {
Expand Down
18 changes: 9 additions & 9 deletions schema/workflow/subworkflow/unit.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"$id": "workflow/unit",
"$id": "workflow/subworkflow/unit",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "workflow unit schema",
"title": "workflow subworkflow unit schema",
"type": "object",
"oneOf": [
{
"$ref": "./unit/io.json"
"$ref": "./../unit/io.json"
},
{
"$ref": "./unit/reduce.json"
"$ref": "./../unit/reduce.json"
},
{
"$ref": "./unit/condition.json"
"$ref": "./../unit/condition.json"
},
{
"$ref": "./unit/assertion.json"
"$ref": "./../unit/assertion.json"
},
{
"$ref": "./unit/execution.json"
"$ref": "./../unit/execution.json"
},
{
"$ref": "./unit/assignment.json"
"$ref": "./../unit/assignment.json"
},
{
"$ref": "./unit/processing.json"
"$ref": "./../unit/processing.json"
}
],
"discriminator": {
Expand Down
68 changes: 62 additions & 6 deletions src/js/esse/JSONSchemasInterface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { SchemaObject } from "ajv";
import Ajv, { SchemaObject } from "ajv";
import fs from "fs";
import path from "path";

import { walkDirSync } from "../scripts/utils";
import { addAdditionalPropertiesToSchema } from "./schemaUtils";

type Query = { [key in keyof SchemaObject]: { $regex: string } };

const schemasCache = new Map<string, SchemaObject>();
export interface AnyObject {
[key: string]: unknown;
}

const ajv = new Ajv({
removeAdditional: true,
strict: false,
useDefaults: true,
/**
* discriminator fixes default values in oneOf
* @see https://ajv.js.org/guide/modifying-data.html#assigning-defaults
*/
discriminator: true,
coerceTypes: true, // convert "true" => true for boolean or "4" => 4 for integer
});

export function readSchemaFolderSync(folderPath: string) {
const schemas: SchemaObject[] = [];
Expand All @@ -25,6 +40,8 @@ export function readSchemaFolderSync(folderPath: string) {
export class JSONSchemasInterface {
static schemaFolder = "./lib/js/schema";

static schemasCache = new Map<string, SchemaObject>();

static setSchemaFolder(schemaFolder: string) {
if (this.schemaFolder !== schemaFolder) {
this.schemaFolder = schemaFolder;
Expand All @@ -37,17 +54,17 @@ export class JSONSchemasInterface {

schemas.forEach((schema) => {
if (schema.$id) {
schemasCache.set(schema.$id, schema);
this.schemasCache.set(schema.$id, schema);
}
});
}

static schemaById(schemaId: string) {
if (schemasCache.size === 0) {
if (this.schemasCache.size === 0) {
this.readSchemaFolder();
}

return schemasCache.get(schemaId);
return this.schemasCache.get(schemaId);
}

/**
Expand All @@ -71,7 +88,7 @@ export class JSONSchemasInterface {
static matchSchema(query: Query) {
const searchFields = Object.keys(query) as Array<keyof typeof query>;

return Array.from(schemasCache.values()).find((schema) => {
return Array.from(this.schemasCache.values()).find((schema) => {
return searchFields.every((field) => {
const queryField = query[field];
const schemaField = schema[field];
Expand All @@ -84,4 +101,43 @@ export class JSONSchemasInterface {
});
});
}

private static getAjvValidator(jsonSchema: SchemaObject) {
const schemaKey = jsonSchema.$id as string;

if (!schemaKey) {
console.log({
jsonSchema,
});
}

let validate = ajv.getSchema(schemaKey);

if (!validate) {
ajv.addSchema(addAdditionalPropertiesToSchema(jsonSchema), schemaKey);
validate = ajv.getSchema(schemaKey);
}

if (!validate) {
throw new Error("JSONSchemasInterface AJV validator error");
}

return validate;
}

/**
* Validates a given example against the schema.
* @param example example to validate.
* @param schema schema to validate the example with.
* @returns whether example is valid.
*/
static validate(data: AnyObject, jsonSchema: SchemaObject) {
const validator = this.getAjvValidator(jsonSchema);
const isValid = validator(data);

return {
isValid,
errors: validator.errors,
};
}
}
69 changes: 0 additions & 69 deletions src/js/esse/index.js

This file was deleted.

Loading

0 comments on commit df9db6b

Please sign in to comment.