Socket
Socket
Sign inDemoInstall

@fastify/ajv-compiler

Package Overview
Dependencies
Maintainers
9
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/ajv-compiler - npm Package Compare versions

Comparing version 3.3.1 to 3.4.0

.eslintrc

91

index.js
'use strict'
const Ajv = require('ajv').default
const AjvJTD = require('ajv/dist/jtd')
const fastUri = require('fast-uri')
const AjvReference = Symbol.for('fastify.ajv-compiler.reference')
const ValidatorCompiler = require('./lib/validator-compiler')
const SerializerCompiler = require('./lib/serializer-compiler')
const defaultAjvOptions = {
coerceTypes: 'array',
useDefaults: true,
removeAdditional: true,
uriResolver: fastUri,
addUsedSchema: false,
// Explicitly set allErrors to `false`.
// When set to `true`, a DoS attack is possible.
allErrors: false
}
function ValidatorSelector () {
function ValidatorSelector (opts) {
const validatorPool = new Map()
const serializerPool = new Map()
if (opts && opts.jtdSerializer === true) {
return function buildSerializerFromPool (externalSchemas, serializerOpts) {
const uniqueAjvKey = getPoolKey({}, serializerOpts)
if (serializerPool.has(uniqueAjvKey)) {
return serializerPool.get(uniqueAjvKey)
}
const compiler = new SerializerCompiler(externalSchemas, serializerOpts)
const ret = compiler.buildSerializerFunction.bind(compiler)
serializerPool.set(uniqueAjvKey, ret)
return ret
}
}
return function buildCompilerFromPool (externalSchemas, options) {
const externals = JSON.stringify(externalSchemas)
const ajvConfig = JSON.stringify(options.customOptions)
const uniqueAjvKey = `${externals}${ajvConfig}`
const uniqueAjvKey = getPoolKey(externalSchemas, options.customOptions)
if (validatorPool.has(uniqueAjvKey)) {

@@ -44,52 +44,9 @@ return validatorPool.get(uniqueAjvKey)

class ValidatorCompiler {
constructor (externalSchemas, options) {
// This instance of Ajv is private
// it should not be customized or used
if (options.mode === 'JTD') {
this.ajv = new AjvJTD(Object.assign({}, defaultAjvOptions, options.customOptions))
} else {
this.ajv = new Ajv(Object.assign({}, defaultAjvOptions, options.customOptions))
}
let addFormatPlugin = true
if (options.plugins && options.plugins.length > 0) {
for (const plugin of options.plugins) {
if (Array.isArray(plugin)) {
addFormatPlugin = addFormatPlugin && plugin[0].name !== 'formatsPlugin'
plugin[0](this.ajv, plugin[1])
} else {
addFormatPlugin = addFormatPlugin && plugin.name !== 'formatsPlugin'
plugin(this.ajv)
}
}
}
if (addFormatPlugin) {
require('ajv-formats')(this.ajv)
}
const sourceSchemas = Object.values(externalSchemas)
for (const extSchema of sourceSchemas) {
this.ajv.addSchema(extSchema)
}
}
buildValidatorFunction ({ schema/*, method, url, httpPart */ }) {
// Ajv does not support compiling two schemas with the same
// id inside the same instance. Therefore if we have already
// compiled the schema with the given id, we just return it.
if (schema.$id) {
const stored = this.ajv.getSchema(schema.$id)
if (stored) {
return stored
}
}
return this.ajv.compile(schema)
}
function getPoolKey (externalSchemas, options) {
const externals = JSON.stringify(externalSchemas)
const ajvConfig = JSON.stringify(options)
return `${externals}${ajvConfig}`
}
module.exports = ValidatorSelector
module.exports.AjvReference = AjvReference
module.exports.StandaloneValidator = require('./standalone')
{
"name": "@fastify/ajv-compiler",
"version": "3.3.1",
"version": "3.4.0",
"description": "Build and manage the AJV instances for the fastify framework",

@@ -41,2 +41,3 @@ "main": "index.js",

"ajv-merge-patch": "^5.0.1",
"cronometro": "^1.1.4",
"fastify": "^4.0.0",

@@ -48,3 +49,3 @@ "require-from-string": "^2.0.2",

"tap": "^16.2.0",
"tsd": "^0.23.0"
"tsd": "^0.24.1"
},

@@ -51,0 +52,0 @@ "dependencies": {

@@ -38,5 +38,5 @@ # @fastify/ajv-compiler

Moreover, the [`ajv-formats`](https://www.npmjs.com/package/ajv-formats) module is included by default.
If you need to customize check the usage section below.
If you need to customize it, check the _usage_ section below.
To customize them, see how in the [Fastify official docs](https://www.fastify.io/docs/latest/Reference/Server/#ajv).
To customize the `ajv`'s options, see how in the [Fastify official docs](https://www.fastify.io/docs/latest/Reference/Server/#ajv).

@@ -91,2 +91,26 @@

#### Fastify with JTD and serialization
You can use JTD Schemas to serialize your response object too:
```js
const factoryValidator = require('@fastify/ajv-compiler')()
const factorySerializer = require('@fastify/ajv-compiler')({ jtdSerializer: true })
const app = fastify({
jsonShorthand: false,
ajv: {
customOptions: { }, // additional JTD options
mode: 'JTD'
},
schemaController: {
compilersFactory: {
buildValidator: factoryValidator,
buildSerializer: factorySerializer
}
}
})
```
### AJV Standalone

@@ -188,3 +212,3 @@

Every Validator Compiler produced has a dedicated AJV instance, so, this factory will try to produce as less as possible AJV instances to reduce the memory footprint and the startup time.
Every Validator Compiler produced, has a dedicated AJV instance, so, this factory will try to produce as less as possible AJV instances to reduce the memory footprint and the startup time.

@@ -191,0 +215,0 @@ The variables involved to choose if a Validator Compiler can be reused are:

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc