seneca-joi

Seneca plugin that provides joi message validation.

Usage no npm install needed!

<script type="module">
  import senecaJoi from 'https://cdn.skypack.dev/seneca-joi';
</script>

README

Seneca

A Seneca.js plugin that validates messages using the joi module.

seneca-joi

npm version Dependency Status Build Status Maintainability Coverage Status Gitter

Installation

npm install seneca-joi

And in your code:

require('seneca')({
  legacy: {validate: false} // needed if using Seneca 2.x
})
.use('seneca-joi', {
  joi: {allowUnknown: true}    // example of passing in Joi options
})

Usage

You can validate action messages by providing joi rules as part of the action definition.

var Joi = require('joi')

require('seneca')
    .use('seneca-joi')
    .add(
      {
        a: 1,
        b: Joi.required()
      },
      function (msg, done) {
        done(null, {c: msg.b})
      })
    .act('a:1,b:2') // valid
    .act('a:1') // invalid as no b value

Any properties in the action pattern that are not constants are interpreted as joi rules.

You can also modify or replace the Joi schema by providing a function via the joi$ property. This gives you full control of the Joi schema.

var Joi = require('joi')

require('seneca')
    .use('seneca-joi')
    .add(
      {
        a: 1,
        joi$: function (schema) {
          return schema.keys({b: Joi.required()})
        }
      },
      function (msg, done) {
        done(null, {c: msg.b})
      })
    .act('a:1,b:2') // valid
    .act('a:1') // invalid as no b value

Contributing

The Senecajs org encourages open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.

License

Licensed under MIT.