Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return Serializer instance in debug mode #529

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: '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)
})