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

Empty modifier passes validation, then wipes document #377

Open
coagmano opened this issue Mar 21, 2018 · 1 comment
Open

Empty modifier passes validation, then wipes document #377

coagmano opened this issue Mar 21, 2018 · 1 comment
Milestone

Comments

@coagmano
Copy link
Member

When performing an update with an empty modifier {}, it passes validation and then Meteor treats it as an literal document and replaces the document with the empty one.


I've got a very simple setup:

export const Pages = new Meteor.Collection('pages');
export const pageSchema = new SimpleSchema({
    projectId: SimpleSchema.RegEx.Id,
    name: String,
});
Pages.attachSchema(pageSchema);
Factory.define('page', Pages, {
    projectId: Factory.get('project'),
    name: () => faker.random.word(),
});

To illustrate, I'll create a test document in meteor shell:

> Factory.create('page')
Document {
  _id: 'imerjq2XrTKw8t6Lh',
  projectId: 'bh52bEnkd8nPP7TLj',
  name: 'alarm' }

Tests with modifiers that violate the schema fail:

Pages.update('imerjq2XrTKw8t6Lh', { $unset: { name: 'test' } }) -> throws ValidationError

Tests with literal documents fail validation:

Pages.update('imerjq2XrTKw8t6Lh', {name: 'test'}) -> throws ValidationError

But passing in the empty modifier:

Pages.update('imerjq2XrTKw8t6Lh', {}) -> returns 1

Wipes the document:

> Pages.findOne('imerjq2XrTKw8t6Lh')
Document { _id: 'imerjq2XrTKw8t6Lh' }

Is this a bug?

What's the recommended way of preventing this? Should I extend and overload update to check for empty objects?

@aldeed
Copy link
Collaborator

aldeed commented Apr 11, 2018

This certainly feels like a bug, but in reality it is the way that Mongo updates work without any validation attached. Still, if you do attach validation, it makes sense that you might want to be warned about this. Not sure exactly what this would look like off the top of my head, but I guess some way to inform it that you explicitly want to allow a whole-document update?

@StorytellerCZ StorytellerCZ added this to the v4 milestone Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants