Skip to content

Commit

Permalink
fix: return Serializer instance in debug mode (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Sep 9, 2022
1 parent 3a737e2 commit 26ef3cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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]))
Expand Down
21 changes: 17 additions & 4 deletions test/debug-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -20,33 +21,36 @@ 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' })

t.type(debugMode, 'object')
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)
Expand All @@ -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',
Expand All @@ -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' })
Expand Down Expand Up @@ -106,3 +111,11 @@ test('to string auto-consistent with ajv-formats', t => {
t.same(compiled({ str: '[email protected]' }), 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)
})

0 comments on commit 26ef3cd

Please sign in to comment.