Forum Discussion

Stoplight's avatar
Stoplight
Community Manager
5 months ago

Cannot use 'in' operator to search for 'then'

Originally posted by user dantuch to the Stoplight Community on 11/18/2022 at 04:54 ET.

hello, I've tried to write a rule that checks if given path, that contains no parameters, has a response code '404'...

404-response-for-path-with-no-param3:
    description: Path has no params - 404 is not a proper response.
    message: '{{description}}'
    severity: warn
    given: $.paths[?(!@property.match(/.*\/{.*}.*/))]..responses
    then:
      field: '404'
      function: falsy

something like this.

but this works somewhat wrong, and I don't know why. Normally if issue is not present I get a nice message of "No results with a severity of 'error' found!", but when using this rule there's no such message (like there's some runtime error or something - it all ends up silently.)
both falsy and undefined didn't work.... Although it depends... I have a dir with 4 files that are valid. If I introduce error to 4th of them - it acts as I said. If i do the same with first file, problem is being detected and warn is present!

So I tried a different approach - custom js function:

404-response-for-path-with-no-param4:
    description: Path has no params - 404 is not a proper response.
    message: '{{description}}'
    severity: warn
    given: $.paths[?(!@property.match(/.*\/{.*}.*/))]..responses
    then:
      field: '404'
      function: assert-failed
export default input => {

    if (typeof input !== "undefined") {
        console.log(input);

        return 'assertion failed';
    }

    return;
};

but then I got this error that I posted in title:

Error running Spectral!
Error #1: Cannot use 'in' operator to search for 'then' in assertion failed
          at lintNode              …nner/lintNode.js:36  if ('then' in targe…
          at cb                    …runner/runner.js:45  (0, lintNode_1.lint…
          at <computed>            …runner/runner.js:93  cb(scope);         

I seems I have more then 1 problem here - firstly why falsy/undefined do not work in a consistent way, and secondly what is wrong with my js-function or its usage.

version used: "name": "@stoplight/spectral-cli", "version": "6.5.1", ... also my issue seems similar to: https://github.com/stoplightio/spectral/issues/2341

some more input after further investigation - i think i have good examples of issues that should be detected by my rules, so this is how I run the rules:

spectral lint api/internal/apigw/*.*  --ruleset .spectral-local.yaml --verbose

currently spectral-local has mostly only:

  404-response-for-path-with-no-param2:
    description: Path has no params - 404 is not a proper response.
    message: '{{description}}'
    severity: warn
    given: $.paths[?(!@property.match(/.*\/{.*}.*/))]..responses.404
    then:
      function: falsy

... and funny part starts with testing of this rule - in api/internal/apigw/*.* I have 4 files. If I add edit the first one being lint'ed, then rule works properly! if I edit only file 2 and/or 3 and/or 4 ... rule does not work and ends up silently without summary.

1 Reply

  • Stoplight's avatar
    Stoplight
    Community Manager

    Skinghar (Staff) [11/28/2022 - 11:10 ET]

    Hello Dantuch, Thank you for sharing the details. We are aware of this issue and have it on our bug report. I do not currently have a timeline for a fix. Again, thank you for sharing the details so that we can pass that the information on to engineering.