New rule action for object keys
See original GitHub issueDetailed description
For my custom rule set that I am creating I would love to have a rule that checks the keys of an object according to a pattern. For example, having a custom rule that ensures the property names are in snake_case.
This is especially tricky in the schema object as fields in the schema object are a schema object on its own. I’ve played around with several options within the current abilities but came short every time.
Possible implementation
I would like to introduce a new rule action that targets the keys of an object, applying a pattern to each and every key.
In my local environment I have this as a rule definition
{ "name":"schema-property-name-snake-case", "object": "schema", "enabled": true, "description": "parameter must be in snake_case", "keyPattern": { "property": "properties", "value": "^([a-z_])*$" } }
and introduced the following to the linter
if (rule.keyPattern) {
const { property, value } = rule.patternForKey;
const target = (property) ? object[property] : false;
if (target) {
const re = new RegExp(value);
const keys = Object.keys(target).forEach(key => {
options.context.push((options.context[options.context.length - 1] + '/' + key).split('//').join('/'));
ensure(rule, () => {
should(re.test(key)).be.exactly(true, rule.description);
});
options.context.pop();
});
}
}
I am curious if this is something we would like to add or is a bad approach.
** Continuing from: https://github.com/wework/speccy/issues/242
Issue Analytics
- State:
- Created 5 years ago
- Comments:17 (9 by maintainers)

Top Related StackOverflow Question
Feel free to add!
Hi @MikeRalphson , can you look as well to the responses object - suffers the same issue? Thx
Speccy rules on the “responses” object are not triggered as the validator only works on the ‘response’ object. We check that the response code is a number so the pattern rule of @pderaaij would be perfect for this…
It would be something like (oas-validator/index.js near line 778)
if (options.lint) options.linter('responses',op.responses,'responses',options);I think. Although the parameters are somewhat guessed …