Spectral rule not working as expected

I have this in my .spectral.yml file.

I’m trying to write a rule that shows an error if the operation id is not kebab cased.

Any ideas why this rule isn’t working? I have other rules in here that I left out of brevity and they are working just fine.

# Extends the default spectral rulesets
extends: ["spectral:oas2", "spectral:oas3"]
rules:
  # Enforce operation id's are kebab-cased
  kebab-case-operation-id:
    description: operationId should be kebab-cased.
    recommended: true
    severity: error
    type: style
    given: "$.operationId"
    then:
      function: casing
      functionOptions:
        type: kebab

I also tried this which didn’t work either:

# Extends the default spectral rulesets
extends: ["spectral:oas2", "spectral:oas3"]
rules:
  # Enforce operation id's are kebab-cased
  kebab-case-operation-id:
    description: operationId should be kebab-cased.
    recommended: true
    severity: error
    type: style
    given: "$"
    then:
      field: operation # also tried operationId here
      function: casing
      functionOptions:
        type: kebab

@tomcaflisch It doesn’t look like the Spectral version that includes the casing rule made it into the latest release. I have opened an issue with the team to expose the Spectral version in the UI somewhere (as well as display an error if the function is incorrect), but what you are trying to do here can be accomplished with a pattern rule as well:

extends:
  - 'spectral:oas2'
  - 'spectral:oas3'
rules:
  kebab-case-operation-id:
    description: operationId should be kebab-cased.
    recommended: true
    severity: error
    type: style
    given: '$..operationId'
    then:
      function: pattern
      functionOptions:
        match: '^[a-z]+(?:-[a-z]+)*$'
1 Like

That worked, although I modified the regex to include numbers '^[a-z]+(?:-[a-z0-9]+)*$' as I have an operation id’s for endpoints search-v1 and search-v2.