message validator through asyncapi schema
npm i asyncapi-validator
- Validate your AsyncApi Schema against AsyncApi Schema definition
- Validate your messages against your AsyncApi Schema definition
- Use 'bundle.min.js' to run validator in browser
- Load your AsyncApi Schema from local file or any URL
- Supports AsyncApi in JSON and YAML format
- Supports all versions of AsyncAPI
- more coming . . .
/**
* 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)
value | type | description | |
---|---|---|---|
ignoreArray | boolean | optional | If true, then if schema is defined as an array and payload is an object, then payload will be placed inside an array before validation. |
msgIdentifier | string | required with channel validation | Name 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 |
/**
* Method to validate the Payload against schema defination.
* @param {String} key - message key
* @param {Object} payload - payload of the message
* @param {string} channel - name of the channel/topic
* @param {string} operation - publish | subscribe (required with channel validation)
* @returns {boolean}
*/
validate(key, payload, channel, operation)
Note: 'channel' validation with 'msgIdentifier' and 'operation' is only supported with AsyncAPI Version >= 2.0.0
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()"
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 'Key' key with payload
va.validate('Key', {1:1})
For this to work, key UserDeleted
must be present in such way.
components:
messages:
UserDeleted:
Error thrown from asyncapi-validator will have these properties.
key | type | value | description |
---|---|---|---|
name | string | AsyncAPIValidationError | AsyncAPIValidationError |
key | string | "key" of payload against which schema is validated | |
message | string | errorsText from AJV | |
errors | array | Array of errors from AJV |
{
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'
}
]
}