Skip to content

Commit

Permalink
chore: ts-config & packages
Browse files Browse the repository at this point in the history
  • Loading branch information
k0stik committed Mar 25, 2024
1 parent 21118c5 commit b2ab8be
Show file tree
Hide file tree
Showing 7 changed files with 556 additions and 666 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ jobs:
shell: bash -l {0}
run: |
npm install
echo "Building Schemas and Examples"
npm run build:assets-with-docs
echo "List build directory ./docs/js"
ls -l docs/js
Expand Down
8 changes: 6 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ if git diff --cached --name-only | grep --quiet -E "$SRC_PATTERN"
then
echo "JSON assets changed. Running build scripts."
echo "Re-building JS and PY assets using JS script."
npm run build:js-and-python-modules
npm run transpile-and-build-assets
fi

npx lint-staged
npx lint-staged --allow-empty

npm run lint:fix
npm run transpile

5 changes: 3 additions & 2 deletions build_schemas.js → build_schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* at build time and writes them out to a single schemas.js file for
* downstream consumption to avoid FS calls in the browser.
*/
const fs = require("fs");
const JSONSchemasGenerator = require("./dist/js/esse/JSONSchemasGenerator").default;
import * as fs from "fs";

import JSONSchemasGenerator from "./src/js/esse/JSONSchemasGenerator";

// JS Modules

Expand Down
1,130 changes: 538 additions & 592 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"description": "Mat3ra's Excellent Source of Schemas and Examples (ESSE) for Digital Materials R&D",
"scripts": {
"prepublishOnly": "rm -rf dist; npm run transpile",
"transpile": "tsc -p tsconfig-transpile.json && BUILD_ASSETS=true BUILD_PATH='./dist/js' node build_schemas.js",
"transpile-and-build-assets": "npm run transpile && ts-node compile_ts.ts",
"build:assets": "node build_schemas.js",
"build:js-and-python-modules": "npm run transpile-and-build-assets && BUILD_PYTHON_MODULES=true node build_schemas.js",
"build:assets-with-docs": "BUILD_ASSETS=true node build_schemas.js",
"test": "nyc --reporter=text mocha --recursive --bail tests/js",
"test-only": "nyc --reporter=text mocha --bail",
"transpile": "tsc -p tsconfig-transpile.json",
"build-schemas": "BUILD_ASSETS=true BUILD_PYTHON_MODULES=true BUILD_PATH='./dist/js' ts-node build_schemas.ts",
"compile-types": "ts-node compile_ts.ts",
"transpile-and-build-assets": "npm run transpile && npm run build-schemas && npm run compile-types",
"test": "npm run build-schemas && nyc --reporter=text mocha --recursive --bail tests/js",
"test-only": "nyc --reporter=text mocha --recursive --bail tests/js",
"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
62 changes: 2 additions & 60 deletions src/js/utils/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FuncKeywordDefinition } from "ajv";
import Ajv, { SchemaObject } from "ajv";
import { AnyValidateFunction } from "ajv/dist/core";

Expand All @@ -25,59 +24,6 @@ function addAdditionalPropertiesToSchema(schema: JSONSchema, additionalPropertie
});
}

function addTransformDateKeywordToSchema(schema: JSONSchema) {
return mapObjectDeep(schema, (object) => {
const localSchema = object as JSONSchema;

if (
typeof object === "object" &&
localSchema?.type === "string" &&
localSchema?.format === "date-time"
) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { type, format, ...restSchema } = localSchema;
return {
...restSchema,
transformDate: true,
};
}
});
}

/**
* This function defines custom transformDate AJV keyword
*
* Problem:
* There's no way to define dates except as a string with "date-time" or "date" format in JsonSchema:
* {
* type: "string",
* format: "date-time"
* }
* But we need the Date object to be stored in databases or to perform correct date comparison
*
* Solution:
* The keyword will convert string with "date-time" format to Date object
*/
function transformDateKeyword(): FuncKeywordDefinition {
return {
keyword: "transformDate",
schemaType: "boolean",
modifying: true,
validate(transformDate, date, _metadata, dataCxt) {
if (transformDate && dataCxt && typeof date === "string") {
const dateObject = new Date(date);
if (dateObject.toString() === "Invalid Date") {
return false;
}

dataCxt.parentData[dataCxt.parentDataProperty] = dateObject;
}

return true;
},
};
}

const ajvConfig = {
strict: false, // TODO: adjust schemas and enable strict mode
useDefaults: true,
Expand All @@ -86,7 +32,6 @@ const ajvConfig = {
* @see https://ajv.js.org/guide/modifying-data.html#assigning-defaults
*/
discriminator: true,
keywords: [transformDateKeyword()],
};

const ajvValidator = new Ajv({ ...ajvConfig });
Expand Down Expand Up @@ -123,11 +68,8 @@ export function getValidator(
let validate = ajv.getSchema(schemaKey);

if (!validate) {
// replace "date-time" format with "transformDate" keyword
const patchedSchema = addTransformDateKeywordToSchema(
// properties that were not defined in schema will be ignored when clean = false
clean ? addAdditionalPropertiesToSchema(jsonSchema) : jsonSchema,
);
// properties that were not defined in schema will be ignored when clean = false
const patchedSchema = clean ? addAdditionalPropertiesToSchema(jsonSchema) : jsonSchema;
ajv.addSchema(patchedSchema, schemaKey);
validate = ajv.getSchema(schemaKey);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/js/JSONSchemasInterface.tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assert, expect } from "chai";
import * as path from "path";

import JSONSchemasInterface from "../../dist/js/esse/JSONSchemasInterfaceServer";
import allSchemas from "../../dist/js/schemas.json";
import JSONSchemasInterface from "../../src/js/esse/JSONSchemasInterfaceServer";
import { JSONSchema } from "../../src/js/esse/utils";

function assertSystemInSetSchema(schema?: JSONSchema) {
Expand Down

0 comments on commit b2ab8be

Please sign in to comment.