-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client [NET-992]: pre-compiled JSON schema validator (#1642)
## Summary Compile JSON schema validator functions at build time instead of run time as described here https://ajv.js.org/standalone.html to avoid having to rely on `eval` during run time. Another added benefit is that we don't need to include `ajv` in our webpack build, reducing the browser bundle size slightly. ## Changes - Mark packages `ajv` and `ajv-format` as dev dependencies. - Add script `bin/generate-config-validator.js` that generates the pre-built JSON schema validator function. - Call above script in `prebuild` phase, ensuring it gets called every time the client package is built. - Add folder `src/generated` for generated code that gets ignored by eslint.
- Loading branch information
Showing
6 changed files
with
44 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ test/benchmarks/** | |
test/memory/* | ||
docs/** | ||
.idea/** | ||
src/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* This script generates a client config AJV validation function to avoid | ||
* having to compile it during run time which requires the use of `eval`. | ||
* Use of `eval` is not allowed e.g. in Chrome plugins. | ||
*/ | ||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const Ajv = require('ajv') | ||
const standaloneCode = require('ajv/dist/standalone').default | ||
const { fastFormats, fullFormats } = require('ajv-formats/dist/formats') | ||
const CONFIG_SCHEMA = require('../src/config.schema.json') | ||
|
||
const ajv = new Ajv({ | ||
useDefaults: true, | ||
code: { | ||
source: true | ||
} | ||
}) | ||
// addFormats(ajv) does not work properly when generating stand-alone code | ||
// (https://github.com/ajv-validator/ajv-formats/issues/68) so adding formats one-by-one | ||
ajv.addFormat('uri', fastFormats['uri']) | ||
ajv.addFormat('ipv4', fullFormats['ipv4']) | ||
ajv.addFormat('ethereum-address', /^0x[a-zA-Z0-9]{40}$/) | ||
ajv.addFormat('ethereum-private-key', /^(0x)?[a-zA-Z0-9]{64}$/) | ||
|
||
const validate = ajv.compile(CONFIG_SCHEMA) | ||
const moduleCode = standaloneCode(ajv, validate) | ||
fs.writeFileSync(path.join(__dirname, "../src/generated/validateConfig.js"), moduleCode) |
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
Large diffs are not rendered by default.
Oops, something went wrong.