Skip to content

Commit de06bca

Browse files
authored
add debug mode (#216)
* add debug mode * add restore utils * add docs restore
1 parent 0523bf9 commit de06bca

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,31 @@ allowing user input to directly supply a schema.
578578
It can't be guaranteed that allowing user input for the schema couldn't feasibly expose an attack
579579
vector.
580580
581+
<a name="debug"></a>
582+
### Debug Mode
583+
584+
The debug mode can be activated during your development to understand what is going on when things do not
585+
work as you expect.
586+
587+
```js
588+
const debugCompiled = fastJson({
589+
title: 'default string',
590+
type: 'object',
591+
properties: {
592+
firstName: {
593+
type: 'string'
594+
}
595+
}
596+
}, { debugMode: true })
597+
598+
console.log(debugCompiled) // it is an array of functions that can create your `stringify` function
599+
console.log(debugCompiled.toString()) // print a "ready to read" string function, you can save it to a file
600+
601+
const rawString = debugCompiled.toString()
602+
const stringify = fastJson.restore(rawString) // use the generated string to get back the `stringify` function
603+
console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}'
604+
```
605+
581606
<a name="acknowledgements"></a>
582607
## Acknowledgements
583608

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ function build (schema, options) {
138138
}
139139

140140
dependenciesName.push(code)
141+
142+
if (options.debugMode) {
143+
dependenciesName.toString = function () {
144+
return dependenciesName.join('\n')
145+
}
146+
return dependenciesName
147+
}
148+
141149
return (Function.apply(null, dependenciesName).apply(null, dependencies))
142150
}
143151

@@ -1156,3 +1164,16 @@ function isEmpty (schema) {
11561164
}
11571165

11581166
module.exports = build
1167+
1168+
module.exports.restore = function (debugModeStr, options = {}) {
1169+
const dependencies = [debugModeStr]
1170+
const args = []
1171+
if (debugModeStr.startsWith('ajv')) {
1172+
dependencies.unshift('ajv')
1173+
args.push(new Ajv(options.ajv))
1174+
}
1175+
1176+
// eslint-disable-next-line
1177+
return (Function.apply(null, ['ajv', debugModeStr])
1178+
.apply(null, args))
1179+
}

test/debug-mode.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const fjs = require('..')
5+
6+
function build (opts) {
7+
return fjs({
8+
title: 'default string',
9+
type: 'object',
10+
properties: {
11+
firstName: {
12+
type: 'string'
13+
}
14+
},
15+
required: ['firstName']
16+
}, opts)
17+
}
18+
19+
test('activate debug mode', t => {
20+
t.plan(2)
21+
const debugMode = build({ debugMode: true })
22+
t.type(debugMode, Array)
23+
t.like(debugMode.toString.toString(), 'join', 'to string override')
24+
})
25+
26+
test('activate debug mode truthy', t => {
27+
t.plan(2)
28+
const debugMode = build({ debugMode: 'yes' })
29+
t.type(debugMode, Array)
30+
t.like(debugMode.toString.toString(), 'join', 'to string override')
31+
})
32+
33+
test('to string auto-consistent', t => {
34+
t.plan(2)
35+
const debugMode = build({ debugMode: 1 })
36+
t.type(debugMode, Array)
37+
38+
const str = debugMode.toString()
39+
const compiled = fjs.restore(str)
40+
const tobe = JSON.stringify({ firstName: 'Foo' })
41+
t.deepEquals(compiled({ firstName: 'Foo', surname: 'bar' }), tobe, 'surname evicted')
42+
})
43+
44+
test('to string auto-consistent with ajv', t => {
45+
t.plan(2)
46+
const debugMode = fjs({
47+
title: 'object with multiple types field',
48+
type: 'object',
49+
properties: {
50+
str: {
51+
anyOf: [{
52+
type: 'string'
53+
}, {
54+
type: 'boolean'
55+
}]
56+
}
57+
}
58+
}, { debugMode: 1 })
59+
t.type(debugMode, Array)
60+
61+
const str = debugMode.toString()
62+
const compiled = fjs.restore(str)
63+
const tobe = JSON.stringify({ str: 'Foo' })
64+
t.deepEquals(compiled({ str: 'Foo', void: 'me' }), tobe)
65+
})

0 commit comments

Comments
 (0)