Last active
April 17, 2023 12:17
-
-
Save lornajane/cf11a33b7cbe862c5ddd4cf3b4182ea5 to your computer and use it in GitHub Desktop.
Redocly custom rule from built-in rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NamedBooleans = require('./rules/named-booleans'); | |
module.exports = { | |
id: 'extras-plugin', | |
rules: { | |
oas3: { | |
'boolean-naming-rules': NamedBooleans, | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in the rules/ directory | |
module.exports = NamedBooleans; | |
// based on https://github.com/Redocly/redocly-cli/blob/main/packages/core/src/rules/oas3/boolean-parameter-prefixes.ts | |
function NamedBooleans({ suppliedSuffixes}) { | |
const suffixes = suppliedSuffixes || ['is', 'has']; | |
const regexp = new RegExp(`[A-Z-_](${suffixes.join('|')})$`); | |
const wrappedSuffixes = suffixes.map((p) => `\`${p}\``); | |
const suffixesString = | |
wrappedSuffixes.length === 1 | |
? wrappedSuffixes[0] | |
: wrappedSuffixes.slice(0, -1).join(', ') + ' or ' + wrappedSuffixes[suffixes.length - 1]; | |
return { | |
Parameter: { | |
// the ctx parameter is different than the built-in plugins) | |
Schema(schema, ctx, parents) { | |
if (schema.type === 'boolean' && !regexp.test(parents.Parameter.name)) { | |
ctx.report({ | |
message: `Boolean parameter \`${parents.Parameter.name}\` should have ${suffixesString} suffix.`, | |
// location defined using the ctx parameter | |
location: ctx.location.child(['name']), | |
}); | |
} | |
}, | |
}, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends: [] | |
plugins: | |
- './my-custom-plugin.js' | |
rules: | |
extras-plugin/boolean-naming-rules: | |
severity: error | |
suppliedSuffixes: ["Enabled", "Indicator"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment