forked from decentralized-identity/dwn-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-compile json schemas into AJV validators (decentralized-identity#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
- Loading branch information
Showing
36 changed files
with
135 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
generated | ||
node_modules | ||
try.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,6 @@ INDEX | |
TEST-INDEX | ||
# folders used by code coverage | ||
.nyc_output/ | ||
coverage | ||
coverage | ||
|
||
generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.