[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: |
| 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:
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: 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:
And then tried two inserts to confirm the validation. First, the example you gave, which triggered the validation error as expected:
And then a passing insert:
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, |