generated from fastify/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
53 lines (43 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'
const AjvReference = Symbol.for('fastify.ajv-compiler.reference')
const ValidatorCompiler = require('./lib/validator-compiler')
const SerializerCompiler = require('./lib/serializer-compiler')
function AjvCompiler (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 uniqueAjvKey = getPoolKey(externalSchemas, options.customOptions)
if (validatorPool.has(uniqueAjvKey)) {
return validatorPool.get(uniqueAjvKey)
}
const compiler = new ValidatorCompiler(externalSchemas, options)
const ret = compiler.buildValidatorFunction.bind(compiler)
validatorPool.set(uniqueAjvKey, ret)
if (options.customOptions.code !== undefined) {
ret[AjvReference] = compiler
}
return ret
}
}
function getPoolKey (externalSchemas, options) {
const externals = JSON.stringify(externalSchemas)
const ajvConfig = JSON.stringify(options)
return `${externals}${ajvConfig}`
}
module.exports = AjvCompiler
module.exports.default = AjvCompiler
module.exports.AjvCompiler = AjvCompiler
module.exports.AjvReference = AjvReference
module.exports.StandaloneValidator = require('./standalone')