Skip to content

How to integrate a custom rule into the linter? #1485

@julioz

Description

@julioz

While adopting AIPs in my organization, and relying on api-linter to perform the validations, we have pretty quickly hit the need of writing our own custom rules, that would extend the behavior of the linter.

This page describes how one might adopt AIPs on their orgs, but I found no reference on this repository, nor on the documentation page on what the advised approach is, or the steps to be taken.

Our current workaround is to fork this repository and maintain our own copy of the linter, but that has an embedded operational cost of distributing the binary, keeping it in sync with the upstream remote, etc.

Let's say I'd like to write the simple rule as follows:

var myCustomRule = &lint.MessageRule{
    Name: lint.NewRuleName("custom", "9001", "message-naming"),
    LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
        if !strings.HasPrefix(m.GetName(), "MyCompany") {
            return []lint.Problem{{
                Message:    "Message names must start with 'MyCompany'",
                Descriptor: m,
            }}
        }
        return nil
    },
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions