New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

asyncapi-validator

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asyncapi-validator

message validator through asyncapi schema

3.0.0
Source
npm
Version published
Weekly downloads
10K
-8.1%
Maintainers
1
Weekly downloads
 
Created
Source

Unit Tests codecov CodeQL

asyncapi-validator

message validator through asyncapi schema

_Note: This package only support AsyncAPI Schema v2.0.0 and above. Since v3.0.0, support for older versions of AsyncAPI Schema has been removed.

npm i asyncapi-validator

Features

  • Validate your AsyncApi Document against AsyncApi Schema definition
  • Validate your messages against your AsyncApi Document
  • Load your AsyncApi Schema from local file or any URL
  • Supports AsyncApi in JSON and YAML format
  • Supports AsyncAPI v2.0.0 and above
  • more coming . . .

Class Methods

AsyncApiValidator.fromSource()

/** 
 * Load and Parse the schema from source.
 * @param {string} path - local PATH or URL of schema
 * @param {Object} options - options for validation
 * @returns {Promise}
 */
AsyncApiValidator.fromSource(path, options)
Options
valuetypedescription
ignoreArraybooleanoptionalIf true, then if schema is defined as an array and payload is an object, then payload will be placed inside an array before validation.
msgIdentifierstringrequiredName of parameter whose value will be used as "key" in .validate() method. Recommendation is to use "name" as described in message-object. You can also use Specification Extensions

Instance Methods

.validate()

/**
 * Method to validate the Payload against schema definition.
 * @param {string} key - required - message key
 * @param {Object} payload - required - payload of the message
 * @param {string} channel - required - name of the channel/topic
 * @param {string} operation - required - publish | subscribe
 * @returns {boolean}
 */
.validate(key, payload, channel, operation)

.schema

.schema property can be used to access AsyncAPI schema in JSON format and with all the refs resolved.

Example usage

Schema

asyncapi: 2.0.0

info:
  title: User Events
  version: 1.0.0

channels:
  user-events:
    description: user related events
    publish:
      message:
        name: UserDeletedMessage
        x-custom-key: UserDeleted
        payload:
          type: object
          properties:
            userEmail:
              type: string
            userId:
              type: string
const AsyncApiValidator = require('asyncapi-validator')
let va = await AsyncApiValidator.fromSource('./api.yaml', {msgIdentifier: 'x-custom-key'})

// validate 'UserDeleted' on channel 'user-events' with operation 'publish'
va.validate('UserDeleted', {
  userId: '123456789',
  userEmail: 'alex@mail.com',
}, 'user-events', 'publish')

In above example, "msgIdentifier" is "x-custom-key". That is why, "UserDeleted" is used as "key" in "va.validate()" method.

Errors

Error thrown from asyncapi-validator will have these properties.

keytypevaluedescription
namestringAsyncAPIValidationErrorAsyncAPIValidationError
keystring"key" of payload against which schema is validated
messagestringerrorsText from AJV
errorsarrayArray of errors from AJV

Error Example

{
  AsyncAPIValidationError: data.type should be equal to one of the allowed values at MessageValidator.validate (.....
  name: 'AsyncAPIValidationError',
  key: 'hello',
  errors:
    [
      { keyword: 'enum',
        dataPath: '.type',
        schemaPath: '#/properties/type/enum',
        params: [Object],
        message: 'should be equal to one of the allowed values'
      }
    ]
}

How it works

asyncapi-validator validates the payload of the messages of a certain message, as described in your schema document. To validate against a certain message, it needs to find the message are you pointing to in schema document. For that, you need to pass it key, channel, and operation of the message.

validate(key, payload, channel, operation)
  • One channel should be defined only once in your whole schema document.
  • The key should be unique for an operation on a channel.

That means,

  • Messages going to different operations on one channel, can have same key.
  • Messages going to different channels, can have same key

Keywords

asyncapi

FAQs

Package last updated on 21 Mar 2021

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