Code Monkey home page Code Monkey logo

Comments (8)

juliancoleman avatar juliancoleman commented on May 20, 2024 1

Great. Thanks for the quick reply! That's all I needed to know.

from celebrate.

giltho avatar giltho commented on May 20, 2024

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.

juliancoleman avatar juliancoleman commented on May 20, 2024

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.

giltho avatar giltho commented on May 20, 2024

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.

juliancoleman avatar juliancoleman commented on May 20, 2024

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.

giltho avatar giltho commented on May 20, 2024

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.

giltho avatar giltho commented on May 20, 2024

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.

juliancoleman avatar juliancoleman commented on May 20, 2024

That is correct, yes

from celebrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.