Skip to content

Commit 68d2b7f

Browse files
author
Micah Scott @ MongoDB
authored
CDRIVER-5721 document bson_validate_with_error_and_offset() (#1769)
* document bson_validate_with_error_and_offset * move bson_validate_flags_t docs to a standalone page * update libbson.inv * opts_templates: fix missing copyright header in mongoc-opts-private.h * build/generate-opts.py
1 parent eddf495 commit 68d2b7f

21 files changed

+159
-61
lines changed

build/generate-opts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, items, **defaults):
115115
validate_option = ('validate', {
116116
'type': 'bson_validate_flags_t',
117117
'convert': '_mongoc_convert_validate_flags',
118-
'help': 'Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.'
118+
'help': 'Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.'
119119
})
120120

121121
collation_option = ('collation', {

build/opts_templates/mongoc-opts-private.h.template

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
#include "mongoc-prelude.h"
218

319
#ifndef MONGOC_OPTS_H

src/libbson/doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ API Reference
1919
bson_subtype_t
2020
bson_type_t
2121
bson_unichar_t
22+
bson_validate_flags_t
2223
bson_value_t
2324
bson_visitor_t
2425
bson_writer_t

src/libbson/doc/bson_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ BSON document contains duplicate keys.
225225
bson_steal
226226
bson_validate
227227
bson_validate_with_error
228+
bson_validate_with_error_and_offset
228229

229230
Example
230231
-------

src/libbson/doc/bson_validate.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ Parameters
1515
----------
1616

1717
* ``bson``: A :symbol:`bson_t`.
18-
* ``flags``: A bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`.
19-
* ``offset``: A location for the offset within ``bson`` where the error occurred.
18+
* ``flags``: A bitwise-or of all desired :symbol:`bson_validate_flags_t`.
19+
* ``offset``: Optional location where the error offset will be written.
2020

2121
Description
2222
-----------
2323

2424
Validates a BSON document by walking through the document and inspecting the keys and values for valid content.
2525

26-
You can modify how the validation occurs through the use of the ``flags`` parameter, see :symbol:`bson_validate_with_error()` for details.
26+
You can modify how the validation occurs through the use of the ``flags`` parameter, see :symbol:`bson_validate_flags_t` for details.
2727

2828
Returns
2929
-------
3030

31-
Returns true if ``bson`` is valid; otherwise false and ``offset`` is set to the byte offset where the error was detected.
31+
If ``bson`` passes the requested validations, returns true.
32+
Otherwise, returns false and if ``offset`` is non-`NULL` it will be written with the byte offset in the document where an error was detected.
33+
34+
To get more information about the specific validation failure, use :symbol:`bson_validate_with_error_and_offset()` instead.
3235

3336
.. seealso::
3437

35-
| :symbol:`bson_validate_with_error()`.
38+
| :symbol:`bson_validate_with_error()`, :symbol:`bson_validate_with_error_and_offset()`.
3639
3740
| :symbol:`bson_visitor_t` can be used for custom validation, :ref:`example_custom_validation`.
38-
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
:man_page: bson_validate_flags_t
2+
3+
bson_validate_flags_t
4+
=====================
5+
6+
Document validation options
7+
8+
Synopsis
9+
--------
10+
11+
.. code-block:: c
12+
13+
#include <bson/bson-types.h>
14+
15+
typedef enum {
16+
BSON_VALIDATE_NONE = 0,
17+
BSON_VALIDATE_UTF8 = (1 << 0),
18+
BSON_VALIDATE_DOLLAR_KEYS = (1 << 1),
19+
BSON_VALIDATE_DOT_KEYS = (1 << 2),
20+
BSON_VALIDATE_UTF8_ALLOW_NULL = (1 << 3),
21+
BSON_VALIDATE_EMPTY_KEYS = (1 << 4),
22+
} bson_validate_flags_t;
23+
24+
Description
25+
-----------
26+
27+
``bson_validate_flags_t`` is a set of binary flags which may be combined to specify a level of BSON document validation.
28+
29+
A value of ``0``, ``false``, or ``BSON_VALIDATE_NONE`` equivalently requests the minimum applicable level of validation.
30+
31+
In the context of validation APIs :symbol:`bson_validate()`, :symbol:`bson_validate_with_error()`, and :symbol:`bson_validate_with_error_and_offset()` the minimum validation still guarantees that a document can be successfully traversed by :symbol:`bson_iter_visit_all()`.
32+
33+
Higher level APIs using this type may have different minimum validation levels. For example, ``libmongoc`` functions that take ``bson_validate_flags_t`` use ``0`` to mean the document contents are not visited and malformed headers will not be detected by the client.
34+
35+
Each defined flag aside from ``BSON_VALIDATE_NONE`` describes an optional validation feature that may be enabled, alone or in combination with other features:
36+
37+
* ``BSON_VALIDATE_NONE`` Minimum level of validation; in ``libbson``, validates element headers.
38+
* ``BSON_VALIDATE_UTF8`` All keys and string values are checked for invalid UTF-8.
39+
* ``BSON_VALIDATE_UTF8_ALLOW_NULL`` String values are allowed to have embedded NULL bytes.
40+
* ``BSON_VALIDATE_DOLLAR_KEYS`` Prohibit keys that start with ``$`` outside of a "DBRef" subdocument.
41+
* ``BSON_VALIDATE_DOT_KEYS`` Prohibit keys that contain ``.`` anywhere in the string.
42+
* ``BSON_VALIDATE_EMPTY_KEYS`` Prohibit zero-length keys.
43+
44+
.. seealso::
45+
46+
| :symbol:`bson_validate()`, :symbol:`bson_validate_with_error()`, :symbol:`bson_validate_with_error_and_offset()`.
47+
48+
| :symbol:`bson_visitor_t` can be used for custom validation, :ref:`example_custom_validation`.

src/libbson/doc/bson_validate_with_error.rst

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ Synopsis
88

99
.. code-block:: c
1010
11-
typedef enum {
12-
BSON_VALIDATE_NONE = 0,
13-
BSON_VALIDATE_UTF8 = (1 << 0),
14-
BSON_VALIDATE_DOLLAR_KEYS = (1 << 1),
15-
BSON_VALIDATE_DOT_KEYS = (1 << 2),
16-
BSON_VALIDATE_UTF8_ALLOW_NULL = (1 << 3),
17-
BSON_VALIDATE_EMPTY_KEYS = (1 << 4),
18-
} bson_validate_flags_t;
19-
2011
bool
2112
bson_validate_with_error (const bson_t *bson,
2213
bson_validate_flags_t flags,
@@ -26,33 +17,28 @@ Parameters
2617
----------
2718

2819
* ``bson``: A :symbol:`bson_t`.
29-
* ``flags``: A bitwise-or of all desired validation flags.
20+
* ``flags``: A bitwise-or of all desired :symbol:`bson_validate_flags_t`.
3021
* ``error``: Optional :symbol:`bson_error_t`.
3122

3223
Description
3324
-----------
3425

3526
Validates a BSON document by walking through the document and inspecting the keys and values for valid content.
3627

37-
You can modify how the validation occurs through the use of the ``flags`` parameter. A description of their effect is below.
38-
39-
* ``BSON_VALIDATE_NONE`` Basic validation of BSON length and structure.
40-
* ``BSON_VALIDATE_UTF8`` All keys and string values are checked for invalid UTF-8.
41-
* ``BSON_VALIDATE_UTF8_ALLOW_NULL`` String values are allowed to have embedded NULL bytes.
42-
* ``BSON_VALIDATE_DOLLAR_KEYS`` Prohibit keys that start with ``$`` outside of a "DBRef" subdocument.
43-
* ``BSON_VALIDATE_DOT_KEYS`` Prohibit keys that contain ``.`` anywhere in the string.
44-
* ``BSON_VALIDATE_EMPTY_KEYS`` Prohibit zero-length keys.
28+
You can modify how the validation occurs through the use of the ``flags`` parameter, see :symbol:`bson_validate_flags_t` for details.
4529

4630
Returns
4731
-------
4832

49-
Returns true if ``bson`` is valid; otherwise false and ``error`` is filled out.
33+
If ``bson`` passes the requested validations, returns true.
34+
Otherwise, returns false and if ``error`` is non-`NULL` it will be filled out with details.
5035

5136
The :symbol:`bson_error_t` domain is set to ``BSON_ERROR_INVALID``. Its code is set to one of the ``bson_validate_flags_t`` flags indicating which validation failed; for example, if a key contains invalid UTF-8, then the code is set to ``BSON_VALIDATE_UTF8``, but if the basic structure of the BSON document is corrupt, the code is set to ``BSON_VALIDATE_NONE``. The error message is filled out, and gives more detail if possible.
5237

38+
To get the specific location of the error, use :symbol:`bson_validate_with_error_and_offset()` instead.
39+
5340
.. seealso::
5441

55-
| :symbol:`bson_validate()`.
42+
| :symbol:`bson_validate()`, :symbol:`bson_validate_with_error_and_offset()`.
5643
5744
| :symbol:`bson_visitor_t` can be used for custom validation, :ref:`example_custom_validation`.
58-
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
:man_page: bson_validate_with_error_and_offset
2+
3+
bson_validate_with_error_and_offset()
4+
=====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
bson_validate_with_error_and_offset (const bson_t *bson,
13+
bson_validate_flags_t flags,
14+
size_t *offset,
15+
bson_error_t *error)
16+
17+
Parameters
18+
----------
19+
20+
* ``bson``: A :symbol:`bson_t`.
21+
* ``flags``: A bitwise-or of all desired :symbol:`bson_validate_flags_t`.
22+
* ``offset``: Optional location where the error offset will be written.
23+
* ``error``: Optional :symbol:`bson_error_t`.
24+
25+
Description
26+
-----------
27+
28+
Validates a BSON document by walking through the document and inspecting the keys and values for valid content.
29+
30+
You can modify how the validation occurs through the use of the ``flags`` parameter, see :symbol:`bson_validate_flags_t` for details.
31+
32+
Returns
33+
-------
34+
35+
If ``bson`` passes the requested validations, returns true.
36+
Otherwise, returns false and writes each non-`NULL` output parameter: ``offset`` with the byte offset of the detected error and ``error`` with the details.
37+
38+
The :symbol:`bson_error_t` domain is set to ``BSON_ERROR_INVALID``. Its code is set to one of the ``bson_validate_flags_t`` flags indicating which validation failed; for example, if a key contains invalid UTF-8, then the code is set to ``BSON_VALIDATE_UTF8``, but if the basic structure of the BSON document is corrupt, the code is set to ``BSON_VALIDATE_NONE``. The error message is filled out, and gives more detail if possible.
39+
40+
.. seealso::
41+
42+
| :symbol:`bson_validate()`, :symbol:`bson_validate_with_error()`.
43+
44+
| :symbol:`bson_visitor_t` can be used for custom validation, :ref:`example_custom_validation`.

src/libmongoc/doc/includes/bulk-insert-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
``opts`` may be NULL or a BSON document with additional command options:
77

8-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
8+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.

src/libmongoc/doc/includes/bulk-replace-one-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
``opts`` may be NULL or a BSON document with additional command options:
77

8-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
8+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
99
* ``collation``: Configure textual comparisons. See `Setting Collation Order <setting_collation_order_>`_, and `the MongoDB Manual entry on Collation <https://www.mongodb.com/docs/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
1010
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1111
* ``upsert``: If true, insert a document if none match ``selector``.

src/libmongoc/doc/includes/bulk-update-many-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
``opts`` may be NULL or a BSON document with additional command options:
77

8-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
8+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
99
* ``collation``: Configure textual comparisons. See `Setting Collation Order <setting_collation_order_>`_, and `the MongoDB Manual entry on Collation <https://www.mongodb.com/docs/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
1010
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1111
* ``upsert``: If true, insert a document if none match ``selector``.

src/libmongoc/doc/includes/bulk-update-one-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
``opts`` may be NULL or a BSON document with additional command options:
77

8-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
8+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
99
* ``collation``: Configure textual comparisons. See `Setting Collation Order <setting_collation_order_>`_, and `the MongoDB Manual entry on Collation <https://www.mongodb.com/docs/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
1010
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1111
* ``upsert``: If true, insert a document if none match ``selector``.

src/libmongoc/doc/includes/delete-many-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
99
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
10-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
10+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``comment``: A :symbol:`bson_value_t` specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Requires MongoDB 4.4 or later.
1212
* ``collation``: Configure textual comparisons. See `Setting Collation Order <setting_collation_order_>`_, and `the MongoDB Manual entry on Collation <https://www.mongodb.com/docs/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
1313
* ``hint``: A document or string that specifies the index to use to support the query predicate.

src/libmongoc/doc/includes/delete-one-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
99
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
10-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
10+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``comment``: A :symbol:`bson_value_t` specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Requires MongoDB 4.4 or later.
1212
* ``collation``: Configure textual comparisons. See `Setting Collation Order <setting_collation_order_>`_, and `the MongoDB Manual entry on Collation <https://www.mongodb.com/docs/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
1313
* ``hint``: A document or string that specifies the index to use to support the query predicate.

src/libmongoc/doc/includes/insert-many-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
99
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
10-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
10+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``comment``: A :symbol:`bson_value_t` specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Requires MongoDB 4.4 or later.
1212
* ``ordered``: set to ``false`` to attempt to insert all documents, continuing after errors.
1313
* ``bypassDocumentValidation``: Set to ``true`` to skip server-side schema validation of the provided BSON documents.

src/libmongoc/doc/includes/insert-one-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
99
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
10-
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
10+
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``comment``: A :symbol:`bson_value_t` specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Requires MongoDB 4.4 or later.
1212
* ``bypassDocumentValidation``: Set to ``true`` to skip server-side schema validation of the provided BSON documents.
34 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)