Skip to content

Commit

Permalink
feat: add support for openapi schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieter Stinglhamber committed Jan 4, 2024
1 parent 821138e commit 1d8b4a0
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"author": "Elhebert",
"license": "MIT",
"dependencies": {
"@asyncapi/openapi-schema-parser": "^3.0.10",
"@asyncapi/parser": "^3.0.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1"
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { type Input, fromFile, fromURL, Parser } from '@asyncapi/parser';
import AsyncAPIParsingError from './AsyncAPIParsingError';
import validate, { type ValidationFunction } from './validate';
import openAPISchemaParser from '@asyncapi/openapi-schema-parser';

const parser = new Parser({ ruleset: { core: true, recommended: false } });
const parser = new Parser({
ruleset: { core: true, recommended: false },
schemaParsers: [openAPISchemaParser()],
});

export default {
/**
Expand Down
44 changes: 44 additions & 0 deletions tests/fixtures/valid-schema-openapi-2.4.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
asyncapi: 2.4.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
publish:
message:
$ref: '#/components/messages/testMessage'

components:
messages:
testMessage:
schemaFormat: application/vnd.oai.openapi;version=3.0.0
payload:
type: object
nullable: true
example:
name: Fran
properties:
name:
type: string
discriminatorTest:
discriminator:
propertyName: objectType
oneOf:
- type: object
properties:
objectType:
type: string
prop1:
type: string
- type: object
properties:
objectType:
type: string
prop2:
type: string
test:
type: object
properties:
testing:
type: string
51 changes: 51 additions & 0 deletions tests/fixtures/valid-schema-openapi-3.0.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
asyncapi: 3.0.0
info:
title: My API
version: 1.0.0
channels:
myChannel:
address: myChannel
messages:
publish.message:
$ref: '#/components/messages/testMessage'
operations:
myChannel.publish:
action: receive
channel:
$ref: '#/channels/myChannel'
messages:
- $ref: '#/channels/myChannel/messages/publish.message'
components:
messages:
testMessage:
payload:
schemaFormat: application/vnd.oai.openapi;version=3.0.0
schema:
type: object
nullable: true
example:
name: Fran
properties:
name:
type: string
discriminatorTest:
discriminator:
propertyName: objectType
oneOf:
- type: object
properties:
objectType:
type: string
prop1:
type: string
- type: object
properties:
objectType:
type: string
prop2:
type: string
test:
type: object
properties:
testing:
type: string
24 changes: 24 additions & 0 deletions tests/from-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,28 @@ describe('parsing `fromFile`', () => {
);
}
});

it('validate an openapi schema inside a valid Async API 3.0.0 schema', async () => {
const validator = await asyncApiValidation.fromFile(
path.join(__dirname, 'fixtures', 'valid-schema-openapi-3.0.0.yaml')
);

const payload = {
name: 'Fran',
};

expect(validator('testMessage', payload)).toBe(true);
});

it('validate an openapi schema inside a valid Async API 2.4.0 schema', async () => {
const validator = await asyncApiValidation.fromFile(
path.join(__dirname, 'fixtures', 'valid-schema-openapi-2.4.0.yaml')
);

const payload = {
name: 'Fran',
};

expect(validator('testMessage', payload)).toBe(true);
});
});
28 changes: 28 additions & 0 deletions tests/from-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,32 @@ describe('parsing `fromSchema`', () => {
);
}
});

it('validate an openapi schema inside a valid Async API 3.0.0 schema', async () => {
const schema = await readFile(
path.join(__dirname, 'fixtures', 'valid-schema-openapi-3.0.0.yaml'),
'utf-8'
);
const validator = await asyncApiValidation.fromSchema(schema);

const payload = {
name: 'Fran',
};

expect(validator('testMessage', payload)).toBe(true);
});

it('validate an openapi schema inside a valid Async API 2.4.0 schema', async () => {
const schema = await readFile(
path.join(__dirname, 'fixtures', 'valid-schema-openapi-2.4.0.yaml'),
'utf-8'
);
const validator = await asyncApiValidation.fromSchema(schema);

const payload = {
name: 'Fran',
};

expect(validator('testMessage', payload)).toBe(true);
});
});

0 comments on commit 1d8b4a0

Please sign in to comment.