@accordproject/concerto-linter
Version:
Concerto Linter using Spectral rulesets
31 lines (27 loc) • 1.39 kB
text/typescript
import * as fs from 'fs/promises';
import * as path from 'path';
import { ModelManager } from '@accordproject/concerto-core';
import { Json as JsonParsers } from '@stoplight/spectral-parsers';
import { Spectral, Document, IRuleResult, RulesetDefinition } from '@stoplight/spectral-core';
/**
* Tests the Concerto linter's default ruleset.
* @param {RulesetDefinition} [ruleset] - Optional ruleset to apply for testing.
* @param {string} modelFile - The model (CTO string) to be linted.
* @returns {Promise<IRuleResult[]>} A promise that resolves to an array of linting results.
* @throws {Error} If a critical processing failure occurs during linting.
*/
export async function testRules(ruleset: RulesetDefinition, modelFile: string): Promise<IRuleResult[]> {
try {
const filePath = path.resolve(__dirname, './fixtures/', modelFile);
const model = await fs.readFile(filePath, 'utf-8');
const manager = new ModelManager();
manager.addCTOModel(model);
const jsonAST = JSON.stringify(manager.getAst());
const spectral = new Spectral();
spectral.setRuleset(ruleset);
const document = new Document(jsonAST, JsonParsers);
return await spectral.run(document);
} catch (error) {
throw new Error(`Failed to test the default-ruleset: ${error instanceof Error ? error.message : String(error)}`);
}
}