[SERVER-32705] Json schema does not correctly validate an array of enums Created: 15/Jan/18  Updated: 31/Jan/18  Resolved: 16/Jan/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Eric Tray Assignee: Kyle Suarez
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Given the following Json schema valiator applied to a collection:

"properties": {
  "values": {
      "bsonType": "array",
      "items": {
        "bsonType": "string",
        "enum": [
          "one",
          "two",
          "three"
        ]
      },
      "minItems": 1,
      "uniqueItems": true
    }
}

I am still able to insert and also update a document with a 'values' property that contains an invalid value not included in the enum. For example if I insert or update a document via $set containing values ["one", "two", "three", "four"] it still passes validation.

Participants:

 Description   

Using a Json schema containing a property with an enum keyword nested inside a items keyword should fail validation on encountering a value not included in the enum array. Currently it passes validation with invalid values included.



 Comments   
Comment by Kyle Suarez [ 16/Jan/18 ]

Hey etay2000,

Glad things are working as expected. If you run into a problem in the future, don't hesitate to post in the user group!

Best,
Kyle

Comment by Eric Tray [ 16/Jan/18 ]

Hi Kyle,

Sure enough I can't reproduce this anymore either. I swear I have been messing with this off and on for a few days and was debating whether or not to post it to the user groups first or file it as a bug. I was using the Java client and loading an external .json file, parsing it into a Document, then passing that Document into Filters#jsonSchema. Now I am even more confused because the only change I made to my external .json file after testing your example was to remove a top level "bsonType" : "object" keyword that I had before the properties keyword. After I removed that keyword and tested it worked as expected. However, I then put the 'bsonType' keyword back in and tested it again and it still worked as expected. You can close this issue and chalk it up to user error, even though I still don't know what exactly that error was.

Thanks,
Eric

Comment by Kyle Suarez [ 16/Jan/18 ]

Hi etay2000,

I can't reproduce a bug. This is what I've tried:

schema.js

1
const schema = {
2
    $jsonSchema: {
3
        properties: {
4
            values: {
5
                bsonType: "array",
6
                items: {
7
                    bsonType: "string",
8
                    enum: ["one", "two", "three"],
9
                },
10
                minItems: 1,
11
                uniqueItems: true
12
            }
13
        }
14
    }
15
};
16
 
17
let coll = db.getCollection("coll");
18
coll.drop();
19
assert.commandWorked(coll.runCommand("create", {validator: schema}));
20
 
21
const badArray = ["one", "two", "three", "four"];
22
 
23
// Insert
24
assert.writeErrorWithCode(coll.insert({values: badArray}), ErrorCodes.DocumentValidationFailure);
25
 
26
// Update
27
assert.writeOK(coll.insert({_id: 1}));
28
assert.writeErrorWithCode(coll.update({_id: 1}, {$set: {values: badArray}}),
29
                          ErrorCodes.DocumentValidationFailure);

This script asserts that both an insert and an update with a bad array will fail. Could you please verify that the schema that you've used is correct?

If you are still experiencing a bug, please let me know what MongoDB version you're using.

Regards,
Kyle

Generated at Thu Feb 08 04:31:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.