-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit testing validators #55
Comments
Celebrate exposes his own version of joi { Joi } = require('celebrate');
Joi.validate(objectToValidate); I am not entirely sure it answers to your question though |
Great. Thanks for the quick reply! That's all I needed to know. |
Perhaps that wasn't the solution. I'll outline my code below. What I'm trying to do is accomplish the same result as // validator.js
const { celebrate, Joi } = require('celebrate');
const Validator = celebrate({
params : Joi.object().keys({
eventKey : Joi.string().required(),
}).unknown(false),
});
module.exports = Validator; // validator_test.js
const { Joi } = require('celebrate');
const R = require('ramda');
const Validator = require('./validator');
const helpers = require('./helpers');
const validPayload = {
params : {
eventKey : 'testKey',
},
};
describe('DeleteEventsValidator', () => {
it(' does not allow unknown keys', () => {
const payload = helpers.mergeObjectWithValidPayload(R.lensProp('params'), { unknownKey : 'hello' }, validPayload);
const result = Joi.validate(payload, Validator);
const { error } = result;
expect(error).to.not.be.null;
expect(R.prop('details', error)).to.have.length(1);
expect(error.message).to.eql('"unknownKey" is not allowed');
});
});
Anyway, this throws the following error:
|
Hum, something is unclear with what you are doing
Also, i don't know what tries to read the I realize this is not very clear, but you might want to try something more like : const Validator = Joi.object.keys( {
params : Joi.object().keys( {
eventKey : Joi.string().required(),
} ).unknown( false ),
} ); And then use |
I have no excuse for not figuring that out sooner. I apologize for the miscommunication. I thought Celebrate was a wrapper around Joi. Now I know! Thanks a bunch for another quick response. I've gotten my test passing now. I can continue with my work. And then when I want to use the Celebrate middleware, will it just be... router.delete('/events/:eventKey', Celebrate(Validator), (req, res) => {
// ...
}); |
It sould be :) |
Wait that is not true, if you actually want to use Celebrate, the actual code would be : const Validator = {
params : Joi.object().keys( {
eventKey : Joi.string().required(),
} ).unknown( false ),
} ;
router.delete('/events/:eventKey', celebrate(validator), (req, res) => {
// ...
});
|
That is correct, yes |
node
version - 8.9.0celebrate
version - ^7.0.1joi
version (vianpm ls --depth=0 | grep joi
) -^13.0.2Am I to be using
Joi.validate()
, or doesCelebrate
expose its own custom validator?The text was updated successfully, but these errors were encountered: