tlivings / enjoi

Converts a JSON schema to a Joi schema.
Apache License 2.0
282 stars 57 forks source link

[Bug] Using a $ref in a anyOf or allOf triggers a RangeError #110

Open Vladirien opened 3 years ago

Vladirien commented 3 years ago

Hi,

Using a $ref in a anyOf or an affOf in a schema triggers the following error: RangeError: Maximum call stack size exceeded

Code to reproduce

const Enjoi = require('enjoi');

const jsonSchema = {
    allOf: [
        {
            $ref: "#/components/schemas/TestObject"
        },
        {
            type: 'object',
            properties: {
                info: {
                    type: 'string'
                }
            }
        }
    ]
};

const subSchemas = {
    components:{
        schemas: {
            TestObject:{
                additionalProperties: false,
                type: 'object',
                properties:{
                    id: {
                        type: 'number'
                    },
                    name:{
                        type: 'string'
                    },
                    subobject:{
                        $ref: "#/components/schemas/TestSubObject"
                    },
                    type:{
                        type: 'string',
                        enum: [
                            'AUDIENCE',
                            'CAMPAIGN'
                        ]
                    }
                }
            },
            TestSubObject:{
                additionalProperties: false,
                type: 'object',
                properties:{
                    id:{
                        type: 'number'
                    },
                    name:{
                        type: 'string'
                    }
                }
            }
        }
    }

};

const schema = Enjoi.schema(jsonSchema, { subSchemas });

const options = {
    allowUnknown: false,
    abortEarly: false,
    stripUnknown: false
};

const result = schema.validate({
    id: 7,
    name: 'test',
    type: 'AUDIENCE',
    info: 'good'
}, options);

console.log('res', result);

Expected result

The validation should be able to handle $refs in anyOf or allOf

Thanks in advance!