Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Upgrade
ajv
to v8
along with related packages
- Loading branch information
Showing
14 changed files
with
228 additions
and
152 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const Ajv = require('ajv').default; | ||
const objectHash = require('object-hash'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
const standaloneCode = require('ajv/dist/standalone').default; | ||
const fs = require('fs'); | ||
const requireFromString = require('require-from-string'); | ||
const deepSortObjectByKey = require('../../utils/deepSortObjectByKey'); | ||
const ensureExists = require('../../utils/ensureExists'); | ||
|
||
const getCacheDir = () => { | ||
return path.resolve( | ||
process.env.SLS_SCHEMA_CACHE_BASE_DIR || os.homedir(), | ||
`.serverless/artifacts/ajv-validate-${require('ajv/package').version}` | ||
); | ||
}; | ||
|
||
// Validators are cached by schema hash for the purpose | ||
// of speeding up tests and reducing their memory footprint. | ||
// If that solution proves to not be enough, we can improve it | ||
// with `uni-global` package. | ||
const cachedValidatorsBySchemaHash = {}; | ||
|
||
const getValidate = async (schema) => { | ||
const schemaHash = objectHash(deepSortObjectByKey(schema)); | ||
if (cachedValidatorsBySchemaHash[schemaHash]) { | ||
return cachedValidatorsBySchemaHash[schemaHash]; | ||
} | ||
const filename = `${schemaHash}.js`; | ||
const cachePath = path.resolve(getCacheDir(), filename); | ||
|
||
const generate = async () => { | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
coerceTypes: 'array', | ||
verbose: true, | ||
strict: true, | ||
strictRequired: false, | ||
code: { source: true }, | ||
}); | ||
require('ajv-formats').default(ajv); | ||
// Ensure AJV related packages work well when there are mutliple AJV installations around | ||
// See: https://github.com/ajv-validator/ajv/issues/1390#issuecomment-763138202 | ||
ajv.opts.code.formats = Ajv._`require("ajv-formats/dist/formats").fullFormats`; | ||
require('ajv-keywords')(ajv, 'regexp'); | ||
const validate = ajv.compile(schema); | ||
const moduleCode = standaloneCode(ajv, validate); | ||
await fs.promises.writeFile(cachePath, moduleCode); | ||
}; | ||
await ensureExists(cachePath, generate); | ||
const loadedModuleCode = await fs.promises.readFile(cachePath, 'utf-8'); | ||
const validator = requireFromString( | ||
loadedModuleCode, | ||
path.resolve(__dirname, `[generated-ajv-validate]${filename}`) | ||
); | ||
cachedValidatorsBySchemaHash[schemaHash] = validator; | ||
return validator; | ||
}; | ||
|
||
module.exports = getValidate; |
Oops, something went wrong.