Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Mar 13, 2020
1 parent 276ecfd commit 6429157
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"lint.fix": "yarn lint --fix",
"lint": "tsc --noEmit && tslint 'src/**/*.ts'",
"copy.html-templates": "copyfiles -u 1 \"./src/formatters/html/*.html\" \"./dist/\"",
"postbuild.functions": "copyfiles -u 1 \"dist/rulesets/{o,a}as/functions/*.js\" ./",
"postbuild.functions": "copyfiles -u 1 \"dist/rulesets/{oas,asyncapi}/functions/*.js\" ./",
"postbuild": "yarn build.functions && yarn generate-assets",
"prebuild": "yarn build.clean && copyfiles -u 1 \"src/rulesets/{o,a}as/**/*.json\" dist && copyfiles -u 1 \"src/rulesets/{o,a}as/**/*.json\" ./ && yarn copy.html-templates",
"prebuild": "yarn build.clean && copyfiles -u 1 \"src/rulesets/{oas,asyncapi}/**/*.json\" dist && copyfiles -u 1 \"src/rulesets/{oas,asyncapi}/**/*.json\" ./ && yarn copy.html-templates",
"prebuild.binary": "yarn build",
"pretest.karma": "node ./scripts/generate-karma-fixtures.js && yarn pretest",
"pretest": "yarn generate-assets",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BASE_PATH = process.cwd();

const functions = [];

const builtIns = ['oas', 'aas']
const builtIns = ['oas', 'asyncapi']

