Skip to content

Commit a2e4ef9

Browse files
authored
feat: Strip external schemas from standalone compiled code (#704)
1 parent c873689 commit a2e4ef9

File tree

2 files changed

+108
-9
lines changed

2 files changed

+108
-9
lines changed

lib/standalone.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ function buildStandaloneCode (contextFunc, context, serializer, validator) {
1010
ajvDependencyCode += 'const validator = null\n'
1111
}
1212

13+
// Don't need to keep external schemas once compiled
14+
// validatorState will hold external schemas if it needs them
15+
const { schema, ...serializerState } = serializer.getState()
16+
1317
return `
1418
'use strict'
1519
1620
const Serializer = require('fast-json-stringify/lib/serializer')
17-
const serializerState = ${JSON.stringify(serializer.getState())}
21+
const serializerState = ${JSON.stringify(serializerState)}
1822
const serializer = Serializer.restoreFromState(serializerState)
1923
2024
${ajvDependencyCode}

test/standalone-mode.test.js

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ test('activate standalone mode', async (t) => {
2626
t.type(code, 'string')
2727
t.equal(code.indexOf('ajv'), -1)
2828

29-
const destionation = path.resolve(tmpDir, 'standalone.js')
29+
const destination = path.resolve(tmpDir, 'standalone.js')
3030

3131
t.teardown(async () => {
32-
await fs.promises.rm(destionation, { force: true })
32+
await fs.promises.rm(destination, { force: true })
3333
})
3434

35-
await fs.promises.writeFile(destionation, code)
36-
const standalone = require(destionation)
35+
await fs.promises.writeFile(destination, code)
36+
const standalone = require(destination)
3737
t.same(standalone({ firstName: 'Foo', surname: 'bar' }), JSON.stringify({ firstName: 'Foo' }), 'surname evicted')
3838
})
3939

@@ -89,14 +89,14 @@ test('test ajv schema', async (t) => {
8989
t.type(code, 'string')
9090
t.equal(code.indexOf('ajv') > 0, true)
9191

92-
const destionation = path.resolve(tmpDir, 'standalone2.js')
92+
const destination = path.resolve(tmpDir, 'standalone2.js')
9393

9494
t.teardown(async () => {
95-
await fs.promises.rm(destionation, { force: true })
95+
await fs.promises.rm(destination, { force: true })
9696
})
9797

98-
await fs.promises.writeFile(destionation, code)
99-
const standalone = require(destionation)
98+
await fs.promises.writeFile(destination, code)
99+
const standalone = require(destination)
100100
t.same(standalone({
101101
kind: 'foobar',
102102
foo: 'FOO',
@@ -119,3 +119,98 @@ test('test ajv schema', async (t) => {
119119
}]
120120
}))
121121
})
122+
123+
test('no need to keep external schemas once compiled', async (t) => {
124+
t.plan(1)
125+
const externalSchema = {
126+
first: {
127+
definitions: {
128+
id1: {
129+
type: 'object',
130+
properties: {
131+
id1: {
132+
type: 'integer'
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
139+
const code = fjs({
140+
$ref: 'first#/definitions/id1'
141+
}, {
142+
mode: 'standalone',
143+
schema: externalSchema
144+
})
145+
146+
const destination = path.resolve(tmpDir, 'standalone3.js')
147+
148+
t.teardown(async () => {
149+
await fs.promises.rm(destination, { force: true })
150+
})
151+
152+
await fs.promises.writeFile(destination, code)
153+
const standalone = require(destination)
154+
155+
t.same(standalone({ id1: 5 }), JSON.stringify({ id1: 5 }), 'serialization works with external schemas')
156+
})
157+
158+
test('no need to keep external schemas once compiled - with oneOf validator', async (t) => {
159+
t.plan(2)
160+
161+
const externalSchema = {
162+
ext: {
163+
definitions: {
164+
oBaz: {
165+
type: 'object',
166+
properties: {
167+
baz: { type: 'number' }
168+
},
169+
required: ['baz']
170+
},
171+
oBar: {
172+
type: 'object',
173+
properties: {
174+
bar: { type: 'string' }
175+
},
176+
required: ['bar']
177+
},
178+
other: {
179+
type: 'string',
180+
const: 'other'
181+
}
182+
}
183+
}
184+
}
185+
186+
const schema = {
187+
title: 'object with oneOf property value containing refs to external schema',
188+
type: 'object',
189+
properties: {
190+
oneOfSchema: {
191+
oneOf: [
192+
{ $ref: 'ext#/definitions/oBaz' },
193+
{ $ref: 'ext#/definitions/oBar' }
194+
]
195+
}
196+
},
197+
required: ['oneOfSchema']
198+
}
199+
200+
const code = fjs(schema, {
201+
mode: 'standalone',
202+
schema: externalSchema
203+
})
204+
205+
const destination = path.resolve(tmpDir, 'standalone-oneOf-ref.js')
206+
207+
t.teardown(async () => {
208+
await fs.promises.rm(destination, { force: true })
209+
})
210+
211+
await fs.promises.writeFile(destination, code)
212+
const stringify = require(destination)
213+
214+
t.equal(stringify({ oneOfSchema: { baz: 5 } }), '{"oneOfSchema":{"baz":5}}')
215+
t.equal(stringify({ oneOfSchema: { bar: 'foo' } }), '{"oneOfSchema":{"bar":"foo"}}')
216+
})

0 commit comments

Comments
 (0)