Skip to content

Commit 26ef3cd

Browse files
fix: return Serializer instance in debug mode (#529)
1 parent 3a737e2 commit 26ef3cd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ function build (schema, options) {
146146
}
147147

148148
if (options.mode === 'debug') {
149-
return { code: dependenciesName.join('\n'), validator, ajv: validator.ajv }
149+
return {
150+
validator,
151+
serializer,
152+
code: dependenciesName.join('\n'),
153+
ajv: validator.ajv
154+
}
150155
}
151156

152157
if (options.mode === 'standalone') {
@@ -928,8 +933,7 @@ module.exports = build
928933

929934
module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms
930935

931-
module.exports.restore = function ({ code, validator }) {
932-
const serializer = new Serializer()
936+
module.exports.restore = function ({ code, validator, serializer }) {
933937
// eslint-disable-next-line
934938
return (Function.apply(null, ['validator', 'serializer', code])
935939
.apply(null, [validator, serializer]))

test/debug-mode.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fjs = require('..')
55

66
const Ajv = require('ajv').default
77
const Validator = require('../lib/validator')
8+
const Serializer = require('../lib/serializer')
89

910
function build (opts) {
1011
return fjs({
@@ -20,33 +21,36 @@ function build (opts) {
2021
}
2122

2223
test('activate debug mode', t => {
23-
t.plan(4)
24+
t.plan(5)
2425
const debugMode = build({ debugMode: true })
2526

2627
t.type(debugMode, 'object')
2728
t.ok(debugMode.ajv instanceof Ajv)
2829
t.ok(debugMode.validator instanceof Validator)
30+
t.ok(debugMode.serializer instanceof Serializer)
2931
t.type(debugMode.code, 'string')
3032
})
3133

3234
test('activate debug mode truthy', t => {
33-
t.plan(4)
35+
t.plan(5)
3436

3537
const debugMode = build({ debugMode: 'yes' })
3638

3739
t.type(debugMode, 'object')
3840
t.type(debugMode.code, 'string')
3941
t.ok(debugMode.ajv instanceof Ajv)
4042
t.ok(debugMode.validator instanceof Validator)
43+
t.ok(debugMode.serializer instanceof Serializer)
4144
})
4245

4346
test('to string auto-consistent', t => {
44-
t.plan(5)
47+
t.plan(6)
4548
const debugMode = build({ debugMode: 1 })
4649

4750
t.type(debugMode, 'object')
4851
t.type(debugMode.code, 'string')
4952
t.ok(debugMode.ajv instanceof Ajv)
53+
t.ok(debugMode.serializer instanceof Serializer)
5054
t.ok(debugMode.validator instanceof Validator)
5155

5256
const compiled = fjs.restore(debugMode)
@@ -55,7 +59,7 @@ test('to string auto-consistent', t => {
5559
})
5660

5761
test('to string auto-consistent with ajv', t => {
58-
t.plan(5)
62+
t.plan(6)
5963

6064
const debugMode = fjs({
6165
title: 'object with multiple types field',
@@ -75,6 +79,7 @@ test('to string auto-consistent with ajv', t => {
7579
t.type(debugMode.code, 'string')
7680
t.ok(debugMode.ajv instanceof Ajv)
7781
t.ok(debugMode.validator instanceof Validator)
82+
t.ok(debugMode.serializer instanceof Serializer)
7883

7984
const compiled = fjs.restore(debugMode)
8085
const tobe = JSON.stringify({ str: 'Foo' })
@@ -106,3 +111,11 @@ test('to string auto-consistent with ajv-formats', t => {
106111
t.same(compiled({ str: '[email protected]' }), tobe)
107112
t.throws(() => compiled({ str: 'foo' }))
108113
})
114+
115+
test('debug should restore the same serializer instance', t => {
116+
t.plan(1)
117+
118+
const debugMode = fjs({ type: 'integer' }, { debugMode: 1, rounding: 'ceil' })
119+
const compiled = fjs.restore(debugMode)
120+
t.same(compiled(3.95), 4)
121+
})

0 commit comments

Comments
 (0)