Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

asyncapi-validator

Package Overview
Dependencies
Maintainers
1
Versions
22
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

  • 2.4.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.6K
decreased by-22.72%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status codecov

asyncapi-validator

message validator through asyncapi schema

Note: This library works with v2 of AsyncAPI Schema. Support for v1 is deprecated and will be removed in next major version.

npm i asyncapi-validator

Features

  • Validate your AsyncApi Schema against AsyncApi Schema definition
  • Validate your messages against your AsyncApi Schema definition
  • Load your AsyncApi Schema from local file or any URL
  • Supports AsyncApi in JSON and YAML format
  • Supports all versions of AsyncAPI
  • more coming . . .

Methods

.fromSource()

/** 
 * Load and Parse the schema from source. You only need to do this once, and then just use .validate() method for validations.
 * @param {string} path - local path or URL of schema
 * @param {Object} options - options for validation
 * @returns {Promise}
 */
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.
msgIdentifierstringrequired with AsyncAPI v2Name of parameter whose value will be used as "key" in .validate() method. Normally it is "name" as described in message-object. You can also use Specification Extensions

.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 (optional with AsyncAPI v1)
 * @param {string} operation - required - publish | subscribe (optional with AsyncAPI v1)
 * @returns {boolean}
 */
validate(key, payload, channel, operation)

_Note: 'channel' and 'operation' can only be used with AsyncAPI v2. Both are required with AsyncAPI v2.

Example usage,

Example 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's why, "UserDeleted" has been use as "key" in "va.validate()"

Example usage with AsyncAPI V1 (deprecated)

const AsyncApiValidator = require('asyncapi-validator')
let va = await AsyncApiValidator.fromSource('./api.yaml')

// validate 'UserDeleted' key with payload
va.validate('UserDeleted', {
  userId: 'bd58d14f-fd3e-449c-b60c-a56548190d68',
  deletedBy: 'bd58d14f-fd3e-449c-b60c-a56548190d68',
  deletedAt: '2017-01-09T08:27:22.222Z',
})

// validate 'UserCreated' key with payload
va.validate('UserCreated', {1:1})

For these examples to work, key UserDeleted and UserCreated must be present in such way.

components:
  messages:
    UserDeleted:
              ...
              ...
    UserCreated:
              ...
              ...

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'
      }
    ]
}

Keywords

FAQs

Package last updated on 29 May 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc