-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: standalone ajv schemas * docs: update readme * types: update types * docs: update readme Co-authored-by: Frazer Smith <[email protected]> * chore: typo Co-authored-by: Matteo Collina <[email protected]> * chore: remove console log * feat: on-demand ajv * chore: lint * chore: typo Co-authored-by: Zed <[email protected]> * chore: update types Co-authored-by: Zed <[email protected]> Co-authored-by: Frazer Smith <[email protected]> Co-authored-by: Matteo Collina <[email protected]> Co-authored-by: Zed <[email protected]>
- Loading branch information
1 parent
310325b
commit 12fa1e9
Showing
6 changed files
with
169 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
function buildStandaloneCode (options, ajvInstance, contextFunctionCode) { | ||
const serializerCode = fs.readFileSync(path.join(__dirname, 'serializer.js')).toString() | ||
let buildAjvCode = '' | ||
let defaultAjvSchema = '' | ||
const defaultMeta = ajvInstance.defaultMeta() | ||
if (typeof defaultMeta === 'string') { | ||
defaultAjvSchema = defaultMeta | ||
} else { | ||
defaultAjvSchema = defaultMeta.$id || defaultMeta.id | ||
} | ||
const shouldUseAjv = contextFunctionCode.indexOf('ajv') !== -1 | ||
// we need to export the custom json schema | ||
let ajvSchemasCode = '' | ||
if (shouldUseAjv) { | ||
ajvSchemasCode += `const ajv = buildAjv(${JSON.stringify(options.ajv || {})})\n` | ||
for (const [id, schema] of Object.entries(ajvInstance.schemas)) { | ||
// should skip ajv default schema | ||
if (id === defaultAjvSchema) continue | ||
ajvSchemasCode += `ajv.addSchema(${JSON.stringify(schema.schema)}, "${id}")\n` | ||
} | ||
buildAjvCode = fs.readFileSync(path.join(__dirname, 'ajv.js')).toString() | ||
buildAjvCode = buildAjvCode.replace("'use strict'", '').replace('module.exports = buildAjv', '') | ||
} | ||
return ` | ||
'use strict' | ||
${serializerCode.replace("'use strict'", '').replace('module.exports = ', '')} | ||
${buildAjvCode} | ||
const serializer = new Serializer(${JSON.stringify(options || {})}) | ||
${ajvSchemasCode} | ||
${contextFunctionCode.replace('return main', '')} | ||
module.exports = main | ||
` | ||
} | ||
|
||
module.exports = buildStandaloneCode |
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