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

Strip external schemas from standalone compiled code #704

Merged
merged 1 commit into from
May 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ function buildStandaloneCode (contextFunc, context, serializer, validator) {
ajvDependencyCode += 'const validator = null\n'
}

// Don't need to keep external schemas once compiled
// validatorState will hold external schemas if it needs them
const { schema, ...serializerState } = serializer.getState()

return `
'use strict'

const Serializer = require('fast-json-stringify/lib/serializer')
const serializerState = ${JSON.stringify(serializer.getState())}
const serializerState = ${JSON.stringify(serializerState)}
const serializer = Serializer.restoreFromState(serializerState)

${ajvDependencyCode}
Expand Down
111 changes: 103 additions & 8 deletions test/standalone-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ test('activate standalone mode', async (t) => {
t.type(code, 'string')
t.equal(code.indexOf('ajv'), -1)

const destionation = path.resolve(tmpDir, 'standalone.js')
const destination = path.resolve(tmpDir, 'standalone.js')

t.teardown(async () => {
await fs.promises.rm(destionation, { force: true })
await fs.promises.rm(destination, { force: true })
})

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

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

const destionation = path.resolve(tmpDir, 'standalone2.js')
const destination = path.resolve(tmpDir, 'standalone2.js')

t.teardown(async () => {
await fs.promises.rm(destionation, { force: true })
await fs.promises.rm(destination, { force: true })
})

await fs.promises.writeFile(destionation, code)
const standalone = require(destionation)
await fs.promises.writeFile(destination, code)
const standalone = require(destination)
t.same(standalone({
kind: 'foobar',
foo: 'FOO',
Expand All @@ -119,3 +119,98 @@ test('test ajv schema', async (t) => {
}]
}))
})

test('no need to keep external schemas once compiled', async (t) => {
t.plan(1)
const externalSchema = {
first: {
definitions: {
id1: {
type: 'object',
properties: {
id1: {
type: 'integer'
}
}
}
}
}
}
const code = fjs({
$ref: 'first#/definitions/id1'
}, {
mode: 'standalone',
schema: externalSchema
})

const destination = path.resolve(tmpDir, 'standalone3.js')

t.teardown(async () => {
await fs.promises.rm(destination, { force: true })
})

await fs.promises.writeFile(destination, code)
const standalone = require(destination)

t.same(standalone({ id1: 5 }), JSON.stringify({ id1: 5 }), 'serialization works with external schemas')
})

test('no need to keep external schemas once compiled - with oneOf validator', async (t) => {
t.plan(2)

const externalSchema = {
ext: {
definitions: {
oBaz: {
type: 'object',
properties: {
baz: { type: 'number' }
},
required: ['baz']
},
oBar: {
type: 'object',
properties: {
bar: { type: 'string' }
},
required: ['bar']
},
other: {
type: 'string',
const: 'other'
}
}
}
}

const schema = {
title: 'object with oneOf property value containing refs to external schema',
type: 'object',
properties: {
oneOfSchema: {
oneOf: [
{ $ref: 'ext#/definitions/oBaz' },
{ $ref: 'ext#/definitions/oBar' }
]
}
},
required: ['oneOfSchema']
}

const code = fjs(schema, {
mode: 'standalone',
schema: externalSchema
})

const destination = path.resolve(tmpDir, 'standalone-oneOf-ref.js')

t.teardown(async () => {
await fs.promises.rm(destination, { force: true })
})

await fs.promises.writeFile(destination, code)
const stringify = require(destination)

t.equal(stringify({ oneOfSchema: { baz: 5 } }), '{"oneOfSchema":{"baz":5}}')
t.equal(stringify({ oneOfSchema: { bar: 'foo' } }), '{"oneOfSchema":{"bar":"foo"}}')
})