This is a plugin to use Ajv (JSON-Schema) for request validation.
npm install hemera-ajv

This is a plugin to use Ajv (JSON-Schema) for request/response validation.
``js`
const hemera = new Hemera(nats)
hemera.use(require('hemera-ajv'))
The primary purpose of ajv is to validate the incoming request.
`js`
hemera.add(
{
topic: 'math',
cmd: 'add',
properties: {
a: { type: 'number' },
b: { type: 'number' }
}
},
(req, cb) => {
cb(null, req.a + req.b)
}
)
You can also validate your response payload by using the schema property where you can define request and response schemas. Response error isn't validated but must be from type Error.
`js`
hemera.add(
{
topic: 'math',
cmd: 'add',
schema: {
response: {
type: 'number'
}
}
},
(req, cb) => {
cb(null, req.a + req.b)
}
)
You can reuse schemas across server actions. This functionality is provided by a custom schema store.
If you reference a schema with schema# it will be replaced by the JSON schema at startup time.
`js`
hemera.addSchema({
$id: 'myRequestSchema',
type: 'object',
properties: {
a: { type: 'number' },
b: { type: 'number' }
}
})
hemera.addSchema({
$id: 'myResponseSchema',
type: 'number'
})
hemera.add(
{
topic: 'math',
cmd: 'add',
schema: {
request: 'myRequestSchema#',
response: 'myResponseSchema#'
}
},
(resp, cb) => {
cb()
}
)
`js``
const hemera = new Hemera(nats)
hemera.use(
require('hemera-ajv', {
ajv: {
coerceTypes: true,
useDefaults: true,
removeAdditional: true
}
})
)
- .addSchema(Object schema)