File size: 3,257 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'

const { test } = require('node:test')
const fjs = require('..')

const Ajv = require('ajv').default
const Validator = require('../lib/validator')
const Serializer = require('../lib/serializer')

function build (opts) {
  return fjs({
    title: 'default string',
    type: 'object',
    properties: {
      firstName: {
        type: 'string'
      }
    },
    required: ['firstName']
  }, opts)
}

test('activate debug mode', t => {
  t.plan(5)
  const debugMode = build({ debugMode: true })

  t.assert.ok(typeof debugMode === 'object')
  t.assert.ok(debugMode.ajv instanceof Ajv)
  t.assert.ok(debugMode.validator instanceof Validator)
  t.assert.ok(debugMode.serializer instanceof Serializer)
  t.assert.ok(typeof debugMode.code === 'string')
})

test('activate debug mode truthy', t => {
  t.plan(5)

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

  t.assert.ok(typeof debugMode === 'object')
  t.assert.ok(typeof debugMode.code === 'string')
  t.assert.ok(debugMode.ajv instanceof Ajv)
  t.assert.ok(debugMode.validator instanceof Validator)
  t.assert.ok(debugMode.serializer instanceof Serializer)
})

test('to string auto-consistent', t => {
  t.plan(6)
  const debugMode = build({ debugMode: 1 })

  t.assert.ok(typeof debugMode === 'object')
  t.assert.ok(typeof debugMode.code === 'string')
  t.assert.ok(debugMode.ajv instanceof Ajv)
  t.assert.ok(debugMode.serializer instanceof Serializer)
  t.assert.ok(debugMode.validator instanceof Validator)

  const compiled = fjs.restore(debugMode)
  const tobe = JSON.stringify({ firstName: 'Foo' })
  t.assert.equal(compiled({ firstName: 'Foo', surname: 'bar' }), tobe, 'surname evicted')
})

test('to string auto-consistent with ajv', t => {
  t.plan(6)

  const debugMode = fjs({
    title: 'object with multiple types field',
    type: 'object',
    properties: {
      str: {
        anyOf: [{
          type: 'string'
        }, {
          type: 'boolean'
        }]
      }
    }
  }, { debugMode: 1 })

  t.assert.ok(typeof debugMode === 'object')
  t.assert.ok(typeof debugMode.code === 'string')
  t.assert.ok(debugMode.ajv instanceof Ajv)
  t.assert.ok(debugMode.validator instanceof Validator)
  t.assert.ok(debugMode.serializer instanceof Serializer)

  const compiled = fjs.restore(debugMode)
  const tobe = JSON.stringify({ str: 'Foo' })
  t.assert.equal(compiled({ str: 'Foo', void: 'me' }), tobe)
})

test('to string auto-consistent with ajv-formats', t => {
  t.plan(3)

  const debugMode = fjs({
    title: 'object with multiple types field and format keyword',
    type: 'object',
    properties: {
      str: {
        anyOf: [{
          type: 'string',
          format: 'email'
        }, {
          type: 'boolean'
        }]
      }
    }
  }, { debugMode: 1 })

  t.assert.ok(typeof debugMode === 'object')

  const compiled = fjs.restore(debugMode)
  const tobe = JSON.stringify({ str: 'foo@bar.com' })
  t.assert.equal(compiled({ str: 'foo@bar.com' }), tobe)
  t.assert.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.assert.equal(compiled(3.95), 4)
})