validate.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2015 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <cstdint>
  16. #include <memory>
  17. #include <bsoncxx/document/view.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. namespace bsoncxx {
  21. BSONCXX_INLINE_NAMESPACE_BEGIN
  22. class validator;
  23. ///
  24. /// Validates a BSON document. This is a simplified overload that will
  25. /// only do the bare minimum validation of document structure, and does
  26. /// not provide any further information if the document is found to be invalid.
  27. ///
  28. /// @param data
  29. /// A buffer containing a BSON document to validate.
  30. /// @param length
  31. /// The size of the buffer.
  32. ///
  33. /// @returns
  34. /// An engaged optional containing a view if the document is valid, or
  35. /// an unengaged optional if the document is invalid.
  36. ///
  37. BSONCXX_API stdx::optional<document::view> BSONCXX_CALL validate(const std::uint8_t* data,
  38. std::size_t length);
  39. ///
  40. /// Validates a BSON document. This overload provides additional control over the
  41. /// precise validation that is performed, and will give the caller access to the
  42. /// offset at which the document was found to be invalid.
  43. ///
  44. /// @param data
  45. /// A buffer containing a BSON document to validate.
  46. /// @param length
  47. /// The size of the buffer.
  48. /// @param validator
  49. /// A validator used to configure what checks are done. If validation fails, it
  50. /// will contain the offset at which the document was found to be invalid.
  51. /// @param invalid_offset
  52. /// If validation fails, the offset at which the document was found to be invalid
  53. /// will be stored here (if non-null).
  54. ///
  55. /// @returns
  56. /// An engaged optional containing a view if the document is valid, or
  57. /// an unengaged optional if the document is invalid.
  58. ///
  59. BSONCXX_API stdx::optional<document::view> BSONCXX_CALL
  60. validate(const std::uint8_t* data,
  61. std::size_t length,
  62. const validator& validator,
  63. std::size_t* invalid_offset = nullptr);
  64. ///
  65. /// A validator is used to enable or disable specific checks that can be
  66. /// performed during BSON validation.
  67. ///
  68. class BSONCXX_API validator {
  69. public:
  70. ///
  71. /// Constructs a validator.
  72. ///
  73. validator();
  74. ///
  75. /// Destructs a validator.
  76. ///
  77. ~validator();
  78. ///
  79. /// Verify that all keys and string values are valid UTF-8.
  80. ///
  81. /// @param check_utf8
  82. /// If true, UTF-8 validation is performed.
  83. ///
  84. void check_utf8(bool check_utf8);
  85. ///
  86. /// Getter for the current check_utf8 value of the underlying validator.
  87. ///
  88. /// @return True if UTF-8 validation is performed.
  89. ///
  90. bool check_utf8() const;
  91. ///
  92. /// Verify that all keys and string values are valid UTF-8, but allow
  93. /// null bytes. This is generally bad practice, but some languages such
  94. /// as Python, can sennd UTF-8 encoded strings with null bytes.
  95. ///
  96. /// @param check_utf8_allow_null
  97. /// If true, UTF-8 validation (with null bytes allowed) is performed.
  98. ///
  99. void check_utf8_allow_null(bool check_utf8_allow_null);
  100. ///
  101. /// Getter for the current check_utf8_allow_null value of the underlying
  102. /// validator.
  103. ///
  104. /// @return True if UTF-8 validation (with null bytes allowed) is
  105. /// performed.
  106. ///
  107. bool check_utf8_allow_null() const;
  108. ///
  109. /// Verifies that document keys are not preceeded with '$'. Such keys
  110. /// are reserved for MongoDB internal use.
  111. ///
  112. /// @param check_dollar_keys
  113. /// If true, keys starting with '$' will be treated as invalid.
  114. ///
  115. void check_dollar_keys(bool check_dollar_keys);
  116. ///
  117. /// Getter for the current check_dollar_keys value of the underlying
  118. /// validator.
  119. ///
  120. /// @return True if keys starting with '$' will be treated as invalid.
  121. ///
  122. bool check_dollar_keys() const;
  123. ///
  124. /// Verifies that document keys do not contain any '.' characters. The dot
  125. /// character is illegal in MongoDB keys.
  126. ///
  127. /// @param check_dot_keys
  128. /// If true, keys containing '.' will be treated as invalid.
  129. ///
  130. void check_dot_keys(bool check_dot_keys);
  131. ///
  132. /// Getter for the current check_dot_keys value of the underlying
  133. /// validator.
  134. ///
  135. /// @return True if keys containing '.' will be treated as invalid.
  136. ///
  137. bool check_dot_keys() const;
  138. private:
  139. struct BSONCXX_PRIVATE impl;
  140. std::unique_ptr<impl> _impl;
  141. };
  142. BSONCXX_INLINE_NAMESPACE_END
  143. } // namespace bsoncxx
  144. #include <bsoncxx/config/postlude.hpp>