2017-08-25 96 views
3

我有一個羽毛球API與messages service。我想用feathers-hooks-validate-joi模塊驗證消息模型。Joi驗證feathersjs

這裏是我的messages-hooks.js文件:

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

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

這裏是我的messages.validator.js文件:

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}; 

當我嘗試通過捲曲發佈一條消息:

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

我收到此錯誤消息:

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

我錯過了什麼嗎?我是否正確使用羽毛鉤?

回答

0
module.exports = {schema}; 

這應該是:

module.exports = schema; 

不應該嗎?

或者,你需要聲明應改爲:當我嘗試你的解決方案,我得到這個錯誤消息

const {schema } = require('./messages.validator'); 
+0

:'{ 「名」:「GeneralError」, 「消息」:「無法讀取屬性'convert'of undefined「, 」code「:500, 」className「:」general-error「, 」data「:{}, 」errors「:{} } –

相關問題