question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting empty object causes validation to fail

See original GitHub issue

I have a schema like this:

{
  // other fields
  challenge: {
    id: {type: String, ref: 'Challenge', validate: [validator.isUUID, 'Invalid uuid.']}, // When set (and userId not set) it's the original task
    taskId: {type: String, ref: 'Task', validate: [validator.isUUID, 'Invalid uuid.']}, // When not set but challenge.id defined it's the original task
    broken: {type: String, enum: ['CHALLENGE_DELETED', 'TASK_DELETED', 'UNSUBSCRIBED', 'CHALLENGE_CLOSED', 'CHALLENGE_TASK_NOT_FOUND']}, // CHALLENGE_TASK_NOT_FOUND comes from v3 migration
    winner: String, // user.profile.name of the winner
  },
}

If doc.challenge has some values defined and then i try to set it to {}

doc.challenge = {}
doc.save().catch(function (err) {
  console.log(err)
})

saving fails with

name=ValidationError, 
message=`null` is not a valid enum value for path `challenge.broken`., 
name=ValidatorError, enumValues=[CHALLENGE_DELETED, TASK_DELETED, UNSUBSCRIBED, CHALLENGE_CLOSED, CHALLENGE_TASK_NOT_FOUND], 
type=enum, 
message=`{VALUE}` is not a valid enum value for path `{PATH}`., 
validator=function (v) {return undefined === v || ~vals.indexOf(v);`

with similar messages for the other fields that have some type of validation.

The problem seems to be (from validator=function (v) {return undefined === v || ~vals.indexOf(v);) that mongoose think challenge.broken is null while instead is undefined and then tries to validate the field which of course fails

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pagliascommented, Jun 19, 2016

Can confirm that this is fixed!

0reactions
vkarpov15commented, Jun 18, 2016

This looks like it was fixed with #4218, the below script works just fine for me with master.

'use strict';

var assert = require('assert');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4182');
mongoose.set('debug', true);

var testSchema = new mongoose.Schema({
  // other fields
  challenge: {
    broken: {type: String, enum: ['CHALLENGE_DELETED', 'TASK_DELETED', 'UNSUBSCRIBED', 'CHALLENGE_CLOSED', 'CHALLENGE_TASK_NOT_FOUND']}, // CHALLENGE_TASK_NOT_FOUND comes from v3 migration
  }
});

var Model = mongoose.model('gh4182', testSchema);

var doc = new Model({ challenge: { broken: 'TASK_DELETED' } });
doc.save(function(error, doc) {
  assert.ifError(error);
  Model.findById(doc, function(error, doc) {
    assert.ifError(error);
    doc.challenge = {};
    doc.save(function(error) {
      assert.ifError(error);
      console.log('done');
      process.exit(0);
    });
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does react hook form errors object temporarily empty?
Because on change of your input, you run the validation again and it clears the errors on all valid fields.
Read more >
Control.CausesValidation Property (System.Windows.Forms)
Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives...
Read more >
empty - Manual - PHP
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value...
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >
Unparsable structured data report - Search Console Help
If you are having problems finding the error, try starting from an empty object, then add back content from your broken code piece...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found