Skip to content

Improve field validation for documents - #769

Merged
pcorpet merged 1 commit into
mongomock:developfrom
plaan:improved-field-validation
Nov 30, 2023
Merged

pcorpet merged 1 commit into
mongomock:developfrom
plaan:improved-field-validation

Conversation

@plaan

@plaan plaan commented Apr 29, 2022

Copy link
Copy Markdown
Contributor

Starting from mongodb 3.6 field names cannot contain the null character and top-field names cannot start with the dollar sign. Apart form those cases mongodb does permit storage of the dots and dollar signs (mongodb v3.6). Newer versions of mongodb add improved support (mongodb v4.0, mongodb v5.0).

Fixes: #720

@pcorpet pcorpet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution.

Please add a test that illustrates that recent versions accept weirder names.

Comment thread mongomock/collection.py Outdated

recursive_none_key_check(data)
for key in data.keys():
if len(key) > 0 and key[0] == "$":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use key.startswith

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot about that one, thanks!

Comment thread mongomock/collection.py Outdated


def _validate_data_fields(data):
def recursive_none_key_check(data):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn't using any closure, so I'd rather not have it as a nested function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it out of the function

Comment thread mongomock/collection.py Outdated
def _validate_data_fields(data):
def recursive_none_key_check(data):
for key, value in data.items():
if "\0" in key:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use single quotes for literal strings. '\0' (same in all your changes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to using all single quotes

@plaan
plaan force-pushed the improved-field-validation branch from 9ce81d5 to 32f9fa1 Compare May 9, 2022 08:03
@codecov

codecov Bot commented May 9, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #769 (bc7544b) into develop (d9a6541) will increase coverage by 0.03%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           develop     #769      +/-   ##
===========================================
+ Coverage    95.22%   95.25%   +0.03%     
===========================================
  Files           20       20              
  Lines         3790     3815      +25     
===========================================
+ Hits          3609     3634      +25     
  Misses         181      181              
Impacted Files Coverage Δ
mongomock/collection.py 92.60% <100.00%> (+0.11%) ⬆️
mongomock/patch.py 84.21% <0.00%> (+0.87%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d9a6541...bc7544b. Read the comment docs.

@plaan
plaan force-pushed the improved-field-validation branch 3 times, most recently from 0cb98b0 to 8b63fd5 Compare May 9, 2022 08:34
@plaan
plaan force-pushed the improved-field-validation branch from 8b63fd5 to bc7544b Compare May 9, 2022 08:40
@plaan
plaan requested a review from pcorpet June 20, 2022 09:06
@plaan

plaan commented Oct 19, 2022

Copy link
Copy Markdown
Contributor Author

Hi @pcorpet, wondering if you have the time for a second review. I made sure to add your suggested changes!

@lluissalord

Copy link
Copy Markdown

Hi @pcorpet @plaan do we have any update on this? We are encountering with this issue, and we would like to solve it.

@josemarcosrf

Copy link
Copy Markdown

This would be a great one so the mocking gets closer to the validation of the actual mongo. Any updates on getting this one merged?

@pcorpet

pcorpet commented Nov 30, 2023

Copy link
Copy Markdown
Member

Thanks for pinging folks, it's working. I'll take a look now.

@pcorpet
pcorpet merged commit 0511941 into mongomock:develop Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bson.errors.invalidDocument: key * must not contains '.'

4 participants