diff --git a/.redocly.yaml b/.redocly.yaml index 4e5e92503a4..fd9109487fd 100644 --- a/.redocly.yaml +++ b/.redocly.yaml @@ -2,6 +2,11 @@ apis: main: root: docs/api/apiv3/openapi-spec.yml -lint: - extends: - - recommended +extends: + - recommended + +plugins: + - '.redocly/plugins/custom-rules.js' + +rules: + custom-rules/skip-abbreviated-examples: warn diff --git a/.redocly/plugins/custom-rules.js b/.redocly/plugins/custom-rules.js new file mode 100644 index 00000000000..a91c53eacad --- /dev/null +++ b/.redocly/plugins/custom-rules.js @@ -0,0 +1,12 @@ +import SkipAbbreviatedExamples from './rules/skip-abbreviated-examples.js' + +export default function CustomRulesPlugin() { + return { + id: 'custom-rules', + rules: { + oas3: { + 'skip-abbreviated-examples': SkipAbbreviatedExamples, + } + } + } +} diff --git a/.redocly/plugins/rules/skip-abbreviated-examples.js b/.redocly/plugins/rules/skip-abbreviated-examples.js new file mode 100644 index 00000000000..a85f655b14c --- /dev/null +++ b/.redocly/plugins/rules/skip-abbreviated-examples.js @@ -0,0 +1,38 @@ +function removeNodesWithKey(obj, key) { + if (obj === null) return; + if (typeof obj === 'string') return; + if (typeof obj === 'number') return; + + if (Array.isArray(obj)) { + for (let i = obj.length - 1; i >= 0; i--) { + if (obj[i] === null) continue; + if (obj[i][key] !== undefined) { + obj.splice(i, 1); + } + } + } else { + const keys = Object.keys(obj); + for (const k of keys) { + if (obj[k] === null) continue; + if (obj[k][key] !== undefined) { + delete obj[k]; + } + } + } + + for (const k of Object.keys(obj)) { + removeNodesWithKey(obj[k], key); + } +} + +export default function SkipAbbreviatedExamples() { + return { + Example: { + enter(example, _ctx) { + // remove every nested object with the `_abbreviated` key, + // but keep the rest to be run against other rules. + removeNodesWithKey(example.value, '_abbreviated'); + }, + }, + } +} diff --git a/script/api/validate_spec b/script/api/validate_spec index 8bc88aef3ec..e4f70b467cb 100755 --- a/script/api/validate_spec +++ b/script/api/validate_spec @@ -18,7 +18,7 @@ begin file.puts full_spec end - system("npx @redocly/cli lint #{openapi_yaml_spec_path}") + system("npx @redocly/cli lint --config=.redocly.yaml #{openapi_yaml_spec_path}") status = $?.exitstatus ensure