2

I have a MongoCollection with a aldeed:simple-schema attached, where the content property has the type Object:

The following code writes the document to console, then inserts it, then fetches the document with the correct id and writes this to console:

console.log(doc);
const id = notes.collection.insert(doc);
let newdoc = notes.collection.findOne({_id: id});
console.log(newdoc);

During the round-robin operation the value inside the object of the content property is lost.

Before the insert:

I20160304-16:52:24.722(-5)? { doctorId: 'xD7FiSfYdqwk94gQ6',                                                                                                                                                                              
I20160304-16:52:24.723(-5)?   patientId: '4wG3nnkzrfH4W2hsG',                                                                                                                                                                             
I20160304-16:52:24.723(-5)?   created: 1457128344,                                                                                                                                                                                        
I20160304-16:52:24.723(-5)?   type: 'note',                                                                                                                                                                                               
I20160304-16:52:24.727(-5)?   content: { noteText: 'Test' } }                                                                                                                                                                             

When retrieved from the database:

I20160304-16:52:24.734(-5)? { _id: 'w6rRoMqtJc5EKFKFs',                                                                                                                                                                                   
I20160304-16:52:24.735(-5)?   doctorId: 'xD7FiSfYdqwk94gQ6',                                                                                                                                                                              
I20160304-16:52:24.735(-5)?   patientId: '4wG3nnkzrfH4W2hsG',                                                                                                                                                                             
I20160304-16:52:24.735(-5)?   created: 1457128344,                                                                                                                                                                                        
I20160304-16:52:24.736(-5)?   type: 'note',                                                                                                                                                                                               
I20160304-16:52:24.736(-5)?   content: {} } 

I don't understand why this happens. This is the specification of the content attribute in the simple-schema:

Carin.subschemas.object = {
  type: Object,
  optional: false
};
Hans
  • 2,800
  • 3
  • 28
  • 40

2 Answers2

1

From the SimpleSchema docs:

If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema. If this is not possible or you don't care to validate the object's properties, use the blackbox: true option to skip validation for everything within the object.

Based on this, you either need to add blackbox: true to your schema:

Carin.subschemas.object = {
  type: Object,
  blackbox: true,
  optional: false
};

Or you need to add all appropriate fields.

Stephen Woods
  • 4,049
  • 1
  • 16
  • 27
  • Thanks I missed that bit. I still find the behaviour odd that the object is inserted instead of a validation error being thrown. Is there a reason for this? – Hans Mar 05 '16 at 08:20
  • 1
    I haven't seen anything in the source to indicate the reasoning unfortunately. – Stephen Woods Mar 05 '16 at 13:52
1

Error Cause : You have to mention whether object properties needs to be validated or not

case 1: if you want some of your properties to be validated

add 'optional :true' to fields that need not be validated

//assume you want only x to be validated
Carin.subschemas.object.x:{
  type:String,
  label:"x"
},
Carin.subschemas.object.y:{
  type:String,
  label:"y",
  optional:true 
} 

case 2 : none of the properties to be validated

Carin.subschemas.object = {
  type: Object,
  blackbox: true,
  optional: false
}
Sai Ram
  • 4,196
  • 4
  • 26
  • 39