Redocly / redocly-cli-cookbook

A community-created collection of configuration, plugins and techniques for getting the best from Redocly CLI in every situation.
MIT License
25 stars 7 forks source link

Custom rules -- can a built-in rule be overridden to relax enforcement of certain specific conditions? #27

Open lagouyn opened 9 months ago

lagouyn commented 9 months ago

Hello.

I'm using Redocly for validation of a Swagger JSON file that references (via $ref) a JSON Schema file.

The top-level object in my JSON Schema file contains these two properties:

"$schema": "http://json-schema.org/draft-04/schema",
"id": "api-response.json",

The Redocly spec rule doesn't like these two properties ... I'd like to be able to ignore those specific top-level properties without totally disabling the spec rule.

Is there a way to configure a built-in rule to allow it to continue to enforce its rules, but with certain exceptions?

Thank you.

lagouyn commented 9 months ago

Ah, perhaps when using the lint command, I can make use of Redocly's support for the Ignore file, which I can create via --generate-ignore-file=true.

lornajane commented 9 months ago

To ignore specific known errors without disabling a whole rule, yes the ignore file is the best approach.

It's hard to comment on the original error without seeing the context of the file and the referenced schema.

jeremyfiel commented 6 months ago

This is a problem encountered with OAS 3.0.x and JSON Schema usage.

You can add those keywords as type-extensions to Redocly's config file by creating these two entries.

stick tap to this answer. https://github.com/Redocly/redocly-cli/issues/689#issuecomment-1309762532

redocly.yaml

plugins:
  - './linting/plugins/index.js'

./linting/plugins/index.js

/** @type { import('@redocly/cli').typeExtensionsConfig} */

// OAS 3.0.x doesn't support $schema and id/$id, thus we need to add these two properties as extended types so they will not throw a linting error

const typeExtension = {
    oas3(types) {
        return {
            ...types,
            Schema: {
                ...types.Schema,
                properties: {
                    ...types.Schema.properties,
                    $id: { type: 'string' },
                    // id is only for draft-04 or older
                    id: { type: 'string' },
                    $schema: { type: 'string' },
                    definitions: { type: 'object' },
                }
            }
        };
    },
}

module.exports = { typeExtension }