Skip to content

Commit

Permalink
chore: add tests and tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieter Stinglhamber committed Dec 19, 2023
1 parent 8d0c2d9 commit bc3231c
Show file tree
Hide file tree
Showing 17 changed files with 1,185 additions and 232 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'monthly'
versioning-strategy: increase
ignore:
- dependency-name: '*'
update-types: ['version-update:semver-major', 'version-update:semver-patch']
25 changes: 25 additions & 0 deletions .github/workflows/check-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check types

on:
push:
branches:
- main
pull_request:

jobs:
check-types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 20.3
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm check:types
24 changes: 24 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint

on:
push:
branches:
- main
pull_request:

jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 20.3
cache: 'pnpm'
- run: pnpm lint
28 changes: 28 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Unit tests

on:
push:
branches:
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 20.3
cache: 'pnpm'
- run: pnpm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm lint-staged
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
src
tests
jest.config.js
tsconfig.json
.husky
.editorconfig
.github
.prettierrc
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# AsyncAPI validation

Message validation package for AsyncAPI documents.

This package is compatible with AsyncAPI documents v2.x.x and v3.0.0.

## Installation

```bash
# NPM
npm install asyncapi-validation
# Yarn
yarn add asyncapi-validation
# PNPM
pnpm install asyncapi-validation
```

## Usage

### Using a remote schema

```ts
import asyncAPIValidation from 'asyncapi-validation';

const validator = await asyncAPIValidation.fromUrl('https://example.org/schema.yaml');
validator('messageName', { foo: 'bar' });
validator('messageId', { foo: 'bar' });
```

# Using a local schema
```ts
const validator = await asyncAPIValidation.fromUrl('./schema.yaml');
validator('messageName', { foo: 'bar' });
validator('messageId', { foo: 'bar' });
```

# Using an in-line schema
```ts
const validator = await asyncAPIValidation.fromSchema(`asyncapi: 3.0.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
userSignedup:
address: user/signedup
messages:
UserSignedUp:
$ref: '#/components/messages/UserSignedUp'
operations:
sendUserSignedup:
action: send
channel:
$ref: '#/channels/userSignedup'
messages:
- $ref: '#/channels/userSignedup/messages/UserSignedUp'
components:
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
`)
validator('messageName', { foo: 'bar' });
validator('messageId', { foo: 'bar' });
```

## Contributing

### Tests

The test suite can be run using pnpm scripts:

```bash
pnpm test
```

Tests can be found in the (tests)[./tests] folder.

### Linting

The following pnpm script can be used to find out about the possible linting errors:

```bash
pnpm lint
```

### Linting error prevention

[Husky](https://github.com/typicode/husky) and [lint-staged](https://github.com/okonet/lint-staged) are set up to check staged files for linting issues. When new changes are commited all the updated files will be checked. If the linting fails for one of them the operation will be cancelled.

## Built with

- [NodeJs](https://nodejs.org/)
- [TypeScript](https://www.typescriptlang.org/)
- [AsyncAPI parser](https://www.asyncapi.com/tools/parsers)
- [Ajv js](https://ajv.js.org/)
27 changes: 22 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
{
"name": "asyncapi-validation",
"version": "1.0.0",
"description": "AsyncAPI message valition",
"version": "0.1.0",
"description": "AsyncAPI message validation",
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/Elhebert/asyncapi-validation.git"
},
"bugs": {
"url": "https://github.com/Elhebert/asyncapi-validation/issues"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest",
"lint": "prettier --check .",
"lint:fix": "prettier --write .",
"check:types": "tsc --noEmit",
"build": "tsc"
},
"keywords": [
"asyncapi",
Expand All @@ -15,10 +26,14 @@
"api",
"message"
],
"lint-staged": {
"*.{md,yaml,yml,ts}": [
"prettier --write"
]
},
"author": "Elhebert",
"license": "ISC",
"license": "MIT",
"dependencies": {
"@asyncapi/openapi-schema-parser": "^3.0.0",
"@asyncapi/parser": "^3.0.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1"
Expand All @@ -27,7 +42,9 @@
"@tsconfig/node20": "^20.1.2",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.5",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
Expand Down
Loading

0 comments on commit bc3231c

Please sign in to comment.