[SERVER-36389] Regex pattern in validation $jsonSchema is not validated whatsoever Created: 01/Aug/18  Updated: 04/Sep/18  Resolved: 01/Aug/18

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

Type: Bug Priority: Major - P3
Reporter: Welcius Oshino [X] Assignee: Nick Brewer
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:

Run this code changing process.env.URL_SERVER_MONGODB for your own test server:

Code to reproduce the issue

Participants:

 Description   

I have defined this schema for a new collection and it executes correctly but it accepts any string for friend_code and does not trigger the pattern validation when I insert new rows with non-matching strings in that property.

As you can see in the provided code (in steps to reproduce), a row with a clearly unmatching string for the provided pattern is inserted after creating the collection instead of reporting an error.

Things I've tried:

  • Changing the value of pattern to javascript regex with slashes instead of a string, does not work
  • Changing the validation to a simple one, still allows all the values

Every time I test those things I delete the collection, so it stays up to date with the code.

 



 Comments   
Comment by Nick Brewer [ 01/Aug/18 ]

Welcius Yes, this does indicate to me that the issue is likely outside of the mongod, as the same commands work when applied directly from the shell. If you find anything unusual behavior on the mongod end while investigating this, comment here and we can re-open this ticket. 

-Nick

Comment by Welcius Oshino [X] [ 01/Aug/18 ]

Hello again Nick, I've achieved to run the command you gave me in the mongo shell and I got correct results as you said in your comment, however when I run my js file and then run db.runCommand("listCollections") to watch the status of the collections (as one dev said to me) I get the following output:

Output

According to him, the generated collection has not set the validator at all, so we can conclude that it is an issue related to mongoose then?

In that case, sorry for the issue: i will try to file an issue in the mongoose repository.

Thanks

 

Comment by Nick Brewer [ 01/Aug/18 ]

Welcius So far I haven't been able to reproduce the behavior you're seeing. Out of curiosity, what happens if you try to recreate this directly in the mongo shell? I entered:

db.createCollection('players', {
        validator: {
            $jsonSchema: {
                bsonType: "object",
                additionalProperties : false,
                required: ["_id", "friend_code", "display_name"],
                properties: {
                    _id:          { bsonType: "string", description: "must be a string" },
                    friend_code:  { bsonType: "string", pattern: "^[0-9]{4}-[0-9]{4}-[0-9]{4}$", description: "must be a string" },
                    display_name: { bsonType: "string", description: "must be a string" }
                }
        }
    } }, (err, response) => { });

And then tried two inserts to confirm the validation. First, the example you gave, which triggered the validation error as expected:

db.players.insertOne( {_id: "3", friend_code: "a", display_name: "Welcius"})
2018-08-01T11:55:15.469-0400 E QUERY    [js] WriteError: Document failed validation :
WriteError({
	"index" : 0,
	"code" : 121,
	"errmsg" : "Document failed validation",
	"op" : {
		"_id" : "3",
		"friend_code" : "a",
		"display_name" : "Welcius"
	}
})

And then a passing insert:

db.players.insertOne( {_id: "2", friend_code: "1234-5678-9012", display_name: "Welcius"})
{ "acknowledged" : true, "insertedId" : "2" }

I'd be curious to know if you see similar results when working with the shell directly - this would at least allow us to narrow down where the problem is occurring.

-Nick

Comment by Welcius Oshino [X] [ 01/Aug/18 ]

@nick.brewer Thanks for your answer! I am sorry to ask but can you tell me the difference between using pattern in $jsonSchema and with query operators? There's an example in the documentation that uses pattern in the $jsonSchema as a way to validate documents (the one creating the contacts2 collection in the section Accept or Reject Invalid Documents, in the email property)

https://docs.mongodb.com/manual/core/schema-validation/

I would really appreciate if you could clarify that for me

Comment by Nick Brewer [ 01/Aug/18 ]

Welcius If you want to use regex in a validator, you'll need to use query operators. There's an example in the linked documentation that uses the $regex operator.

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Regards,
Nick

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