Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-48646

JSON Schema validation error does not indicate invalid fields

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Duplicate
    • Affects Version/s: 4.2.6
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      When using JSON Schema validation on a collection, the error returned by the server does not contain any information about which fields didn't match the schema.

      Steps to reproduce

      1. Create a collection with a JSON Schema validator:

      db.createCollection('testschema', {
        validator: {
          $jsonSchema: {
            bsonType: 'object',
            properties: {
              name: { bsonType: 'string' }
            }
          }
        }
      })
      

      2. Try to insert a document which does not respect the schema:

       db.getCollection('testschema').insertOne({ name: 123 })
      

      Actual result

       2020-06-08T17:25:29.696+0200 E QUERY [js] WriteError: Document failed validation :
       WriteError({
       "index" : 0,
       "code" : 121,
       "errmsg" : "Document failed validation",
       "op" : {
         "_id" : ObjectId("5ede5869cc2a7550ab8ab047"),
         "name" : 123
        }
      })
       WriteError@src/mongo/shell/bulk_api.js:461:48
       Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49
       Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13
       Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
       DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
       @(shell):1:1
      

      Expected result

      The error should contain information about which fields failed to match the validation schema.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              backlog-server-triage Backlog - Triage Team
              Reporter:
              avaly@plexapp.com Valentin Agachi
              Participants:
              Votes:
              3 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: