You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

asyncapi-validation

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asyncapi-validation

Message validation package for YAML and JSON AsyncAPI documents.

0.2.0
latest
Source
npm
Version published
Weekly downloads
951
-16.51%
Maintainers
1
Weekly downloads
 
Created
Source

AsyncAPI validation

Unit tests codecov CodeQL

Message validation package for YAML and JSON AsyncAPI documents.

This package:

  • Load and parse your AsyncAPI documents from a file, an url or an in-line schema
  • Support AsyncAPI documents v2.x.x and v3.x.x
  • Support both YAML and JSON documents

Installation

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

Usage

Parsing Functions

fromFile(path)Promise.<ValidationFunction>

Parses an AsyncAPI schema from a file and returns a validation function.

fromUrl(url)Promise.<ValidationFunction>

Parses an AsyncAPI schema from a URL and returns a validation function.

fromSchema(schema)Promise.<ValidationFunction>

Parses an AsyncAPI schema from a string and returns a validation function.

fromFile(path) ⇒ Promise.<ValidationFunction>

Parses an AsyncAPI schema from a file and returns a validation function.

Kind: global function Returns: Promise.<ValidationFunction> - A promise that resolves to the validation function. Throws:

  • AsyncAPIParsingError if the schema is not valid or has governance issues.
ParamTypeDescription
pathstringThe path to the AsyncAPI schema file.

Example

const validator = await asyncApiValidation.parseFromFile('path/to/schema.yaml');
validator('messageKey', { foo: 'bar' });

fromUrl(url) ⇒ Promise.<ValidationFunction>

Parses an AsyncAPI schema from a URL and returns a validation function.

Kind: global function Returns: Promise.<ValidationFunction> - A promise that resolves to the validation function. Throws:

  • AsyncAPIParsingError if the schema is not valid or has governance issues.
ParamTypeDescription
urlstringThe URL of the AsyncAPI schema.

Example

const validator = await asyncApiValidation.fromUrl(
  'https://example.org/schema.yaml'
);
validator('messageKey', { foo: 'bar' });

fromSchema(schema) ⇒ Promise.<ValidationFunction>

Parses an AsyncAPI schema from a string and returns a validation function.

Kind: global function Returns: Promise.<ValidationFunction> - A promise that resolves to the validation function. Throws:

  • AsyncAPIParsingError if the schema is not valid or has governance issues.
ParamTypeDescription
schemastringThe AsyncAPI schema as a string.

Example

const validator = await asyncApiValidation.fromSchema('asyncapi: 2.0.0');
validator('messageKey', { foo: 'bar' });

Validator Function

validate(schema) ⇒ ValidationFunction

Validates the provided AsyncAPI schema and returns a validation function.

Kind: global function Returns: ValidationFunction - The validation function.

ParamTypeDescription
schemaParseOutputThe parsed AsyncAPI schema.

validate~validatorFunction(key, payload) ⇒ boolean

Validates the provided payload against the AsyncAPI schema.

Kind: inner method of validate Returns: boolean - true if the payload is valid. Throws:

  • Error if no messages are found for the given key.
  • AsyncAPIValidationError if the payload fails validation.
ParamTypeDescription
keystringThe key of the message to validate.
payloadunknownThe payload to validate.

Example

const validator = await asyncApiValidation.fromSchema('asyncapi: 2.0.0');
validator('messageKey', { foo: 'bar' });

Errors

AsyncAPIParsingError

Represents an error that occurs during the parsing of an AsyncAPI document.

Kind: global class

new AsyncAPIParsingError(message, [errors])

Represents an error that occurs during the parsing of an AsyncAPI document.

ParamTypeDescription
messagestringThe error message.
[errors]Array.<Diagnostic>Optional array of diagnostic errors associated with the parsing error.

AsyncAPIValidationError

Represents an error that occurs during AsyncAPI validation.

Kind: global class

new AsyncAPIValidationError(message, key, [errors])

Represents an error that occurs during AsyncAPI validation.

ParamTypeDescription
messagestringThe error message.
keystringThe key associated with the error.
[errors]Array.<ErrorObject> | nullThe array of validation error objects.

Keywords

asyncapi

FAQs

Package last updated on 04 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts