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

Show descriptive error message on validation error #241

Open
lisovyk opened this issue May 27, 2022 · 3 comments
Open

Show descriptive error message on validation error #241

lisovyk opened this issue May 27, 2022 · 3 comments

Comments

@lisovyk
Copy link

lisovyk commented May 27, 2022

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
Copy link
Owner

jeroen commented May 29, 2022

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

@lisovyk
Copy link
Author

lisovyk commented May 29, 2022 via email

@lisovyk
Copy link
Author

lisovyk commented May 29, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants