Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
asyncapi-validator
Advanced tools
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
/**
* 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 AsyncAPI v2 | 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 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 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 '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:
...
...
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'
}
]
}
FAQs
message validator through asyncapi schema
The npm package asyncapi-validator receives a total of 7,481 weekly downloads. As such, asyncapi-validator popularity was classified as popular.
We found that asyncapi-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.