-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
Expose Javascript function to load custom ruleset from file #1956
Comments
I did struggle with this earlier today, and here is my conclusion (frustration):
When I included the this file (getRuleset) it failed and the error was that it couldn't load node presets from here: https://github.com/stoplightio/spectral/tree/develop/packages/ruleset-bundler/src/presets. So even if you just include the package and include the file you'll see error, meaning you can't use it. The solution was that I implemented (almost copy-paste from here) my own solution in the codebase I work with. |
👋 |
Hi @P0lip we would like to keep the rules in yml files separated from microservice code, one of the reasons it's because we want to also load same rules in the vscode extension and/or from the command line (depending on users)
we have a Pull Request to your official vscode plugin to upgrade it to Spectral6 and loading rules form .spectral[.json|.yml|.js] is the last missing piece Regarding the vscode extension, we have made a very simple implementation/refactoring... it's under 250 loc + tests, and includes all functionality from current extension but upgraded to Spectral6 with a very simple build process We see that vscode extension has not been updated in a while. |
@ivangsa The function is unlikely to become a method of the Spectral class. It'd probably be in ruleset-bundler, and be consumed in the following way import { Spectral } from '@stoplight/spectral-core';
import { importLegacyRuleset } from '@stoplight/ruleset-bundler';
import * as fs from 'fs';
const s = new Spectral();
s.setRuleset(await importLegacyRuleset('.spectral.yaml'), { fs })); The actual implementation will be presumably slightly different. |
Hey, I've started working on it. |
@Andras-Csanyi @ivangsa // ...
import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
// ...
const rulesetFile = path.join(__dirname, ".spectral.yaml");
spectral.setRuleset(await bundleAndLoadRuleset(rulesetFile, { fs, fetch })); What do you folks think? |
@P0lip it looks perfect!! |
🎉 This issue has been resolved in version @stoplight/spectral-ruleset-bundler-v1.1.0 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version @stoplight/spectral-ruleset-migrator-v1.6.0 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
@ivangsa it's out, LMK how it goes and if there's anything I could help you with. |
Hi @P0lip, thanks for the update I'm not sure what I'm doing wrong but I can not import the module: I added to my package.json:
I see that the package is in node_modules, and that has that "export" in its package.json but it fails importing it (both vscode and webpack) And I can import other functions in ruleset-bundler package :-( |
Hey! Although having a peek at some of the most recent comments, it seems like we may need to add If that's indeed the case, perhaps you can try setting the path for now "compilerOptions": {
"baseUrl": ".",
"paths": {
"@stoplight/spectral-ruleset-bundler/with-loader": ["packages/ruleset-bundler/src/loader/node.ts"]
}
} while I'll address the issue with the lack of |
I was just trying with the whole path
let's see if it works |
|
@ivangsa do you have a repo somewhere I could try it out on? |
I'm also seeing:
When using CommonJS with Node v17.3.0 Amending it as follows seems to work: -const { bundleAndLoadRuleset } = require("@stoplight/spectral-ruleset-bundler/with-loader");
+const { bundleAndLoadRuleset } = require("@stoplight/spectral-ruleset-bundler/dist/loader/node"); |
Edit: Deleted message, as it was due to me passing the contents of the file to |
Hey folks! |
(no worries on this side... I'll be offline for a couple of weeks) |
hi @P0lip, any news on this? |
1 similar comment
hi @P0lip, any news on this? |
We are the same issue here with TypeScript: import { Spectral } from '@stoplight/spectral-core';
import { fetch } from '@stoplight/spectral-runtime';
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/dist/loader/node';
import * as fs from 'fs'
const spectral = new Spectral();
const rulesetFilepath = path.join(__dirname, ".spectral.yaml");
spectral.setRuleset(await bundleAndLoadRuleset(rulesetFilepath, { fs, fetch })); this throws then:
exporting the functions properly should help all TS users |
hi @P0lip, any news on this? |
hi @P0lip, are there any finding? I'm having the same issue. |
I am having a problem with require of the ruleset bundler. It only happens after the app is built for windows with electron builder. It then complains that it cannot find the file location of "with-loader". Almost like it cannot navigate to that path on windows in a packaged app. It works fine if I run the app, that uses the ruleset bundler, from console, ie start my electron app directly. It works fine after my app is build for Mac. Do you have any workaround? I was thinking to go around the "with-loader" and not require that directly but I can't figure out how to use the bundle function directly that is being called from "with-loader" exported function. |
Man, this hit me, too. This |
@tillig I solved my problem by building the app on the specific environments, here is my "guide" I wrote down: Manual build processMac x64 system can build Mac x64 and Windows x64 packaged application BackgroundBuilding the Windows packaged application on a Mac ARM system will result in @stoplight/spectral-ruleset-bundler/with-loader require error thrown on application start after install Not sure if it solves your issue, but maybe give it a try. I also found that building on GitHub on macos-latest, seems to be able to build fine for MacOS ARM, MacOS x64 and Win x64. Not sure how it works in the depth. But so far, fingers crossed, the builds seems to work also with the Spectral validations. |
My use case is that I have a set of Mocha tests (with ts-node) in Typescript that I'm using to test my custom ruleset. I'm not building an app, basically just running Mocha. I tried to update to the latest Spectral and hit this issue. |
I am also experiencing the exact same issue. My use case is similar to @tillig in that I want to load the file based ruleset to execute some tests against each rule. |
…er/with-loader' or its corresponding type declarations" error by specifying path to loader directly based on stoplightio/spectral#1956 (comment).
I was running into this The fix in my case was to add a {
"compilerOptions": {
"moduleResolution": "node16"
}
} After adding the |
Ran into this same issue - @br-tyler-milner - your suggestion worked for me. It makes sense in my case to modify the module resolution anyhow https://www.typescriptlang.org/tsconfig#moduleResolution |
User story.
As a developer consuming spectral-core from javascript, I can load custom ruselet from yml files, so that I can use them programatically on a microservice.
Is your feature request related to a problem?
I'm trying to use spectral-core 6 from javascript code and I'm not able to programatically load rulesets from yml files.
Loading files as yml files doesn't work as it doesn't resolve functions (like truthy, falsy...) and all I get are string properties...
Digging in spectral-core source code there is a function for reading files is included in spectral-cli but that function is not exported as a type.d.ts (or is it?).
https://github.com/stoplightio/spectral/blob/develop/packages/cli/src/services/linter/utils/getRuleset.ts#L36
Trying to import the function like this doesn't work:
(I guess I can not import from dist folder just like that)
Describe the solution you'd like
I would like spectral-core to expose
getRuleset
function for other applications to use it programaticallyor even update
Spectral.setRuleset
signature so it can receive rules as string urlsAdditional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: