Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makeKeyGeneric supports $[] #12

Merged
merged 1 commit into from
Apr 18, 2021
Merged

Conversation

kfritsch
Copy link
Contributor

This change would let simple-schema support $[<identifier>] as update modifier as requested in longshotlabs/simpl-schema#378. This is the implementation I proposed in this issue.

  • makeKeyGeneric now makes any $[<identifier>] notation generic as well

In case this might cause unwanted behavior for other use cases it might be more wise to add a separate function. Something like:

static generalizePositionalOperator(key) {
    if (typeof key !== 'string') return null;
    return key.replace(/\.([0-9]+|\$\[.*\])(?=\.|$)/g, '.$');
  }

@kfritsch
Copy link
Contributor Author

@aldeed

lib/mongo-object.js Outdated Show resolved Hide resolved
@kfritsch
Copy link
Contributor Author

Made the identifier matching regex more restrictive by only matching character sequences. Im not entirely sure what mongo allows as part of an identifier but since its seems resonable to only use letters there I thought it would be wiser to be more restrictive.

@aldeed aldeed self-requested a review April 13, 2021 17:46
Copy link
Collaborator

@aldeed aldeed left a comment

Choose a reason for hiding this comment

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

@kfritsch Thank you for working on this. It looks like your solution is probably mostly correct, but given the enormous role this function plays in simpl-schema code, you should add a couple tests to prove it works correctly for both $[] and $[<identifier>] at various levels of nesting. The existing test is here: https://github.com/longshotlabs/node-mongo-object/blob/master/lib/mongo-object.test.js#L203-L206

Regarding the identifier piece, the docs say this here:

The <identifier> must begin with a lowercase letter and contain only alphanumeric characters.

So it also needs to handle numbers. But for our purposes, is there any reason to be restrictive? Wouldn't it work to say: $[ (0 or more characters) ]

This would make it future proof if Mongo changes. Either way having some more good test cases in the test file, with various identifiers, will add confidence that this is being done correctly.

@kfritsch
Copy link
Contributor Author

kfritsch commented Apr 14, 2021

According to the docs mongodb is not very restrictive concerning their field names. It seems like a nested field could very well have the name "$[]" or "$[elem]". Isn't there already a problem with numbers as positional index. Consider an array with nested documents, where the nested documents contain numeric fields. collection.find({"foos.5": {"$exists": true}}) could either mean the 6th element or all nested documents that contain the field "5".

There is already a feature request for mongo to escape certain characters.

In any case. I made the identifier less restrictive and added some tests for correct uses of the positional operator neglecting very odd field name usages. I also squashed the commits and made the message more meaningful.

@kfritsch kfritsch requested a review from aldeed April 14, 2021 12:04
@aldeed
Copy link
Collaborator

aldeed commented Apr 14, 2021

@kfritsch

It seems like a nested field could very well have the name "$[]" or "$[elem]".

I don't think so because the two rules as far as I remember are "no periods" and "can't start with a dollar sign"

It is true that foos.5 could be referring to a field with the name "5", but as far as I remember simpl-schema handles this by checking whether foos is an array or object in the schema. That doesn't help in this package, but the current way it works has been fine for years. Basically most people follow the simple guideline of avoiding using a solely numeric key.

Copy link
Collaborator

@aldeed aldeed left a comment

Choose a reason for hiding this comment

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

One more thing and then this should be good

lib/mongo-object.test.js Outdated Show resolved Hide resolved
@aldeed aldeed merged commit 67507c3 into longshotlabs:master Apr 18, 2021
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.

2 participants