4

I have a feathersjs API with a messages service. I want to validate the message model with feathers-hooks-validate-joi module.

Here is my messages-hooks.js file:

const validate = require('feathers-hooks-validate-joi');
const schema = require('./messages.validator');

module.exports = {
  before: {
    create: [validate.form(schema)],
    //others method fields
  },
  after: {...},
  error: {...}
};

Here is my messages.validator.js file:

const Joi = require('joi');

const schema = Joi.object().keys({
  name: Joi.string().trim().min(2).required(),
  text: Joi.string().trim().min(2).required()
});

module.exports = {schema};

When I try to post a message via curl:

curl 'http://localhost:3030/messages/' -H 'Content-Type: application/json' --data-binary '{ "name": "Hello", "text": "World" }'

I receive this error message:

{
  "name": "BadRequest",
  "message": "Invalid data",
  "code": 400,
  "className": "bad-request",
  "data": {},
  "errors": {
    "name": "\"name\" is not allowed",
    "text": "\"text\" is not allowed"
  }
}

Am I missing something? Am I using the feathers hook correctly?

louis amoros
  • 2,418
  • 3
  • 19
  • 40

1 Answers1

1
module.exports = {schema};

This should be:

module.exports = schema;

shouldn't it?

Alternatively, your require statement should be changed to:

const {schema } = require('./messages.validator');
Linus Borg
  • 23,622
  • 7
  • 63
  • 50
  • When I try your solution I got this error message: `{ "name": "GeneralError", "message": "Cannot read property 'convert' of undefined", "code": 500, "className": "general-error", "data": {}, "errors": {} }` – louis amoros Aug 25 '17 at 10:16