Skip to content

Instantly share code, notes, and snippets.

@lornajane
Last active April 17, 2023 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lornajane/cf11a33b7cbe862c5ddd4cf3b4182ea5 to your computer and use it in GitHub Desktop.
Save lornajane/cf11a33b7cbe862c5ddd4cf3b4182ea5 to your computer and use it in GitHub Desktop.
Redocly custom rule from built-in rule
const NamedBooleans = require('./rules/named-booleans');
module.exports = {
id: 'extras-plugin',
rules: {
oas3: {
'boolean-naming-rules': NamedBooleans,
}
}
}
// 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']),
});
}
},
},
};
}
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