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

Getting an AggregateError when using JSON custom ruleset #2242

Closed
imsuneth opened this issue Aug 15, 2022 · 6 comments
Closed

Getting an AggregateError when using JSON custom ruleset #2242

imsuneth opened this issue Aug 15, 2022 · 6 comments

Comments

@imsuneth
Copy link

I have a configuration file that includes the custom rules among other configurations. I retrieve the custom rules object (I have defined it as a const below) from the file and set it and run the linter.

const apiDefinition
const customRules = {
  "rules": {
    "empty-title-property": {
      "message": "Title must not be empty",
      "given": "$..title",
      "then": {
        "function": "truthy"
      }
    }
  }
};
const spectral = new Spectral();
spectral.setRuleset(customRules);
spectral.run(apiDefinition).then((result)=> {
  console.log(result);
}

However, I am getting this AggregateError and not sure what I am doing wrong. Could you please help?

AggregateError: Error running Nimma
    at Scope.destroy (scope.js:137:1)
    at Nimma.eval (eval at query (index.js:66:1), <anonymous>:17:11)
    at Nimma.query (index.js:68:1)
    at execute (runner.js:90:1)
    at Runner.run (runner.js:53:1)
    at Spectral.runWithResolved (spectral.js:60:1)
    at async Spectral.run (spectral.js:68:1)

Environment (remove any that are not applicable):

  • Library version: 1.12.2
  • OS: macOs monetrey 12.4
@LukeMccon
Copy link

I've run into this before as well, try logging the .errors property of the aggregate error you're receiving if you haven't already. That may give you some extra context

@imsuneth
Copy link
Author

Thanks @LukeMccon. Here is what i found in .errors

Error: $ threw: ErrorWithCause("Function "undefined" threw an exception: then.function is not a function")
at _callbacks. (proxy-callbacks.js:34:1)
at Scope.emit (scope.js:109:1)
at Nimma.eval (eval at query (index.js:66:1), :7:11)
at Nimma.query (index.js:68:1)
at execute (runner.js:90:1)
at Runner.run (runner.js:53:1)
at Spectral.runWithResolved (spectral.js:60:1)
at async Spectral.run (spectral.js:68:1)

TypeError: then.function is not a function
at lintNode (lintNode.js:24:1)
at cb (runner.js:45:1)
at mapped. (runner.js:93:1)
at _callbacks. (proxy-callbacks.js:31:1)
at Scope.emit (scope.js:109:1)
at Nimma.eval (eval at query (index.js:66:1), :7:11)
at Nimma.query (index.js:68:1)
at execute (runner.js:90:1)
at Runner.run (runner.js:53:1)
at Spectral.runWithResolved (spectral.js:60:1)

@P0lip
Copy link
Contributor

P0lip commented Aug 15, 2022

You cannot load a JSON ruleset this way. Spectral#setRuleset accepts only JS rulesets.

const { truthy } = require('@stoplight/spectral-functions');

const apiDefinition
const customRules = {
  "rules": {
    "empty-title-property": {
      "message": "Title must not be empty",
      "given": "$..title",
      "then": {
        "function": truthy
      }
    }
  }
};
const spectral = new Spectral();
spectral.setRuleset(customRules);
spectral.run(apiDefinition).then((result)=> {
  console.log(result);
}

This is how a correct code would look like.
Feel free to reach out in case you have some questions.

@P0lip P0lip closed this as completed Aug 15, 2022
@imsuneth
Copy link
Author

Thanks a lot @P0lip for the clarification. Is there any way right now to parse a JSON ruleset to a JS ruleset?

@LukeMccon
Copy link

@imsuneth look at the ruleset-migrator package. There should be instructions on that page, I was able to use it to move from JSON to JS rulesets.

@imsuneth
Copy link
Author

@LukeMccon thanks a lot and i'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants