How to add a custom rule in lint file to check underscore?

Hi,
I am trying to add a custom rule to check if there are underscores in the URI. Here is my code. But it is not working. Any syntax mistake or the pattern?

rules:
oas2:
host_must_not_contain_underscore:
enabled: true
summary: URI should not contain underscores.
type: “style”
given: “$”
then:
field: host
function: pattern
functionOptions:
notMatch: “^[^_]+$”

Thanks in advance!

Pattern looks good to me, but what does the rest of your spectral.yml look like? Is that it?

Part of it is a bit different than how the docs describe here: https://github.com/stoplightio/spectral/blob/develop/docs/rulesets.md#adding-to-the-recommended-openapi-rules

Also, could you describe what is not working? Is it just not catching the errors? Or something else?

I am using this URL for guidance on custom rules:

I cloned the repo from git. It is empty. Now i added the above content only and checked in. I am able to see the rule i added in the list.

But, the warning is always true, even if there are no underscores in the URI.

Awesome! This is helpful. I thought you might be using Spectral outside of Stoplight and so it works a bit differently there than the link you shared.

Let me check with my teammate on what might be going on!

1 Like

Hi Taylor, Any response from your teammate?

We haven’t been able to track down the issue just yet. Will try to dig more into it today.

Hey @maneesh.moota!

Can you try it with this?

rules:
  oas2:
    host_must_not_contain_underscore:
      enabled: true
      summary: URI should not contain underscores.
      type: style
      then:
        field: host
        function: pattern
        functionOptions:
         notMatch: _

trying now with the new snippet.

Tried you code as-is (noticed there is no “given” in your code snippet). Tried with given and no given.

In your original rule, you named it saying that host must not contain an underscore, that looks like a path to me. Is that correct?

Edit: I see you want it not in the URI. I listed the field as host since that is what you had used in your example. So it sounds like you want to expand the rule.

1 Like

Thank you! You are right. That was my mistake.
Your code snippet is working when I added an _ in api host field. If I want to check _ anywhere in the URI itself, which field should I use?
Is there a lookup for all the fields I can refer to?

We don’t have great docs for this specific use case, most people are targeting a specific field, which doesn’t work here. If you don’t want any underscores anywhere, you can try to remove the field property and add:

given: $..*

This worked for me.
rules:
oas2:
host_must_not_contain_underscore:
enabled: true
summary: Host must not contain underscores
type: style
then:
field: host
function: pattern
functionOptions:
notMatch: _
basepath_must_not_contain_underscore:
enabled: true
summary: Base Path should not contain underscores
type: style
then:
field: basePath
function: pattern
functionOptions:
notMatch: _
path_name_must_not_contain_underscore:
enabled: true
summary: Path must not contain underscores
type: style
given: $.paths
then:
field: ‘@key
function: pattern
functionOptions:
notMatch: _