Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-14355

Incorrect regex example in schema validation documentation

    XMLWordPrintableJSON

Details

    Description

      Description

      Hi team,

      I discovered an incorrect regex example in our documentation regarding schema validation. https://docs.mongodb.com/manual/core/schema-validation/#accept-or-reject-invalid-documents 

      ```

      email:

      { bsonType : "string", pattern : "@mongodb\.com$", description: "must be a string and match the regular expression pattern" }

      ```

      This part may lead the readers to believe that the regex expression defined in "pattern" will match any string ends with "@mongodb.com", however, the regex above will also match "@mongodbxcom" or "@mongodbbcom"

      The correct expression when used with "pattern" should be  "@mongodb\\.com$" because the `\` must be escaped and replaced with \\ in a JSON string.

       

       

      Test 1: shows the current example accepts "@mongodbb.com"

      ```

      MongoDB Enterprise >
      db.getSiblingDB('TSA').createCollection("test", {validator: {
        $jsonSchema:{
        bsonType: "object",
        properties:
        {
          emailsuffix: { bsonType:"string", pattern: "@mongodb\.com$"}
        }}}})
       
      { "ok" : 1 }
      

      MongoDB Enterprise >
      db.getSiblingDB('TSA').test.insert(
      {emailsuffix: "abcd@mongodbbcom"}
      )
       
      WriteResult(\{ "nInserted" : 1 })
      

      ```

      Test 2: shows with the correctly escaped '\' it won't accept "@mongodbb.com", and accepts only "@mongodb.com"

      ```

      MongoDB Enterprise > db.getSiblingDB('TSA').test.drop()

      true

      MongoDB Enterprise >
      db.getSiblingDB('TSA').createCollection("test", {validator: {
        $jsonSchema:{
          bsonType: "object",
          properties: {
            emailsuffix: { bsonType:"string", pattern: "@mongodb\\.com$"}
          }
      }}})
       
      { "ok" : 1 }
      

       

      MongoDB Enterprise > db.getSiblingDB('TSA').test.insert(\{emailsuffix: "abcd@mongodbbcom"})
       
      WriteResult(\{ "nInserted" : 0, "writeError" : { "code" : 121, "errmsg" : "Document failed validation" }})
      
      

       
       
      MongoDB Enterprise > db.getSiblingDB('TSA').test.insert(\{emailsuffix: "abcd@mongodb.com"})
       
      WriteResult(\{ "nInserted" : 1 })
      

       

      Best regards,

       

      Raymond

      Scope of changes

      Impact to Other Docs

      MVP (Work and Date)

      Resources (Scope or Design Docs, Invision, etc.)

      Attachments

        Activity

          People

            jeffrey.allen@mongodb.com Jeffrey Allen
            raymond.hu@mongodb.com Raymond Hu
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              1 year, 8 weeks, 2 days ago