### AsyncAPI Schema Example Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md An example AsyncAPI v3.0.0 schema defining channels, messages, and schemas, used for message validation. ```yaml asyncapi: 3.0.0 info: title: Streetlights Kafka API version: 1.0.0 channels: lightingMeasured: messages: lightMeasured: $ref: '#/components/messages/lightMeasured' operations: sendLightMeasurement: action: send channel: $ref: '#/channels/lightingMeasured' messages: - $ref: '#/channels/lightingMeasured/messages/lightMeasured' components: messages: lightMeasured: x-unique-id: lightMeasured payload: $ref: '#/components/schemas/lightMeasuredPayload' schemas: lightMeasuredPayload: type: object properties: lumens: type: integer minimum: 0 description: Light intensity measured in lumens. ``` -------------------------------- ### Example Usage: Validate Method Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Demonstrates how to use the `.validate()` method after loading an AsyncAPI schema from a YAML file. It shows setting the `msgIdentifier` and then validating a specific message. ```javascript const AsyncApiValidator = require('asyncapi-validator') let va = await AsyncApiValidator.fromSource('./api.yaml', {msgIdentifier: 'x-unique-id'}) // validate 'lightMeasured' on channel 'lightingMeasured' with operation 'send' va.validate('lightMeasured', { lumens: 0, sentAt: '2019-08-24T14:15:22Z' }, 'lightingMeasured', 'send') ``` -------------------------------- ### AsyncAPI Validation Error Structure Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Illustrates the structure of errors returned by the `asyncapi-validator` library. Errors include a name, key, and an array of detailed validation issues, often originating from AJV. ```javascript { AsyncAPIValidationError: data.type must 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: 'must be equal to one of the allowed values' } ] } ``` -------------------------------- ### Load AsyncAPI Schema from Source Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Loads and parses an AsyncAPI schema from a given source (local path, URL, or object). It accepts options for validation, such as specifying a message identifier or ignoring array validation. ```javascript /** * Load and Parse the schema from source. * @param {string | Object} source - local PATH or URL of schema or schema Object * @param {Object} options - options for validation * @returns {Promise} */ AsyncApiValidator.fromSource(source, options) ``` -------------------------------- ### Validate Message by ID with AsyncAPI Schema Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Demonstrates how to load an AsyncAPI schema from a file and validate a message payload against a specific message ID ('UserRemoved'). This requires the `asyncapi-validator` library and a valid AsyncAPI schema file. ```yaml asyncapi: 2.4.0 info: title: User Events version: 1.0.0 channels: user-events: description: user related events publish: message: messageId: UserRemoved payload: type: object properties: userEmail: type: string userId: type: string ``` ```javascript const AsyncApiValidator = require('asyncapi-validator') let va = await AsyncApiValidator.fromSource('./api.yaml') // validate messageId 'UserRemoved' va.validateByMessageId('UserRemoved', { userId: '123456789', userEmail: 'alex@mail.com', }) ``` -------------------------------- ### Access Resolved AsyncAPI Schema Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md The `.schema` property provides access to the AsyncAPI schema in JSON format, with all internal references resolved. ```javascript `.schema` property can be used to access AsyncApi schema in JSON format and with all the refs resolved. ``` -------------------------------- ### Validate Message Payload Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Validates a message payload against the AsyncAPI schema definition. Requires a message key (derived from `msgIdentifier`), the payload object, channel name, and operation type (publish/subscribe/send/receive). ```javascript /** * Method to validate the Payload against schema definition. * @param {string} key - required - message key (as per msgIdentifier) * @param {Object} payload - required - payload of the message * @param {string} channel - required - name of the channel/topic * @param {string} operation - required - publish | subscribe | send | receive * @returns {boolean} */ .validate(key, payload, channel, operation) ``` -------------------------------- ### Validate Message by Message ID (Deprecated) Source: https://github.com/waleedashraf/asyncapi-validator/blob/master/README.md Validates a message payload using a message ID. This method is deprecated due to the removal of `messageId` in AsyncAPI v3.0.0. It requires the AsyncAPI schema version to be between v2.4.0 and v3.0.0. ```javascript /** * Method to validate the Payload against schema definition. * @param {string} key - required - messageId * @param {Object} payload - required - payload of the message * @returns {boolean} */ .validateByMessageId(key, payload) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.