diff --git a/index.js b/index.js index f630e766..5457ff06 100644 --- a/index.js +++ b/index.js @@ -146,7 +146,12 @@ function build (schema, options) { } if (options.mode === 'debug') { - return { code: dependenciesName.join('\n'), validator, ajv: validator.ajv } + return { + validator, + serializer, + code: dependenciesName.join('\n'), + ajv: validator.ajv + } } if (options.mode === 'standalone') { @@ -928,8 +933,7 @@ module.exports = build module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms -module.exports.restore = function ({ code, validator }) { - const serializer = new Serializer() +module.exports.restore = function ({ code, validator, serializer }) { // eslint-disable-next-line return (Function.apply(null, ['validator', 'serializer', code]) .apply(null, [validator, serializer])) diff --git a/test/debug-mode.test.js b/test/debug-mode.test.js index 623d37f1..b02a08fb 100644 --- a/test/debug-mode.test.js +++ b/test/debug-mode.test.js @@ -5,6 +5,7 @@ const fjs = require('..') const Ajv = require('ajv').default const Validator = require('../lib/validator') +const Serializer = require('../lib/serializer') function build (opts) { return fjs({ @@ -20,17 +21,18 @@ function build (opts) { } test('activate debug mode', t => { - t.plan(4) + t.plan(5) const debugMode = build({ debugMode: true }) t.type(debugMode, 'object') t.ok(debugMode.ajv instanceof Ajv) t.ok(debugMode.validator instanceof Validator) + t.ok(debugMode.serializer instanceof Serializer) t.type(debugMode.code, 'string') }) test('activate debug mode truthy', t => { - t.plan(4) + t.plan(5) const debugMode = build({ debugMode: 'yes' }) @@ -38,15 +40,17 @@ test('activate debug mode truthy', t => { t.type(debugMode.code, 'string') t.ok(debugMode.ajv instanceof Ajv) t.ok(debugMode.validator instanceof Validator) + t.ok(debugMode.serializer instanceof Serializer) }) test('to string auto-consistent', t => { - t.plan(5) + t.plan(6) const debugMode = build({ debugMode: 1 }) t.type(debugMode, 'object') t.type(debugMode.code, 'string') t.ok(debugMode.ajv instanceof Ajv) + t.ok(debugMode.serializer instanceof Serializer) t.ok(debugMode.validator instanceof Validator) const compiled = fjs.restore(debugMode) @@ -55,7 +59,7 @@ test('to string auto-consistent', t => { }) test('to string auto-consistent with ajv', t => { - t.plan(5) + t.plan(6) const debugMode = fjs({ title: 'object with multiple types field', @@ -75,6 +79,7 @@ test('to string auto-consistent with ajv', t => { t.type(debugMode.code, 'string') t.ok(debugMode.ajv instanceof Ajv) t.ok(debugMode.validator instanceof Validator) + t.ok(debugMode.serializer instanceof Serializer) const compiled = fjs.restore(debugMode) const tobe = JSON.stringify({ str: 'Foo' }) @@ -106,3 +111,11 @@ test('to string auto-consistent with ajv-formats', t => { t.same(compiled({ str: 'foo@bar.com' }), tobe) t.throws(() => compiled({ str: 'foo' })) }) + +test('debug should restore the same serializer instance', t => { + t.plan(1) + + const debugMode = fjs({ type: 'integer' }, { debugMode: 1, rounding: 'ceil' }) + const compiled = fjs.restore(debugMode) + t.same(compiled(3.95), 4) +})