tlivings / enjoi

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

Infinite loop when schema contains a self-reference #106

Closed nlundquist closed 3 years ago

nlundquist commented 3 years ago
import * as Enjoi from "https://cdn.skypack.dev/enjoi@8.0.0";

const jsonSchema = {
  type: "object",
  properties: {
    value: { type: "string" },
    next: { $ref: '#' }
  }
}

// add self reference to schema, equivalent to dereferencing `next: { $ref: '#' }`
jsonSchema.properties.next = jsonSchema

const schema = Enjoi.schema(jsonSchema)
const result = schema.validate({
  value: "foo",
  next: {
     value: "bar",
     next: {
       value: 0
     }
  }
})

The result in the above code sample should return an error about value: 0 not being a string. Instead Enjoi fails at schema parsing time with a call stack size exceeded error.

This is because Enjoi can't handle schemas with valid self references in them. This issue had been previously raised in #5 and you suggested handling circular references as part of a dereferencing step using another tool. However even when using a fully dereferenced schema, like the above example, Enjoi still breaks.

As is, seems like Enjoi is incapable of parsing valid, recursive JSONSchema no matter how they're passed.