Skip to content

Commit

Permalink
fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Sep 9, 2022
1 parent e43a778 commit a23a493
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"release": "semantic-release"
},
"dependencies": {
"@asyncapi/specs": "^3.1.0",
"@asyncapi/specs": "file:../asyncapi-node/asyncapi-specs-3.1.0.tgz",
"@openapi-contrib/openapi-schema-to-json-schema": "^3.2.0",
"@stoplight/spectral-core": "^1.14.1",
"@stoplight/spectral-functions": "^1.7.1",
Expand Down Expand Up @@ -75,9 +75,9 @@
"path-browserify": "^1.0.1",
"puppeteer": "^17.1.1",
"semantic-release": "17.4.3",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"typescript": "^4.8.2",
"ts-loader": "^9.3.1",
"webpack": "^5.74.0",
"webpack-bundle-analyzer": "^4.6.1",
"webpack-cli": "^4.10.0"
Expand Down
6 changes: 4 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import specs from '@asyncapi/specs/supported';

export const xParserSpecParsed = 'x-parser-spec-parsed';
export const xParserSpecStringified = 'x-parser-spec-stringified';

Expand All @@ -13,5 +15,5 @@ export const xParserCircularProps = 'x-parser-circular-props';

export const EXTENSION_REGEX = /^x-[\w\d.\-_]+$/;

// Only >=2.0.0 versions are supported
export const specVersions = ['2.0.0', '2.1.0', '2.2.0', '2.3.0', '2.4.0'];
export const specVersions = Object.keys(specs);
export const lastVersion = specVersions[specVersions.length - 1];
35 changes: 6 additions & 29 deletions src/schema-parser/asyncapi-schema-parser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import Ajv from 'ajv';
import specs from '@asyncapi/specs/supported';

import { specVersions } from '../constants';

import type { ErrorObject, ValidateFunction } from 'ajv';
import type { JSONSchema7 } from 'json-schema';
import type { AsyncAPISchema, SchemaValidateResult } from '../types';
import type { SchemaParser, ParseSchemaInput, ValidateSchemaInput } from '../schema-parser';
import type { v2 } from '../spec-types';

// import only 2.X.X AsyncAPI JSON Schemas for better treeshaking
import * as asyncAPI2_0_0Schema from '@asyncapi/specs/schemas/2.0.0.json';
import * as asyncAPI2_1_0Schema from '@asyncapi/specs/schemas/2.1.0.json';
import * as asyncAPI2_2_0Schema from '@asyncapi/specs/schemas/2.2.0.json';
import * as asyncAPI2_3_0Schema from '@asyncapi/specs/schemas/2.3.0.json';
import * as asyncAPI2_4_0Schema from '@asyncapi/specs/schemas/2.4.0.json';

const ajv = new Ajv({
allErrors: true,
Expand All @@ -29,7 +23,7 @@ export function AsyncAPISchemaParser(): SchemaParser {
}

async function validate(input: ValidateSchemaInput<unknown, unknown>): Promise<SchemaValidateResult[]> {
const version = input.asyncapi.semver.version;
const version = input.asyncapi.semver.version as keyof typeof specs;
const validator = getSchemaValidator(version);

let result: SchemaValidateResult[] = [];
Expand Down Expand Up @@ -71,10 +65,10 @@ function ajvToSpectralResult(errors: ErrorObject[]): SchemaValidateResult[] {
});
}

function getSchemaValidator(version: string): ValidateFunction {
function getSchemaValidator(version: keyof typeof specs): ValidateFunction {
let validator = ajv.getSchema(version);
if (!validator) {
const schema = preparePayloadSchema(getSchema(version), version);
const schema = preparePayloadSchema(specs[version], version);

ajv.addSchema(schema, version);
validator = ajv.getSchema(version);
Expand All @@ -87,7 +81,7 @@ function getSchemaValidator(version: string): ValidateFunction {
* To validate the schema of the payload we just need a small portion of official AsyncAPI spec JSON Schema, the Schema Object in particular. The definition of Schema Object must be
* included in the returned JSON Schema.
*/
function preparePayloadSchema(asyncapiSchema: v2.AsyncAPISchemaDefinition, version: string): v2.AsyncAPISchemaDefinition {
function preparePayloadSchema(asyncapiSchema: JSONSchema7, version: string): JSONSchema7 {
const payloadSchema = `http://asyncapi.com/definitions/${version}/schema.json`;
const definitions = asyncapiSchema.definitions;
if (definitions === undefined) {
Expand All @@ -103,20 +97,3 @@ function preparePayloadSchema(asyncapiSchema: v2.AsyncAPISchemaDefinition, versi
definitions
};
}

function getSchema(version: string): Record<string, unknown> {
switch (version) {
case '2.0.0':
return asyncAPI2_0_0Schema;
case '2.1.0':
return asyncAPI2_1_0Schema;
case '2.2.0':
return asyncAPI2_2_0Schema;
case '2.3.0':
return asyncAPI2_3_0Schema;
case '2.4.0':
return asyncAPI2_4_0Schema;
default:
throw new Error(`Specification with "${version}" version does not exist.`);
}
}
2 changes: 1 addition & 1 deletion test/schema-parser/spectral-rule-v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SchemaValidateResult } from '../../src/types';
describe('aas2schemaParserRule', function() {
const parser = new Parser();

it('should validate AsyncAPI Schema with valid schema', async function() {
it.only('should validate AsyncAPI Schema with valid schema', async function() {
const document = {
asyncapi: '2.0.0',
info: {
Expand Down

0 comments on commit a23a493

Please sign in to comment.