Repository navigation
|
How we can use it non in CLI but in code? I want to "pack" the validation/linting in a javascript function and be able to call it. I expect something like this pseudo code import {validate} from "@stoplightio/???"
import firstRuleset from "@stoplightio/XXX"
function validateMyDocument(document) {
const result = validate(
document,
{
ruleset: [
firstRuleset
],
}
);
console.log(result);
} |
Answered by
tresorama
Jan 19, 2026
Replies: 2 comments
|
Replying to myself... This validate an OpenAPI spec with ruleset NOTE: I tried using ruleset from # install deps
pnpm add @stoplight/spectral-core @stoplight/spectral-rulesets
# this combo worked
@stoplight/spectral-core 1.20.0
@stoplight/spectral-rulesets 1.22.0import Spectral from "@stoplight/spectral-core";
import { oas } from "@stoplight/spectral-rulesets";
type SpecJson = Record<string, any>;
type ValidateSpecOptions = {
specJson: SpecJson,
};
export async function validateWithSpectral(options: ValidateSpecOptions) {
try {
const spectral = new Spectral.Spectral();
spectral.setRuleset(new Spectral.Ruleset(oas));
const result = await spectral.run(options.specJson);
return {
status: 'success',
data: result
};
} catch (error) {
return {
status: 'error',
data: {
code: 'SPEC_VALIDATION_ERROR',
errorObject: error,
}
};
}
} |
0 replies
Answer selected by
tresorama
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replying to myself...
This validate an OpenAPI spec with ruleset
oasfrom@stoplight/spectral-rulesets.NOTE: I tried using ruleset from
@stoplight/spectral-documentationbut my bundler (vite) cannot import it.