question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule action for object keys

See original GitHub issue

Detailed 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:closed
  • Created 5 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pderaaijcommented, Jan 8, 2019

Feel free to add!

1reaction
bottoycommented, Jan 7, 2019

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 …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object.keys() - JavaScript - MDN Web Docs
The Object.keys() method returns an array of a given object's own enumerable string-keyed property names.
Read more >
Lifecycle configuration elements - Amazon Simple Storage ...
The rule directs Amazon S3 to perform lifecycle actions on objects with two tags (with the specific tag key and value). <LifecycleConfiguration> <Rule>...
Read more >
Can we use action.payload on multiple object keys?
I wanted to know if this is the right way to do this and if I can actually pass action.payload to two different...
Read more >
User Object Properties in Rules - Auth0
Property Data Type Description user.email text (unique) User's email address. user.family_name text User's family name. user.given_name text User's given name.
Read more >
JavaScript Object.keys, Values, Entries - W3docs
Defining Object.keys, values, and entries ... As a rule, for plain objects, you can use the methods below: ... There are significant differences...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found