jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
284 stars 64 forks source link

Show descriptive error message on validation error #241

Open lisovyk opened 2 years ago

lisovyk commented 2 years ago

Please, add context to the error message when validation is failing upon insert.

As of mongolite version 2.3.1 it gives a general validation error:

>     conn$insert(my_table)
Error: Document failed validation

While in mongo console I can get a right away answer into what's causing validation error:

Document failed validation
...
[ { err: 
        { index: 0,
            code: 121,
            errmsg: 'Document failed validation',
            errInfo: 
                { failingDocumentId: {},
                    details: 
                        { operatorName: '$jsonSchema',
                            schemaRulesNotSatisfied: 
                                [ { operatorName: 'required',
                                    specifiedAs: 
                                        { required: 
                                                [ 'title',
                                                  'date_from',
                                                  'date_to',
                                                  'price_from',
                                                  'price_to',
                                                  'event_id' ] },
                                    missingProperties: 
                                        [ 'event_id',
                                          'price_to',
                                          'type' ] 
                                } ] 
                        } } 
        }
}]

Alternatively, please give me a hint on how to find that error json in mongolite, setting log_level didn't do it and I digged into source code to no avail :(

jeroen commented 2 years ago

Can you give an example how I can setup such a validated collection, in order to reproduce the problem?

lisovyk commented 2 years ago

Sure, will do in couple of hours!

On Sun, May 29, 2022, 15:58 Jeroen Ooms @.***> wrote:

Can you give an example how I can setup such a validated collection, in order to reproduce the problem?

— Reply to this email directly, view it on GitHub https://github.com/jeroen/mongolite/issues/241#issuecomment-1140444179, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF6FCEASW4DSZ446WVV7HA3VMNSXFANCNFSM5XEULFNA . You are receiving this because you authored the thread.Message ID: @.***>

lisovyk commented 2 years ago

Create a database table with a validator, taken example from here

db.createCollection( "contacts", {
   validator: { $jsonSchema: {
      bsonType: "object",
      required: [ "phone" ],
      properties: {
         phone: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         email: {
            bsonType : "string",
            pattern : "@mongodb\.com$",
            description: "must be a string and match the regular expression pattern"
         },
         status: {
            enum: [ "Unknown", "Incomplete" ],
            description: "can only be one of the enum values"
         }
      }
   } }
} )

Try to write with wrong column type from R, e.g.

library(mongolite)
db_connection <- mongo(...) # not sure about that part on localhost, sorry
db_connection$insert('{ "name": "Amanda", "status": "Updated"}') #gives validation error