validation_criteria.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <bsoncxx/document/view_or_value.hpp>
  16. #include <bsoncxx/stdx/optional.hpp>
  17. #include <mongocxx/stdx.hpp>
  18. #include <mongocxx/config/prelude.hpp>
  19. namespace mongocxx {
  20. MONGOCXX_INLINE_NAMESPACE_BEGIN
  21. ///
  22. /// Class representing criteria for document validation, to be applied to a collection.
  23. ///
  24. /// @see https://docs.mongodb.com/master/core/document-validation/
  25. ///
  26. class MONGOCXX_API validation_criteria {
  27. public:
  28. ///
  29. /// Sets a validation rule for this validation object.
  30. ///
  31. /// @param rule
  32. /// Document representing a validation rule.
  33. ///
  34. /// @return
  35. /// A reference to the object on which this member function is being called. This facilitates
  36. /// method chaining.
  37. ///
  38. validation_criteria& rule(bsoncxx::document::view_or_value rule);
  39. ///
  40. /// Gets the validation rule for this validation object.
  41. ///
  42. /// @return
  43. /// Document representing a validation rule.
  44. ///
  45. const stdx::optional<bsoncxx::document::view_or_value>& rule() const;
  46. ///
  47. /// A class to represent the different validation level options.
  48. ///
  49. /// - k_off: Disable validation entirely.
  50. /// - k_moderate: Apply validation rules to inserts, and apply validation rules to updates only
  51. /// if the document to be updated already fulfills the validation criteria.
  52. /// - k_strict: Apply validation rules to all inserts and updates.
  53. ///
  54. enum class validation_level {
  55. k_off,
  56. k_moderate,
  57. k_strict,
  58. };
  59. ///
  60. /// Sets a validation level.
  61. ///
  62. /// @param level
  63. /// An enumerated validation level.
  64. ///
  65. /// @return
  66. /// A reference to the object on which this member function is being called. This facilitates
  67. /// method chaining.
  68. ///
  69. validation_criteria& level(validation_level level);
  70. ///
  71. /// Gets the validation level.
  72. ///
  73. /// @return
  74. /// The enumerated validation level.
  75. ///
  76. const stdx::optional<validation_level>& level() const;
  77. ///
  78. /// A class to represent the different validation action options.
  79. ///
  80. /// - k_error: Reject any insertion or update that violates the validation criteria.
  81. /// - k_warn: Log any violations of the validation criteria, but allow the insertion or update
  82. /// to proceed.
  83. ///
  84. enum class validation_action {
  85. k_error,
  86. k_warn,
  87. };
  88. ///
  89. /// Sets a validation action to run when documents failing validation are inserted or modified.
  90. ///
  91. /// @param action
  92. /// An enumerated validation action.
  93. ///
  94. /// @return
  95. /// A reference to the object on which this member function is being called. This facilitates
  96. /// method chaining.
  97. ///
  98. validation_criteria& action(validation_action action);
  99. ///
  100. /// Gets the validation action to run when documents failing validation are inserted or
  101. /// modified.
  102. ///
  103. /// @return
  104. /// The enumerated validation action.
  105. ///
  106. const stdx::optional<validation_action>& action() const;
  107. ///
  108. /// Returns a bson document representing this set of validation criteria.
  109. ///
  110. /// @deprecated
  111. /// This method is deprecated. To determine which options are set on this object, use the
  112. /// provided accessors instead.
  113. ///
  114. /// @return Validation criteria, as a document.
  115. ///
  116. MONGOCXX_DEPRECATED bsoncxx::document::value to_document() const;
  117. bsoncxx::document::value to_document_deprecated() const;
  118. ///
  119. /// @deprecated
  120. /// This method is deprecated. To determine which options are set on this object, use the
  121. /// provided accessors instead.
  122. ///
  123. MONGOCXX_DEPRECATED MONGOCXX_INLINE operator bsoncxx::document::value() const;
  124. private:
  125. stdx::optional<bsoncxx::document::view_or_value> _rule;
  126. stdx::optional<validation_level> _level;
  127. stdx::optional<validation_action> _action;
  128. };
  129. MONGOCXX_API bool MONGOCXX_CALL operator==(const validation_criteria& lhs,
  130. const validation_criteria& rhs);
  131. MONGOCXX_API bool MONGOCXX_CALL operator!=(const validation_criteria& lhs,
  132. const validation_criteria& rhs);
  133. MONGOCXX_INLINE validation_criteria::operator bsoncxx::document::value() const {
  134. return to_document_deprecated();
  135. }
  136. MONGOCXX_INLINE_NAMESPACE_END
  137. } // namespace mongocxx
  138. #include <mongocxx/config/postlude.hpp>