Skip to content
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

Weird behavior with arrays of objects #60

Open
LinoMota opened this issue Jul 23, 2021 · 3 comments
Open

Weird behavior with arrays of objects #60

LinoMota opened this issue Jul 23, 2021 · 3 comments

Comments

@LinoMota
Copy link

I don't know how deep joigoose works for converting schemas to the ones compatible with mongoose but I am having this problem where my joi schema is kinda valid (cause it did not throw me any error) but when I try to use the joigoose convert method he does not work at all. For my other schemas, it works fine but in this specific case doesn't.

const Joi = require('joi');
const mongoose = require('mongoose');
const Joigoose = require('joigoose')(mongoose);

const item = Joi.object().keys({
  id: Joi.string(),
  label: Joi.string(),
  type: Joi.string(),
  listIds: Joi.array(),
});

const autocomplete = Joi.array().items(item);

const where = {
  restrictions: {
    content: Joi.array().items(autocomplete).allow(null),
  },
};

const schema = {
  where: Joi.array().items(where),
};

console.log(Joigoose.convert(schema));

TypeError: Invalid schema configuration: undefined is not a valid type within the array restrictions.content.See http://bit.ly/mongoose-schematypes for a list of valid schema types.

This is a sample of valid data:

const valid = {
  where: [
    {
      restrictions: {
        content: [
          [{
            id: '123',
            label: 'a',
            type: 'product',
          }],
          [{
            id: '1234',
            label: 'b',
            type: 'tag',
          }],
        ],
      },
    },
  ],
};
@yoitsro
Copy link
Owner

yoitsro commented Jul 27, 2021

Hey @LinoMota. This is certainly strange.

Please can you create a PR which adds this scenario to the test suite? From there, I'll have a base where I can start to debug this.

Thanks in advance!

@yoitsro
Copy link
Owner

yoitsro commented Aug 11, 2021

Thanks for adding the PR. I'm super swamped with my day job at the moment so not 100% when I'll get around to this.

If you're able to, please try to dig into this yourself and see if you can find anything obviously amiss.

@LinoMota
Copy link
Author

You're welcome @yoitsro!

For those who also had this problem I managed to solve using just normal Joi.array().allow(null) without any inner validation, of course it is temporary, I will be digging in the next few days trying to solve this problem too. Have a nice day :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants