Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lagouyn opened this issue Jan 25, 2024 · 3 comments

Comments

@lagouyn
Copy link

lagouyn commented Jan 25, 2024

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
Copy link
Author

lagouyn commented Jan 25, 2024

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
Copy link
Collaborator

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
Copy link
Contributor

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. Redocly/redocly-cli#689 (comment)

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants