-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMessageValidator.js
104 lines (87 loc) · 3.62 KB
/
MessageValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Ajv = require('ajv')
const addFormats = require('ajv-formats')
const ValidationError = require('./ValidationError')
class MessageValidator {
/**
* @param {object} schema - user defined AsyncApi Schema
* @param {{ msgIdentifier?: string; ignoreArray?: boolean; path?: any; }} options - options for validations
* @param {object} channels - simplified channels object from schema
*/
constructor(schema, options, channels, messagesWithId) {
this.schema = schema
this._messages = this.schema.components ? this.schema.components.messages : []
this._messagesWithId = messagesWithId
this._channels = channels
this._ajv = new Ajv({allErrors: true, strict: false, unicodeRegExp: false})
addFormats(this._ajv)
this._options = options
}
/**
* @param {string} key
* @param {Object} payload
* @param {Object} channel
* @param {Object} operation
* @returns {boolean}
*/
validate(key, payload, channel, operation) {
this._validateArgs(key, channel, operation)
const payloadSchema = this._channels[channel][operation][key].payload
payload = this._shouldHandleArray(payloadSchema, payload) ? [payload] : payload
const validator = this._ajv.compile(payloadSchema)
const result = validator(payload)
if (!result) throw new ValidationError(this._ajv.errorsText(validator.errors), key, validator.errors)
return true
}
// deprecated
validateByMessageId(key, payload) {
this._validateArgs(key, null, null, 'validateByMessageId')
const payloadSchema = this._messagesWithId[key].payload
payload = this._shouldHandleArray(payloadSchema, payload) ? [payload] : payload
const validator = this._ajv.compile(payloadSchema)
const result = validator(payload)
if (!result) throw new ValidationError(this._ajv.errorsText(validator.errors), key, validator.errors)
return true
}
/**
* @param {string} key
* @param {string | number | null} channel
* @param {string | null} operation
* @param {string | null} method
*/
_validateArgs(key, channel, operation, method = null) {
if (method === 'validateByMessageId') {
const [major, minor] = this.schema.asyncapi.split('.')
if (parseInt(major) < 2 || (parseInt(major) === 2 && parseInt(minor) < 4)) {
throw new ValidationError(`AsyncAPI schema version should be >= 2.4.0 and <3.0.0 . Your version is "${this.schema.asyncapi}"`)
}
if (!this._messagesWithId[key]) {
throw new ValidationError(`message with messageId "${key}" not found`)
}
} else {
if (!this._options.msgIdentifier) {
throw new ValidationError('"msgIdentifier" is required')
}
if (!operation || !(operation === 'publish' || operation === 'subscribe' || operation === 'send' || operation === 'receive')) {
throw new ValidationError(`operation "${operation}" is not valid`)
}
if (channel !== null && !this._channels[channel]) {
throw new ValidationError(`channel "${channel}" not found`)
}
if (channel !== null && !this._channels[channel][operation][key]) {
throw new ValidationError(`message with key "${key}" on channel "${channel}" and operation "${operation}" not found`)
}
}
}
/**
* @param {{ type: string; }} payloadSchema
* @param {any} payload
*/
_shouldHandleArray(payloadSchema, payload) {
const shouldIgnoreArray = this._options.ignoreArray === true
const schemaIsArray = payloadSchema.type === 'array'
const payloadIsNotArray = !Array.isArray(payload)
if (shouldIgnoreArray && schemaIsArray && payloadIsNotArray) return true
return false
}
}
module.exports = MessageValidator