From ecd72c95e4ab31f68dbd74d36bc3502967d163da Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Thu, 10 Nov 2022 17:32:54 -0800 Subject: [PATCH] Pre-compile json schemas into AJV validators (#148) * address warnings generated when validating `public-jwk` json schema * janky attempt to use precompiled json schema validators * move bundling scripts & config to `build` dir * pre-compile json schemas into ajv validators * add comment describing `compile-validators` build script * gitignore generated dir --- .eslintignore | 3 +- .gitignore | 4 +- README.md | 2 +- build/compile-validators.js | 67 +++++++++++++++++++ .../create-browser-bundle.cjs | 0 .../esbuild-browser-config.cjs | 0 .../collections/collections-query.json | 0 .../collections/collections-write.json | 0 .../definitions.json | 0 .../general-jws.json | 0 .../hooks/hooks-write.json | 0 .../jwk-verification-method.json | 0 .../jwk/general-jwk.json | 0 .../jwk/public-jwk.json | 3 + .../permissions/definitions.json | 0 .../permissions/permissions-grant.json | 0 .../permissions/permissions-request.json | 0 .../protocol-definition.json | 0 .../protocol-rule-set.json | 0 .../protocols/protocols-configure.json | 0 .../protocols/protocols-query.json | 0 .../request.json | 0 karma.conf.cjs | 2 +- karma.conf.debug.cjs | 15 +---- package-lock.json | 47 +++++++++---- package.json | 18 ++--- src/core/message.ts | 2 +- src/core/request.ts | 2 +- src/dwn.ts | 7 +- src/jose/jws/general/verifier.ts | 2 +- src/validation/json-schemas/index.ts | 35 ---------- src/{validation => }/validator.ts | 16 +---- .../jwk-verification-method.spec.ts | 2 +- .../json-schemas/jwk/general-jwk.spec.ts | 2 +- .../json-schemas/jwk/public-jwk.spec.ts | 2 +- tsconfig.json | 1 + 36 files changed, 135 insertions(+), 97 deletions(-) create mode 100644 build/compile-validators.js rename create-browser-bundle.cjs => build/create-browser-bundle.cjs (100%) rename esbuild-browser-config.cjs => build/esbuild-browser-config.cjs (100%) rename {src/validation/json-schemas => json-schemas}/collections/collections-query.json (100%) rename {src/validation/json-schemas => json-schemas}/collections/collections-write.json (100%) rename {src/validation/json-schemas => json-schemas}/definitions.json (100%) rename {src/validation/json-schemas => json-schemas}/general-jws.json (100%) rename {src/validation/json-schemas => json-schemas}/hooks/hooks-write.json (100%) rename {src/validation/json-schemas => json-schemas}/jwk-verification-method.json (100%) rename {src/validation/json-schemas => json-schemas}/jwk/general-jwk.json (100%) rename {src/validation/json-schemas => json-schemas}/jwk/public-jwk.json (93%) rename {src/validation/json-schemas => json-schemas}/permissions/definitions.json (100%) rename {src/validation/json-schemas => json-schemas}/permissions/permissions-grant.json (100%) rename {src/validation/json-schemas => json-schemas}/permissions/permissions-request.json (100%) rename {src/validation/json-schemas => json-schemas}/protocol-definition.json (100%) rename {src/validation/json-schemas => json-schemas}/protocol-rule-set.json (100%) rename {src/validation/json-schemas => json-schemas}/protocols/protocols-configure.json (100%) rename {src/validation/json-schemas => json-schemas}/protocols/protocols-query.json (100%) rename {src/validation/json-schemas => json-schemas}/request.json (100%) delete mode 100644 src/validation/json-schemas/index.ts rename src/{validation => }/validator.ts (71%) diff --git a/.eslintignore b/.eslintignore index 0ccdf0cf5..4b97d67a7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ -node_modules dist +generated +node_modules try.ts \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0e259089c..ccf09e920 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ INDEX TEST-INDEX # folders used by code coverage .nyc_output/ -coverage \ No newline at end of file +coverage + +generated \ No newline at end of file diff --git a/README.md b/README.md index 464e1ee6c..dbfa62fe0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Code Coverage -![Statements](https://img.shields.io/badge/statements-50.97%25-red.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-71.42%25-red.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-31.46%25-red.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-50.97%25-red.svg?style=flat) +![Statements](https://img.shields.io/badge/statements-91.1%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-88.73%25-yellow.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-87.41%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-91.1%25-brightgreen.svg?style=flat) ## Introduction diff --git a/build/compile-validators.js b/build/compile-validators.js new file mode 100644 index 000000000..a0e5b7df6 --- /dev/null +++ b/build/compile-validators.js @@ -0,0 +1,67 @@ +/** + * Pre-compiles Ajv validators from json schemas + * Ajv supports generating standalone validation functions from JSON Schemas at compile/build time. + * These functions can then be used during runtime to do validation without initializing Ajv. + * It is useful for several reasons: + * - to avoid dynamic code evaluation with Function constructor (used for schema compilation) - + * when it is prohibited by the browser page [Content Security Policy](https://ajv.js.org/security.html#content-security-policy). + * - to reduce the browser bundle size - Ajv is not included in the bundle + * - to reduce the start-up time - the validation and compilation of schemas will happen during build time. + */ + +import fs from 'node:fs'; +import path from 'node:path'; +import url from 'node:url'; + +import Ajv from 'ajv'; +import standaloneCode from 'ajv/dist/standalone'; +import mkdirp from 'mkdirp'; + +import CollectionsQuery from '../json-schemas/collections/collections-query.json' assert { type: 'json' }; +import CollectionsWrite from '../json-schemas/collections/collections-write.json' assert { type: 'json' }; +import Definitions from '../json-schemas/definitions.json' assert { type: 'json' }; +import GeneralJwk from '../json-schemas/jwk/general-jwk.json' assert { type: 'json' }; +import GeneralJws from '../json-schemas/general-jws.json' assert { type: 'json' }; +import HooksWrite from '../json-schemas/hooks/hooks-write.json' assert { type: 'json' }; +import JwkVerificationMethod from '../json-schemas/jwk-verification-method.json' assert {type: 'json'}; +import PermissionsDefinitions from '../json-schemas/permissions/definitions.json' assert { type: 'json' }; +import PermissionsRequest from '../json-schemas/permissions/permissions-request.json' assert { type: 'json' }; +import PermissionsGrant from '../json-schemas/permissions/permissions-grant.json' assert { type: 'json' }; +import ProtocolDefinition from '../json-schemas/protocol-definition.json' assert { type: 'json' }; +import ProtocolRuleSet from '../json-schemas/protocol-rule-set.json' assert { type: 'json' }; +import ProtocolsConfigure from '../json-schemas/protocols/protocols-configure.json' assert { type: 'json' }; +import ProtocolsQuery from '../json-schemas/protocols/protocols-query.json' assert { type: 'json' }; +import PublicJwk from '../json-schemas/jwk/public-jwk.json' assert { type: 'json' }; +import Request from '../json-schemas/request.json' assert { type: 'json' }; + +const schemas = { + CollectionsQuery, + CollectionsWrite, + Definitions, + GeneralJwk, + GeneralJws, + HooksWrite, + JwkVerificationMethod, + PermissionsDefinitions, + PermissionsGrant, + PermissionsRequest, + ProtocolDefinition, + ProtocolRuleSet, + ProtocolsConfigure, + ProtocolsQuery, + PublicJwk, + Request +}; + +const ajv = new Ajv({ code: { source: true, esm: true } }); + +for (const schemaName in schemas) { + ajv.addSchema(schemas[schemaName], schemaName); +} + +const moduleCode = standaloneCode(ajv); + +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + +await mkdirp(path.join(__dirname, '../generated')); +fs.writeFileSync(path.join(__dirname, '../generated/precompiled-validators.js'), moduleCode); \ No newline at end of file diff --git a/create-browser-bundle.cjs b/build/create-browser-bundle.cjs similarity index 100% rename from create-browser-bundle.cjs rename to build/create-browser-bundle.cjs diff --git a/esbuild-browser-config.cjs b/build/esbuild-browser-config.cjs similarity index 100% rename from esbuild-browser-config.cjs rename to build/esbuild-browser-config.cjs diff --git a/src/validation/json-schemas/collections/collections-query.json b/json-schemas/collections/collections-query.json similarity index 100% rename from src/validation/json-schemas/collections/collections-query.json rename to json-schemas/collections/collections-query.json diff --git a/src/validation/json-schemas/collections/collections-write.json b/json-schemas/collections/collections-write.json similarity index 100% rename from src/validation/json-schemas/collections/collections-write.json rename to json-schemas/collections/collections-write.json diff --git a/src/validation/json-schemas/definitions.json b/json-schemas/definitions.json similarity index 100% rename from src/validation/json-schemas/definitions.json rename to json-schemas/definitions.json diff --git a/src/validation/json-schemas/general-jws.json b/json-schemas/general-jws.json similarity index 100% rename from src/validation/json-schemas/general-jws.json rename to json-schemas/general-jws.json diff --git a/src/validation/json-schemas/hooks/hooks-write.json b/json-schemas/hooks/hooks-write.json similarity index 100% rename from src/validation/json-schemas/hooks/hooks-write.json rename to json-schemas/hooks/hooks-write.json diff --git a/src/validation/json-schemas/jwk-verification-method.json b/json-schemas/jwk-verification-method.json similarity index 100% rename from src/validation/json-schemas/jwk-verification-method.json rename to json-schemas/jwk-verification-method.json diff --git a/src/validation/json-schemas/jwk/general-jwk.json b/json-schemas/jwk/general-jwk.json similarity index 100% rename from src/validation/json-schemas/jwk/general-jwk.json rename to json-schemas/jwk/general-jwk.json diff --git a/src/validation/json-schemas/jwk/public-jwk.json b/json-schemas/jwk/public-jwk.json similarity index 93% rename from src/validation/json-schemas/jwk/public-jwk.json rename to json-schemas/jwk/public-jwk.json index b32d9497c..6cb4860ca 100644 --- a/src/validation/json-schemas/jwk/public-jwk.json +++ b/json-schemas/jwk/public-jwk.json @@ -5,6 +5,7 @@ "not": { "anyOf": [ { + "type": "object", "properties": { "kty": { "const": "EC" @@ -13,6 +14,7 @@ "anyOf": [{ "required": ["d"] }] }, { + "type": "object", "properties": { "kty": { "const": "OKP" @@ -21,6 +23,7 @@ "anyOf": [{ "required": ["d"] }] }, { + "type": "object", "properties": { "kty": { "const": "RSA" diff --git a/src/validation/json-schemas/permissions/definitions.json b/json-schemas/permissions/definitions.json similarity index 100% rename from src/validation/json-schemas/permissions/definitions.json rename to json-schemas/permissions/definitions.json diff --git a/src/validation/json-schemas/permissions/permissions-grant.json b/json-schemas/permissions/permissions-grant.json similarity index 100% rename from src/validation/json-schemas/permissions/permissions-grant.json rename to json-schemas/permissions/permissions-grant.json diff --git a/src/validation/json-schemas/permissions/permissions-request.json b/json-schemas/permissions/permissions-request.json similarity index 100% rename from src/validation/json-schemas/permissions/permissions-request.json rename to json-schemas/permissions/permissions-request.json diff --git a/src/validation/json-schemas/protocol-definition.json b/json-schemas/protocol-definition.json similarity index 100% rename from src/validation/json-schemas/protocol-definition.json rename to json-schemas/protocol-definition.json diff --git a/src/validation/json-schemas/protocol-rule-set.json b/json-schemas/protocol-rule-set.json similarity index 100% rename from src/validation/json-schemas/protocol-rule-set.json rename to json-schemas/protocol-rule-set.json diff --git a/src/validation/json-schemas/protocols/protocols-configure.json b/json-schemas/protocols/protocols-configure.json similarity index 100% rename from src/validation/json-schemas/protocols/protocols-configure.json rename to json-schemas/protocols/protocols-configure.json diff --git a/src/validation/json-schemas/protocols/protocols-query.json b/json-schemas/protocols/protocols-query.json similarity index 100% rename from src/validation/json-schemas/protocols/protocols-query.json rename to json-schemas/protocols/protocols-query.json diff --git a/src/validation/json-schemas/request.json b/json-schemas/request.json similarity index 100% rename from src/validation/json-schemas/request.json rename to json-schemas/request.json diff --git a/karma.conf.cjs b/karma.conf.cjs index 1c29a42e0..09efff5a2 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -1,7 +1,7 @@ // Karma is what we're using to run our tests in browser environments // Karma does not support .mjs -const esbuildBrowserConfig = require('./esbuild-browser-config.cjs'); +const esbuildBrowserConfig = require('./build/esbuild-browser-config.cjs'); module.exports = function(config) { config.set({ diff --git a/karma.conf.debug.cjs b/karma.conf.debug.cjs index 76a3070b9..563ce281c 100644 --- a/karma.conf.debug.cjs +++ b/karma.conf.debug.cjs @@ -1,7 +1,8 @@ // Karma is what we're using to run our tests in browser environments // Karma does not support .mjs -const { NodeGlobalsPolyfillPlugin } = require('@esbuild-plugins/node-globals-polyfill'); +const esbuildBrowserConfig = require('./build/esbuild-browser-config.cjs'); + module.exports = function(config) { config.set({ plugins: [ @@ -26,17 +27,7 @@ module.exports = function(config) { 'tests/**/*.ts': ['esbuild'] }, - esbuild: { - mainFields : ['browser', 'module', 'main'], - target : ['chrome101'], - plugins : [NodeGlobalsPolyfillPlugin({ - process: true - })], - sourcemap : true, - define : { - 'global': 'window' - }, - }, + esbuild: esbuildBrowserConfig, // list of files / patterns to exclude exclude: [], diff --git a/package-lock.json b/package-lock.json index 977b356a9..86e664858 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,6 +57,7 @@ "karma-esbuild": "2.2.4", "karma-mocha": "2.0.1", "karma-mocha-reporter": "2.2.5", + "mkdirp": "1.0.4", "mocha": "10.1.0", "mockdate": "3.0.5", "ms": "2.1.3", @@ -3721,6 +3722,18 @@ "node": ">=4" } }, + "node_modules/karma/node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "dev": true, + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, "node_modules/level": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/level/-/level-8.0.0.tgz", @@ -4161,15 +4174,15 @@ "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==" }, "node_modules/mkdirp": { - "version": "0.5.6", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", - "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", "dev": true, - "dependencies": { - "minimist": "^1.2.6" - }, "bin": { "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" } }, "node_modules/mocha": { @@ -8632,6 +8645,17 @@ "tmp": "^0.2.1", "ua-parser-js": "^0.7.30", "yargs": "^16.1.1" + }, + "dependencies": { + "mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "dev": true, + "requires": { + "minimist": "^1.2.6" + } + } } }, "karma-chai": { @@ -9100,13 +9124,10 @@ "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==" }, "mkdirp": { - "version": "0.5.6", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", - "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", - "dev": true, - "requires": { - "minimist": "^1.2.6" - } + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true }, "mocha": { "version": "10.1.0", diff --git a/package.json b/package.json index 7ef9efe47..28d62c065 100644 --- a/package.json +++ b/package.json @@ -21,17 +21,18 @@ ], "scripts": { "build:esm": "tsc", - "build": "npm-run-all -l clean -p build:esm bundle", - "clean": "rimraf dist", - "bundle": "node create-browser-bundle.cjs", + "build": "npm-run-all -l clean compile-validators -p build:esm bundle", + "bundle": "node ./build/create-browser-bundle.cjs", + "clean": "rimraf dist && rimraf generated/*", + "compile-validators": "node --es-module-specifier-resolution=node ./build/compile-validators.js", "lint": "eslint . --ext .ts --max-warnings 0", "lint:fix": "eslint . --ext .ts --fix", - "test:node": "tsc && c8 mocha --es-module-specifier-resolution=node \"dist/esm/tests/**/*.spec.js\" && npm run make-coverage-badges", - "test:node:ci": "tsc && c8 mocha --es-module-specifier-resolution=node \"dist/esm/tests/**/*.spec.js\"", - "test:browser": "cross-env karma start karma.conf.cjs", - "test:browser-debug": "cross-env karma start karma.conf.debug.cjs", "make-coverage-badges": "istanbul-badges-readme", - "make-coverage-badges:ci": "npm run make-coverage-badges -- --ci" + "make-coverage-badges:ci": "npm run make-coverage-badges -- --ci", + "test:node": "npm run compile-validators && tsc && c8 mocha --es-module-specifier-resolution=node \"dist/esm/tests/**/*.spec.js\" && npm run make-coverage-badges", + "test:node:ci": "npm run compile-validators && tsc && c8 mocha --es-module-specifier-resolution=node \"dist/esm/tests/**/*.spec.js\"", + "test:browser": "npm run compile-validators && cross-env karma start karma.conf.cjs", + "test:browser-debug": "npm run compile-validators && cross-env karma start karma.conf.debug.cjs" }, "dependencies": { "@ipld/dag-cbor": "7.0.1", @@ -83,6 +84,7 @@ "karma-mocha": "2.0.1", "karma-mocha-reporter": "2.2.5", "mocha": "10.1.0", + "mkdirp": "1.0.4", "mockdate": "3.0.5", "ms": "2.1.3", "npm-run-all": "4.1.5", diff --git a/src/core/message.ts b/src/core/message.ts index 307e48da6..aa9b7b0c2 100644 --- a/src/core/message.ts +++ b/src/core/message.ts @@ -6,7 +6,7 @@ import { CollectionsWriteMessage } from '../interfaces/collections/types'; import { compareCids, generateCid } from '../utils/cid'; import { GeneralJws } from '../jose/jws/general/types'; import { GeneralJwsSigner, GeneralJwsVerifier } from '../jose/jws/general'; -import { validate } from '../validation/validator'; +import { validate } from '../validator'; export enum DwnMethodName { CollectionsWrite = 'CollectionsWrite', diff --git a/src/core/request.ts b/src/core/request.ts index 95a9ebec2..92546b4b6 100644 --- a/src/core/request.ts +++ b/src/core/request.ts @@ -1,5 +1,5 @@ import { RequestSchema } from './types'; -import { validate } from '../validation/validator'; +import { validate } from '../validator'; export class Request { /** diff --git a/src/dwn.ts b/src/dwn.ts index 0d0538079..c7ee73c76 100644 --- a/src/dwn.ts +++ b/src/dwn.ts @@ -3,7 +3,6 @@ import type { DidMethodResolver } from './did/did-resolver'; import type { Interface, MethodHandler } from './interfaces/types'; import type { MessageStore } from './store/message-store'; import * as encoder from '../src/utils/encoder'; -import { addSchema } from './validation/validator'; import { CollectionsInterface, PermissionsInterface, ProtocolsInterface } from './interfaces'; import { DidResolver } from './did/did-resolver'; import { MessageReply, Request, Response } from './core'; @@ -29,7 +28,7 @@ export class Dwn { config.messageStore ??= new MessageStoreLevel(); config.interfaces ??= []; - for (const { methodHandlers, schemas } of config.interfaces) { + for (const { methodHandlers } of config.interfaces) { for (const messageType in methodHandlers) { if (Dwn.methodHandlers[messageType]) { @@ -38,10 +37,6 @@ export class Dwn { Dwn.methodHandlers[messageType] = methodHandlers[messageType]; } } - - for (const schemaName in schemas) { - addSchema(schemaName, schemas[schemaName]); - } } const dwn = new Dwn(config); diff --git a/src/jose/jws/general/verifier.ts b/src/jose/jws/general/verifier.ts index f58b26e3f..ce9de520a 100644 --- a/src/jose/jws/general/verifier.ts +++ b/src/jose/jws/general/verifier.ts @@ -6,7 +6,7 @@ import * as encoder from '../../../utils/encoder'; import { DidResolver } from '../../../did/did-resolver'; import { MemoryCache } from '../../../utils/memory-cache'; import { signers as verifiers } from '../../algorithms'; -import { validate } from '../../../validation/validator'; +import { validate } from '../../../validator'; type VerificationResult = { /** DIDs of all signers */ diff --git a/src/validation/json-schemas/index.ts b/src/validation/json-schemas/index.ts deleted file mode 100644 index 75748357a..000000000 --- a/src/validation/json-schemas/index.ts +++ /dev/null @@ -1,35 +0,0 @@ -import CollectionsQuery from './collections/collections-query.json' assert { type: 'json' }; -import CollectionsWrite from './collections/collections-write.json' assert { type: 'json' }; -import Definitions from './definitions.json' assert { type: 'json' }; -import GeneralJwk from './jwk/general-jwk.json' assert { type: 'json' }; -import GeneralJws from './general-jws.json' assert { type: 'json' }; -import HooksWrite from './hooks/hooks-write.json' assert { type: 'json' }; -import JwkVerificationMethod from './jwk-verification-method.json' assert {type: 'json'}; -import PermissionsDefinitions from './permissions/definitions.json' assert { type: 'json' }; -import PermissionsRequest from './permissions/permissions-request.json' assert { type: 'json' }; -import PermissionsGrant from './permissions/permissions-grant.json' assert { type: 'json' }; -import ProtocolDefinition from './protocol-definition.json' assert { type: 'json' }; -import ProtocolRuleSet from './protocol-rule-set.json' assert { type: 'json' }; -import ProtocolsConfigure from './protocols/protocols-configure.json' assert { type: 'json' }; -import ProtocolsQuery from './protocols/protocols-query.json' assert { type: 'json' }; -import PublicJwk from './jwk/public-jwk.json' assert { type: 'json' }; -import Request from './request.json' assert { type: 'json' }; - -export const schemas = { - CollectionsQuery, - CollectionsWrite, - Definitions, - GeneralJwk, - GeneralJws, - HooksWrite, - JwkVerificationMethod, - PermissionsDefinitions, - PermissionsGrant, - PermissionsRequest, - ProtocolDefinition, - ProtocolRuleSet, - ProtocolsConfigure, - ProtocolsQuery, - PublicJwk, - Request -}; \ No newline at end of file diff --git a/src/validation/validator.ts b/src/validator.ts similarity index 71% rename from src/validation/validator.ts rename to src/validator.ts index b9f9e28ba..5e52fbf3c 100644 --- a/src/validation/validator.ts +++ b/src/validator.ts @@ -1,15 +1,4 @@ -import Ajv from 'ajv'; -import { schemas } from './json-schemas'; - -const validator = new Ajv(); - -for (const schemaName in schemas) { - addSchema(schemaName, schemas[schemaName]); -} - -export function addSchema(schemaName: string, schema): void { - validator.addSchema(schema, schemaName); -} +import * as precompiledValidators from '../generated/precompiled-validators'; /** * Validates the given payload using JSON schema keyed by the given schema name. Throws if the given payload fails validation. @@ -17,7 +6,8 @@ export function addSchema(schemaName: string, schema): void { * @param payload javascript object to be validated */ export function validate(schemaName: string, payload: any): void { - const validateFn = validator.getSchema(schemaName); + // const validateFn = validator.getSchema(schemaName); + const validateFn = precompiledValidators[schemaName]; if (!validateFn) { throw new Error(`schema for ${schemaName} not found.`); diff --git a/tests/validation/json-schemas/jwk-verification-method.spec.ts b/tests/validation/json-schemas/jwk-verification-method.spec.ts index 152ca2f9f..4b4ef8c5f 100644 --- a/tests/validation/json-schemas/jwk-verification-method.spec.ts +++ b/tests/validation/json-schemas/jwk-verification-method.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { validate } from '../../../src/validation/validator'; +import { validate } from '../../../src/validator'; import { signers } from '../../../src/jose/algorithms'; const { secp256k1 } = signers; diff --git a/tests/validation/json-schemas/jwk/general-jwk.spec.ts b/tests/validation/json-schemas/jwk/general-jwk.spec.ts index 2cbc708eb..744d00c52 100644 --- a/tests/validation/json-schemas/jwk/general-jwk.spec.ts +++ b/tests/validation/json-schemas/jwk/general-jwk.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { validate } from '../../../../src/validation/validator'; +import { validate } from '../../../../src/validator'; import { signers } from '../../../../src/jose/algorithms'; const { Ed25519, secp256k1 } = signers; diff --git a/tests/validation/json-schemas/jwk/public-jwk.spec.ts b/tests/validation/json-schemas/jwk/public-jwk.spec.ts index bd2eb1e24..7931492c0 100644 --- a/tests/validation/json-schemas/jwk/public-jwk.spec.ts +++ b/tests/validation/json-schemas/jwk/public-jwk.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { validate } from '../../../../src/validation/validator'; +import { validate } from '../../../../src/validator'; import { signers } from '../../../../src/jose/algorithms'; const { Ed25519, secp256k1 } = signers; diff --git a/tsconfig.json b/tsconfig.json index 6bdb394d2..5c36738f0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "DOM", "ES6" ], + "allowJs": true, "target": "es6", "module": "ESNext", // Required for enabling JavaScript import assertion support "declaration": true,