Comments (8)
Great. Thanks for the quick reply! That's all I needed to know.
from celebrate.
Celebrate exposes his own version of joi
So basically
{ Joi } = require('celebrate');
Joi.validate(objectToValidate);
I am not entirely sure it answers to your question though
from celebrate.
Perhaps that wasn't the solution. I'll outline my code below. What I'm trying to do is accomplish the same result as Joi.validate(payload, validator);
// 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');
});
});
helpers.mergeObjectWithValidPayload
is just a deep object merge. It will merge { unknownKey : 'hello' }
into validPayload.params
.
Anyway, this throws the following error:
1) DeleteEventsValidator does not allow unknown keys:
TypeError: Cannot read property 'params' of null
from celebrate.
Hum, something is unclear with what you are doing
Celebrate is a middleware that should be used to validate inputs such as query/body/headers.
I don't see why you need to use Celebrate here, isn't Joi enough ?
celebrate(...)
doesn't create a validator, it creates a middleware (a function that takes req, res and next)
Also, i don't know what tries to read the params
property of your object but it might not be celebrate, I think the error comes from your call of helpers.mergeObjectWithValidPayload
(Although it does not change your question since it would have failed anyway because of your usage of celebrate
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 Joi.validate
as you do
from celebrate.
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) => {
// ...
});
from celebrate.
It sould be :)
Although depending on your version it might be celebrate(Validator)
instead of Celebrate(Validator)
(I think there's a mistake in the readme on that point)
from celebrate.
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) => {
// ...
});
celebrate
takes an object with properties params
, headers
etc. (at least one of them), and each of these property should be a Joi schema.
from celebrate.
That is correct, yes
from celebrate.
Related Issues (20)
- Question: should full mode collect multiple errors per segment? HOT 2
- Assertion error on validating an uri HOT 10
- Validate multipart/form-data HOT 1
- Error details are empty in error message HOT 6
- Difference between 'body' and '[Segments.BODY]' (Question) HOT 4
- Set default value if validation is failed. especially for query params which are optional HOT 6
- Swagger integration HOT 2
- Full mode validation can not be enabled HOT 3
- Validating a form data json object? HOT 3
- Mode FULL doesn't seem to be working. HOT 2
- Examples in TypeScript too HOT 2
- ChainAlert: npm package release (15.0.1) has no matching tag in this repo HOT 1
- Type definitions for converted data HOT 2
- node 16 Object.defineProperty compatibility HOT 2
- Compatebility with Prisma and prisma-joi-generator HOT 2
- joi.alternatives error message too generic HOT 3
- feat: infer typing from celebrate args HOT 1
- Option to not call next with errors HOT 2
- Migrate away from TravisCI to Github Actions HOT 1
- Nested schema error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from celebrate.