Skip to content

Error when using anyOf with null and object type #74

@florianreinhart

Description

@florianreinhart

I ran into this issue when trying to create a JSON schema for fastify. We have a property that can either be null or be an object. I've created a simple example to reproduce the issue:

const fastJson = require("fast-json-stringify")

const mySchema = {
    type: "object",
    properties: {
        myProperty: {
            anyOf: [
                {
                    type: "object",
                    properties: {
                        propertyA: {
                            type: "string"
                        }
                    }
                },
                {
                    type: "null",
                }
            ]
        }
    }
}

const stringify = fastJson(mySchema)

let json = stringify({
    myProperty: null
})
console.log(json)

json = stringify({
    myProperty: {
        propertyA: "hello"
    }
})
console.log(json)

It looks like fast-json-stringify generates invalid code:

ReferenceError: $mainmyPropertyundefined is not defined
    at $main (eval at build (/test/fast-json-stringify/node_modules/fast-json-stringify/index.js:99:20), <anonymous>:77:9)
    at Object.<anonymous> (/test/fast-json-stringify/index.js:31:8)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions