Resolve references in custom rule
I have to check an API which requires a distinct schema definition in case of a negative status code.
My current implementation takes the responses and if it is a negative status code it would check whether it complies to the schema:
export default function XNegStatusCode() {
return {
Responses: {
enter(obj, ctx) {
for (const key of Object.keys(obj)) {
if (httpNegativeStatusCode.test(key)) {
Some OpenAPI definitions use $ref to point to the schema definition. However, this reference is not resolved. From the documentation I'd expect to be able to call ctx.resolve(obj) which however is undefined.
What do you mean by a negative status code? As far as I'm concerned, they should comply with the standard: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml. What is your API description? Do you want to replace the existing $refs with something else? Maybe simply putting a remote url into those $refs will be enough for your case?
To be clear, if your document doesn't contain the $ref you want to resolve, it will return undefined because visitors are synchronous functions and all refs get resolved in a preparation step. So you need to put this $ref somewhere in your API description anyway.
The status codes are standard, but the response must have a well-defined structure. I noticed that the internal rules access the context for resolve only in the leave method. After using the leave method, the resolve method is defined.