Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule for enforcing snake_case for properties for AsyncAPI payloads #2115

Open
jonaslagoni opened this issue Apr 7, 2022 · 0 comments
Open
Labels
AsyncAPI Issues related to the AsyncAPI ruleset enhancement New feature or request triaged

Comments

@jonaslagoni
Copy link
Collaborator

jonaslagoni commented Apr 7, 2022

User story.
As a user, I want to force the snake_case format for all properties.

Describe the solution you'd like
I think it makes sense for the rule to only focus on a single format, but maybe in the future, it makes sense?

I want this rule to be part of the built-in ruleset.

Additional context

The way I am currently enforcing it, is through the rule:

...
functions: [..., snake-case-properties]
rules: 
  ...
  asyncapi-properties-must-follow-snake-case:
    given: $.channels.[*][subscribe,publish].message..properties
    severity: error
    then:
      function: snake-case-properties

And with the function:

function snake_case_string(str) {
  return str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(s => s.toLowerCase())
    .join('_');
}

export default (properties, _, { path }) => {
  const results = [];
  for (const [property, _] of Object.entries(properties)) {
    const expectedPropertyName = snake_case_string(property);
    if (property !== expectedPropertyName) {
      results.push({
        message: `Property MUST follow snake-case. Expected property "${property}" to be called "${expectedPropertyName}"`,
        path: [...path, property],
      });
    }
  }
  return results;
};
@jonaslagoni jonaslagoni added enhancement New feature or request AsyncAPI Issues related to the AsyncAPI ruleset labels Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AsyncAPI Issues related to the AsyncAPI ruleset enhancement New feature or request triaged
Projects
None yet
Development

No branches or pull requests

2 participants