mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-06 04:21:09 +00:00
* docs: updated docs for release, added license * docs: remove outdated test files * update README and rules descriptions * updated license * update config file name * update parameter names * Grammar fixes, clarifications, and minor restructure of the docs (#36) * change 'codeframes' param name * update gif * update image location * fix typo
53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
import yaml from 'js-yaml';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import loadRuleset from './loader';
|
|
import getConfig from './config';
|
|
import OpenAPIRoot from './types';
|
|
|
|
import traverseNode from './traverse';
|
|
|
|
function createContext(node, sourceFile, filePath, config) {
|
|
return {
|
|
document: node,
|
|
filePath: path.resolve(filePath),
|
|
path: [],
|
|
cache: {},
|
|
visited: [],
|
|
result: [],
|
|
definitionStack: [],
|
|
pathStack: [],
|
|
source: sourceFile,
|
|
enableCodeframe: !!(config && (config.codeframes === 'on' || config.codeframes === true)),
|
|
// customRules: config && config.enbaleCustomRuleset ? loadRuleset(config) : [],
|
|
customRules: loadRuleset(config),
|
|
config,
|
|
};
|
|
}
|
|
|
|
export const validate = (yamlData, filePath, options = {}) => {
|
|
let document;
|
|
try {
|
|
document = yaml.safeLoad(yamlData);
|
|
} catch (ex) {
|
|
throw new Error("Can't load yaml file");
|
|
}
|
|
if (!document.openapi) return [];
|
|
|
|
const config = getConfig(options);
|
|
// console.log(config);
|
|
const ctx = createContext(document, yamlData, filePath, config);
|
|
// ctx.result.push(ctx);
|
|
|
|
traverseNode(document, OpenAPIRoot, ctx);
|
|
return ctx.result;
|
|
};
|
|
|
|
export const validateFromFile = (fName, options) => {
|
|
const resolvedFileName = fName; // path.resolve(fName);
|
|
const doc = fs.readFileSync(resolvedFileName, 'utf-8');
|
|
const validationResult = validate(doc, resolvedFileName, options);
|
|
return validationResult;
|
|
};
|