for (const rulesetName of builtIns) {
const targetDir = path.join(BASE_PATH, `dist/rulesets/${rulesetName}/functions/`);
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This script generates a list of assets that are needed to load spectral:oas and spectral:aas rulesets.
* This script generates a list of assets that are needed to load spectral:oas and spectral:asyncapi rulesets.
* It contains all custom functions and *resolved* rulesets.
* The assets are stored in a single file named `assets.json` in the following format:
* `<require-call-path>: <content>`
Expand Down Expand Up @@ -30,7 +30,7 @@ const assetsPath = path.join(baseDir, `assets.json`);
const generatedAssets = {};

(async () => {
for (const kind of ['oas', 'aas']) {
for (const kind of ['oas', 'asyncapi']) {
await processDirectory(generatedAssets, path.join(__dirname, `../rulesets/${kind}`));
await writeFileAsync(assetsPath, JSON.stringify(generatedAssets, null, 2));
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-karma-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (!fs.existsSync(baseDir)) {
fs.mkdirSync(baseDir);
}

for (const rulesetName of ['oas', 'aas']) {
for (const rulesetName of ['oas', 'asyncapi']) {
const target = path.join(baseDir, `${rulesetName}-functions.json`);
const fnsPath = path.join(__dirname, `../rulesets/${rulesetName}/functions`);
const bundledFns = {};
Expand Down
4 changes: 2 additions & 2 deletions setupKarma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FetchMockSandbox } from 'fetch-mock';

const oasRuleset = JSON.parse(JSON.stringify(require('./rulesets/oas/index.json')));
const oasFunctions = JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/oas-functions.json')));
const aasFunctions = JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/aas-functions.json')));
const asyncApiFunctions = JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/asyncapi-functions.json')));
const oas2Schema = JSON.parse(JSON.stringify(require('./rulesets/oas/schemas/schema.oas2.json')));
const oas3Schema = JSON.parse(JSON.stringify(require('./rulesets/oas/schemas/schema.oas3.json')));

Expand Down Expand Up @@ -31,7 +31,7 @@ beforeEach(() => {

[
['oas', oasFunctions],
['aas', aasFunctions],
['asyncapi', asyncApiFunctions],
].forEach(([rulesetName, funcs]) => {
for (const [name, fn] of Object.entries<string>(funcs)) {
fetchMock.get(`https://unpkg.com/@stoplight/spectral/rulesets/${rulesetName}/functions/${name}`, {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/generate-assets.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('generate-assets', () => {
const testCases = [
['oas', 'oas2-schema', 'title', 'A JSON Schema for Swagger 2.0 API.'],
['oas', 'oas3-schema', 'description', 'Validation schema for OpenAPI Specification 3.0.X.'],
['aas', 'asyncapi-schema', 'title', 'AsyncAPI 2.0.0 schema.'],
['asyncapi', 'asyncapi-schema', 'title', 'AsyncAPI 2.0.0 schema.'],
];

it.each(testCases)(
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/spectral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { RulesetExceptionCollection } from '../types/ruleset';
import { buildRulesetExceptionCollectionFrom } from '../../setupTests';

const oasRuleset = JSON.parse(JSON.stringify(require('../rulesets/oas/index.json')));
const aasRuleset = JSON.parse(JSON.stringify(require('../rulesets/aas/index.json')));
const asyncApiRuleset = JSON.parse(JSON.stringify(require('../rulesets/asyncapi/index.json')));
const oasRulesetRules: Dictionary<IRunRule, string> = oasRuleset.rules;
const aasRulesetRules: Dictionary<IRunRule, string> = aasRuleset.rules;
const asyncApiRulesetRules: Dictionary<IRunRule, string> = asyncApiRuleset.rules;

describe('spectral', () => {
describe('loadRuleset', () => {
test.each([
['spectral:oas', oasRulesetRules],
['spectral:aas', aasRulesetRules],
['spectral:asyncapi', asyncApiRulesetRules],
])('should support loading "%s" built-in ruleset', async (rulesetName, rules) => {
const s = new Spectral();
await s.loadRuleset(rulesetName);
Expand All @@ -44,7 +44,7 @@ describe('spectral', () => {

test.each([
['spectral:oas', oasRulesetRules],
['spectral:aas', aasRulesetRules],
['spectral:asyncapi', asyncApiRulesetRules],
])('should support loading multiple times the built-in ruleset "%s"', async (rulesetName, expectedRules) => {
const s = new Spectral();
await s.loadRuleset([rulesetName, rulesetName]);
Expand Down
2 changes: 1 addition & 1 deletion src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function resolveSpectralRuleset(ruleset: string) {

export const RESOLVE_ALIASES: Dictionary<string, string> = {
'spectral:oas': resolveSpectralRuleset('oas'),
'spectral:aas': resolveSpectralRuleset('aas'),
'spectral:asyncapi': resolveSpectralRuleset('asyncapi'),
};

export const STATIC_ASSETS: Dictionary<string> = {};
2 changes: 1 addition & 1 deletion src/cli/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getResolver } from './utils/getResolver';
const KNOWN_FORMATS: Array<[string, FormatLookup, string]> = [
['oas2', isOpenApiv2, 'OpenAPI 2.0 (Swagger) detected'],
['oas3', isOpenApiv3, 'OpenAPI 3.x detected'],
['aas2', isAsyncApiv2, 'AsyncAPI 2.x detected'],
['asyncapi2', isAsyncApiv2, 'AsyncAPI 2.x detected'],
['json-schema', isJSONSchema, 'JSON Schema detected'],
['json-schema-loose', isJSONSchemaLoose, 'JSON Schema (loose) detected'],
['json-schema-draft4', isJSONSchemaDraft4, 'JSON Schema Draft 4 detected'],
Expand Down
2 changes: 1 addition & 1 deletion src/cli/services/linter/utils/getRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export async function getRuleset(rulesetFile: Optional<string[]>) {

return await (rulesetFiles
? loadRulesets(process.cwd(), Array.isArray(rulesetFiles) ? rulesetFiles : [rulesetFiles])
: readRuleset(['spectral:oas', 'spectral:aas'])); // TODO: Rule name clashes
: readRuleset(['spectral:oas', 'spectral:asyncapi']));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe('streetlights', () => {
const streetlights = path.join(__dirname, './__fixtures__/streetlights.yaml');

const s = new Spectral({ resolver: httpAndFileResolver });
s.registerFormat('aas2', isAsyncApiv2);
s.registerFormat('asyncapi2', isAsyncApiv2);

test('can be linted', async () => {
await s.loadRuleset('spectral:aas');
await s.loadRuleset('spectral:asyncapi');

const document = new Document(await readParsable(streetlights, { encoding: 'utf8' }), Parsers.Yaml, streetlights);

Expand Down
42 changes: 21 additions & 21 deletions src/rulesets/aas/index.json → src/rulesets/asyncapi/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Contact object should have `name`, `url` and `email`.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"severity": 2,
"type": "style",
Expand All @@ -28,7 +28,7 @@
"description": "Example should have either a `value` or `externalValue` field.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$..example",
Expand All @@ -46,7 +46,7 @@
"description": "Info object should contain `contact` object.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$",
Expand All @@ -59,7 +59,7 @@
"description": "AsyncAPI object info `description` must be present and non-empty string.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$",
Expand All @@ -75,7 +75,7 @@
"description": "AsyncAPI object info `license` must be present and non-empty string.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$",
Expand All @@ -91,7 +91,7 @@
"description": "License object should include `url`.",
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$",
Expand All @@ -104,7 +104,7 @@
"description": "AsyncAPI object should have alphabetical `tags`.",
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$",
Expand All @@ -120,7 +120,7 @@
"description": "Operation `description` must be present and non-empty string.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.paths.*[?( @property === 'get' || @property === 'put' || @property === 'post' || @property === 'delete' || @property === 'options' || @property === 'head' || @property === 'patch' || @property === 'trace' )]",
Expand All @@ -136,7 +136,7 @@
"description": "Operation should have an `operationId`.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.paths.*[?( @property === 'get' || @property === 'put' || @property === 'post' || @property === 'delete' || @property === 'options' || @property === 'head' || @property === 'patch' || @property === 'trace' )]",
Expand All @@ -152,7 +152,7 @@
"description": "Path parameter declarations cannot be empty, ex.`/companies/{}` is invalid.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.paths",
Expand All @@ -171,7 +171,7 @@
"description": "paths should not end with a slash.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.paths",
Expand All @@ -190,7 +190,7 @@
"description": "given keys should not include a query string.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.paths",
Expand All @@ -209,7 +209,7 @@
"description": "Tag object should have a `description`.",
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.tags[*]",
Expand All @@ -222,7 +222,7 @@
"description": "Define at least one server.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"given": "$",
"then": {
Expand All @@ -240,7 +240,7 @@
"description": "Parameter objects should have a `description`.",
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"given": "$..['parameters']",
"then": {
Expand All @@ -255,7 +255,7 @@
"description": "Server URL should not point at example.com.",
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"given": "$.servers[*].url",
"then": {
Expand All @@ -269,7 +269,7 @@
"description": "Server URL should not have a trailing slash.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"given": "$.servers[*].url",
Expand All @@ -286,7 +286,7 @@
"severity": 0,
"recommended": false,
"formats": [
"aas2"
"asyncapi2"
],
"type": "validation",
"given": "$.components.schemas..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref))]",
Expand All @@ -304,15 +304,15 @@
"severity": 0,
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "validation",
"given": "$",
"then": {
"function": "schema",
"functionOptions": {
"schema": {
"$ref": "./schemas/schema.aas2.json"
"$ref": "./schemas/schema.asyncapi2.json"
}
}
}
Expand All @@ -321,7 +321,7 @@
"description": "Potentially unused components schema has been detected.",
"recommended": true,
"formats": [
"aas2"
"asyncapi2"
],
"type": "style",
"resolved": false,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ rules:
1
====stdout====
{document}
1:1 warning unrecognized-format The provided document does not match any of the registered formats [oas2, oas3, aas2, json-schema, json-schema-loose, json-schema-draft4, json-schema-draft6, json-schema-draft7, json-schema-2019-09]
1:1 warning unrecognized-format The provided document does not match any of the registered formats [oas2, oas3, asyncapi2, json-schema, json-schema-loose, json-schema-draft4, json-schema-draft6, json-schema-draft7, json-schema-2019-09]
6:15 error valid-user-properties must match the pattern '/^string$/'
10:15 error valid-user-properties must match the pattern '/^string$/'
11:11 error require-user-and-address `address` property is not truthy
Expand Down
2 changes: 1 addition & 1 deletion test-harness/scenarios/unrecognized-format.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ info: {}
{bin} lint {document}
====stdout====
{document}
1:1 warning unrecognized-format The provided document does not match any of the registered formats [oas2, oas3, aas2, json-schema, json-schema-loose, json-schema-draft4, json-schema-draft6, json-schema-draft7, json-schema-2019-09]
1:1 warning unrecognized-format The provided document does not match any of the registered formats [oas2, oas3, asyncapi2, json-schema, json-schema-loose, json-schema-draft4, json-schema-draft6, json-schema-draft7, json-schema-2019-09]

✖ 1 problem (0 errors, 1 warning, 0 infos, 0 hints)

0 comments on commit 6429157

Please sign in to comment.