Setting empty object causes validation to fail
See original GitHub issueI 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:
- Created 7 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can confirm that this is fixed!
This looks like it was fixed with #4218, the below script works just fine for me with master.