AsyncAPI Parser

repository·master·Indexed 40 days ago

https://github.com/asyncapi/parser-js

A JavaScript/TypeScript monorepo for parsing and validating AsyncAPI documents. It provides the @asyncapi/parser core library, supports multiple AsyncAPI spec versions (2.0.0+), and integrates with Spectral for rule-based validation. The toolkit includes official schema parsers for OpenAPI 3.0.x/Swagger 2.x, Avro, and RAML, and supports usage in both Node.js and browser environments.

Tokens
32.9K
Snippets
108
Records
142
Agent score
49%

What's inside AsyncAPI Parser

  1. Overview of AsyncAPI v2 Core Ruleset

    master
    The AsyncAPI v2 Core Ruleset is a collection of validation rules designed to ensure the structural integrity of AsyncAPI 2.x.x specifications. These rules check for common errors such as undefined security schemes, mismatched server variables, duplicate identifiers, and schema inconsistencies. Note that these rules are specifically scoped to AsyncAPI version 2.x.x documents.
  2. Available AsyncAPI Spectral Rulesets

    master

    ParserJS includes built-in Spectral rulesets designed to validate AsyncAPI documents and enforce best practices. These rulesets are categorized by their scope (Core vs. Recommended) and the AsyncAPI version they target.

    Ruleset Types

    • Core Rulesets: Validate the fundamental structure of the AsyncAPI document.
    • Recommended Rulesets: Verify adherence to good practices and organizational standards within the document structure.

    Available Rulesets

    • Core Ruleset: Validates overall structure for all AsyncAPI versions (starting from 2.0.0).
    • Recommended Ruleset: Verifies good practices for all AsyncAPI versions (starting from 2.0.0).
    • v2 Core Ruleset: Specifically validates the structure of AsyncAPI version 2.x.x documents.
    • v2 Recommended Ruleset: Specifically verifies good practices for AsyncAPI version 2.x.x documents.
  3. AsyncAPI v2 Recommended Ruleset Overview

    master

    The AsyncAPI v2 Recommended Ruleset is a collection of Spectral rules designed to verify best practices within AsyncAPI version 2.x.x documents. These rules help ensure document quality, prevent common tooling errors (like URL concatenation issues), and improve compatibility with documentation and code-generation tools.

    Note: These rules only apply to documents where the asyncapi version is 2.x.x.

  4. What is @asyncapi/openapi-schema-parser?

    master

    The @asyncapi/openapi-schema-parser is an optional plugin for @asyncapi/parser. Its purpose is to allow the AsyncAPI parser to validate and convert message payloads written in OpenAPI 3.0 Schema Object syntax into the JSON Schema format used internally by the parser.

    Important Limitations:

    • It is not a full OpenAPI document parser (it does not handle paths, servers, etc.).
    • It only handles schema objects used in AsyncAPI message payloads (or similar slots) when the schemaFormat is set to an OpenAPI MIME type.
    • It currently supports OpenAPI 3.0 only (it does not support Swagger 2.x).
  5. Use npm workspaces for local package development

    master

    The project uses npm workspaces to manage dependencies between local packages. Instead of publishing a package to npm to test it in another package, npm workspaces creates symlinks in the root node_modules folder. This allows one package to import another directly from the local filesystem.

    To enable workspaces, the root package.json defines the workspace directories:

    {
      "workspaces": [
        "apps/*",
        "packages/*"
      ]
    }

    After running npm install at the root, you can verify that a package is symlinked by checking its location in node_modules. For example, @asyncapi/openapi-schema-parser will point to ../../packages/openapi-schema-parser.

    Note on Jest Configuration: Because dependencies are hoisted to the root node_modules, Jest configurations within individual packages may need to reference dependencies using relative paths from the package root to the root node_modules (e.g., <rootDir>/../../node_modules/...).

    ls -la node_modules/@asyncapi/openapi-schema-parser
    # → this is a symlink pointing to ../../packages/openapi-schema-parser
  6. Implement custom schema parsers

    master

    The parser can be extended to support non-standard schema formats (like Avro or OpenAPI) by registering a custom schema parser.

    A custom parser module must export an object with three functions:

    1. validate(input): Validates the syntax of the used schema.
    2. parse(input): Parses the schema into the AsyncAPI Schema Format.
    3. getMimeTypes(): Returns an array of MIME types that will be used as the schemaFormat property to identify the schema.

    To use it, call parser.registerSchemaParser(yourCustomParser) before calling parse() or validate().

    // 1. Define the custom parser
    export default {
      validate(input) { ... },
      parse(input) { ... },
      getMimeTypes() {
        return [
          'application/vnd.custom.type;version=1.0.0',
          'application/vnd.custom.type+json;version=1.0.0',
        ]
      }
    }
    
    // 2. Register it
    import { Parser } from "@asyncapi/parser";
    import myCustomSchemaParser from "./my-custom-schema-parser";
    
    const parser = new Parser();
    parser.registerSchemaParser(myCustomSchemaParser);
  7. Understand the relationship between @asyncapi/parser and @asyncapi/multi-parser

    master

    The parser-js repository provides two primary npm packages:

    1. @asyncapi/parser: The main, core parser.
    2. @asyncapi/multi-parser: A wrapper package that allows tools to receive parsed documents in specific 'Parser-API' versions (v1, v2, or v3).

    @asyncapi/multi-parser also acts as a bundler for all schema parsers. When called with includeSchemaParsers: true, it automatically registers Avro, OpenAPI, Protobuf, and RAML parsers.

  8. Understand @asyncapi/openapi-schema-parser package structure

    master

    The @asyncapi/openapi-schema-parser package is distributed with two builds to support different Node.js environments:

    • CommonJS (CJS): Located in cjs/, intended for use with Node.js require().
    • ES Modules (ESM): Located in esm/, intended for use with import. TypeScript type definitions (.d.ts) are provided via the ESM build.

    When installing via npm, only the esm/, cjs/, LICENSE, and README.md files are included in the package tarball.

  9. Understand the monorepo package structure

    master

    The parser-js repository is organized as a monorepo using npm workspaces. It contains several distinct packages, each with its own package.json.

    • Root package.json: Acts as the orchestrator. It is marked as "private": true and is not published to npm. It uses Turborepo to manage builds and tests across all workspace members.
    • @asyncapi/parser (packages/parser): The main parser package. It is published to npm.
    • @asyncapi/multi-parser (packages/multi-parser): Supports multiple Parser-API versions. It depends on the other workspace packages using the "*" version range, which tells npm to resolve the dependency to the local workspace member instead of fetching from the npm registry.
    • @asyncapi/openapi-schema-parser (packages/openapi-schema-parser): A specialized package for OpenAPI schemas, published to npm.

    To run tasks for a specific package using Turborepo from the root, use the --filter flag.

    # Example: Run tests only for the openapi-schema-parser package
    npm run openapi-parser:test
    
    # Under the hood, this uses Turborepo filtering:
    turbo run test --filter=@asyncapi/openapi-schema-parser
  10. Understand the parser-js monorepo structure

    master

    The parser-js repository is organized as a monorepo, meaning it contains multiple distinct, independently publishable packages within a single Git repository. This structure allows for shared tooling, unified CI/CD, and easier cross-package development.

    Key packages in the repository include:

    • @asyncapi/parser: The core library for reading and validating AsyncAPI documents.
    • @asyncapi/multi-parser: A package that consumes other parsers.
    • @asyncapi/openapi-schema-parser: A plugin for understanding OpenAPI schemas within AsyncAPI documents.

    Each package resides in the packages/ directory and maintains its own package.json and configuration.

    parser-js/                          ← the single Git repository
    ├── packages/
    │   ├── parser/                     ← @asyncapi/parser
    │   ├── multi-parser/               ← @asyncapi/multi-parser
    │   └── openapi-schema-parser/      ← @asyncapi/openapi-schema-parser
    ├── package.json                    ← root config (NOT published to npm)
    ├── turbo.json                      ← Turborepo pipeline config
    └── .github/workflows/              ← shared CI for all packages