6.0.2 • Published 4 years ago

hemera-joi v6.0.2

Weekly downloads
701
License
MIT
Repository
github
Last release
4 years ago

Hemera-joi package

npm styled with prettier

This is a plugin to use Joi for request/response validation.

Usage

const hemera = new Hemera(nats)
hemera.use(require('hemera-joi'))

Request validation

The primary purpose of joi is to validate the incoming request. You can define your validation schema with the joi$ property or inline.

let Joi = hemera.joi

// inline
hemera.add(
  {
    topic: 'math',
    cmd: 'add',
    a: Joi.number().required()
  },
  (req, cb) => {
    cb(null, req.a + req.b)
  }
)

// with `joi$` property
hemera.add(
  {
    topic: 'math',
    cmd: 'add',
    joi$: Joi.object().keys({ a: Joi.number().required() })
  },
  (req, cb) => {
    cb(null, req.a + req.b)
  }
)

Response validation

You can validate the response payload as well if you use the postJoi$ property. Response error isn't validated but must be from type Error.

Missing fields

If a field is present in the schema (and is not required) but it is not present in the object to validate, joi will not write it in the final payload.

let Joi = hemera.joi

hemera.add(
  {
    topic: 'math',
    cmd: 'add',
    preJoi$: {
      a: Joi.number().required()
    },
    postJoi$: {
      foo: Joi.number().default(500)
    }
  },
  (req, cb) => {
    cb(null, { foo: req.a + req.b })
  }
)

Joi settings

You can modify the joi validation settings with the pre and post plugin options.

const hemera = new Hemera(nats)
hemera.use(
  require('hemera-joi', {
    patternKeys: {
      default: 'joi$',
      pre: 'preJoi$',
      post: 'postJoi$'
    },
    // joi settings
    pre: { allowUnknown: true },
    post: { stripUnknown: true }
  })
)

Base schemas

You can define base schemas which enrich your existing pre/post schemas. In that way you can ensure that a specific property is always send or if you want to set the joi property allowUnknown to false.

hemera.use(HemeraJoi, {
  basePreSchema: {
    topic: Joi.string().required(),
    cmd: Joi.string().required()
  },
  basePostSchema: {
    userId: Joi.number().required()
  },
  pre: { allowUnknown: false }
})

Plugin decorators

  • .joi
6.0.2

4 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.3.3

5 years ago

5.3.2

6 years ago

5.3.1

6 years ago

5.3.0

6 years ago

5.2.1

6 years ago

5.2.0

6 years ago

5.1.3

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.78

7 years ago

0.1.77

7 years ago

0.1.76

7 years ago

0.1.75

7 years ago

0.1.74

7 years ago

0.1.73

7 years ago

0.1.72

7 years ago

0.1.71

7 years ago

0.1.70

7 years ago

0.1.69

7 years ago

0.1.68

7 years ago

0.1.67

7 years ago

0.1.66

7 years ago

0.1.65

7 years ago

0.1.64

7 years ago

0.1.63

7 years ago

0.1.62

7 years ago

0.1.61

7 years ago

0.1.60

7 years ago

0.1.59

7 years ago

0.1.58

7 years ago