[CDRIVER-2296] Option to pass bson_validate_flags_t to update/insert functions Created: 22/Sep/17  Updated: 28/Oct/23  Resolved: 12/Dec/17

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 1.10.0

Type: Improvement Priority: Minor - P4
Reporter: Lars Lundstedt [X] Assignee: Xiangyu Yao (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CDRIVER-2265 Overly validating documents in bulk i... Closed
Related
is related to CDRIVER-2331 New "with_opts" functions to accept s... Closed
is related to CDRIVER-2340 Refactor mongoc_write_command_t's opt... Backlog

 Description   

There is currently no way of controlling what the insert/update functions check for when validating BSON documents prior to insert/update. For example, a client can not ask the validation function called by mongoc_collection_update() to validate the BSON and tell it to allow empty keys. This can of course be worked around by explicitly calling bson_validate() with the desired flags and then calling insert/update with MONGOC_INSERT/UPDATE_NO_VALIDATE set, but that adds a little bit of complexity and some code clutter to client code. It would be handy to just be able to pass bson_validate_flags_t to the insert/update calls.



 Comments   
Comment by Githook User [ 02/Feb/18 ]

Author:

{'email': 'jesse@mongodb.com', 'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis'}

Message: CDRIVER-2296 fix "validate" options docs
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/6855c9b553e2255f008f17f44f794e89c46ed5c0

Comment by Githook User [ 12/Dec/17 ]

Author:

{'name': 'Xiangyu Yao', 'email': 'xiangyu.yao24@gmail.com', 'username': 'xy24'}

Message: CDRIVER-2296 pass bson_validate_flags_t to update/insert functions via opt
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/48a9e73c2067514dad94afafc0f8659c32760b2d

Comment by Xiangyu Yao (Inactive) [ 08/Dec/17 ]

jesse I think

allowNull: true, means the BSON_VALIDATE_UTF8_ALLOW_NULL bit is 0

should be

allowNull: true, means the BSON_VALIDATE_UTF8_ALLOW_NULL bit is 1

Base on how we use it eventually: link

Comment by Githook User [ 14/Nov/17 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: CDRIVER-2296 CRUD functions take "validate: false"
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/af1164d8d991499103943d86f92b75a147ea3209

Comment by A. Jesse Jiryu Davis [ 29/Oct/17 ]

I'm beginning this API for the new CRUD functions in 1.9.0. The functions are:

CDRIVER-2331: mongoc_collection_insert_one, mongoc_collection_insert_many
CDRIVER-2205: mongoc_collection_update_one, mongoc_collection_update_many, mongoc_collection_replace_one

These will now accept an option with field name "validate". If its value is falsey according to bson_iter_as_bool (false, 0, null, ...) then no validation is performed. If it's exactly "true" then the driver does standard validation. Finer control (check UTF-8, allow NIL or "$" or "." or empty keys) will be designed and implemented later.

Comment by A. Jesse Jiryu Davis [ 24/Oct/17 ]

We could define a way to pass validation options in a bson_t to all the new _with_opts functions, e.g. if you pass 'allowEmptyKeys': true to mongoc_collection_insert_with_opts it'll allow empty keys.

Update the validate functions in mongoc-util.c: _mongoc_validate_new_document, _mongoc_validate_replace, and _mongoc_validate_update.

We need a function that converts from a bson_t *opts to a bson_validate_flags_t bit field, and uses that to override the default validate flags used by those three functions. Let's interpret fields in "opts" like so:

  • validate: false, means all bits are 0

Otherwise:

  • allowEmptyKeys: true, means the BSON_VALIDATE_EMPTY_KEYS bit is 0
  • allowDollarKeys: true, means the BSON_VALIDATE_DOLLAR_KEYS bit is 0
  • allowNull: true, means the BSON_VALIDATE_UTF8_ALLOW_NULL bit is 0
  • allowDot: true, means the BSON_VALIDATE_DOT_KEYS bit is 0
  • validateUTF8: false, means the BSON_VALIDATE_UTF8 bit is 0
Generated at Wed Feb 07 21:14:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.