object_constraint.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include <cstdlib>
  3. #include <map>
  4. #include <string>
  5. #include <unordered_set>
  6. #include <utility>
  7. #include <vector>
  8. #include <jvalidate/forward.h>
  9. namespace jvalidate::constraint {
  10. /**
  11. * @brief A constraint on the Object type.
  12. * Given an argument object, every element that is not subject to a
  13. * {@see PropertiesConstraint} or {@see PatternPropertiesConstraint} in the
  14. * same level of the Schema as this constraint is validated against subschema.
  15. *
  16. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2.3
  17. */
  18. struct AdditionalPropertiesConstraint {
  19. schema::Node const * subschema = nullptr;
  20. std::unordered_set<std::string> properties;
  21. std::vector<std::string> patterns;
  22. };
  23. /**
  24. * @brief A constraint on the Object type representing two different
  25. * constraints:
  26. * - If a given property is present in the Object, then validate the overall
  27. * object against the subschema (dependentSchemas).
  28. * - If a given property is present in the Object, then require that the
  29. * overall object also contains a list of properties (dependentRequired).
  30. * Because the explicit separation into different keywords is part of
  31. * Draft2020-12, the behaviors are merged together for legacy reasons.
  32. *
  33. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.2.4
  34. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.4
  35. */
  36. struct DependenciesConstraint {
  37. std::map<std::string, schema::Node const *> subschemas;
  38. std::map<std::string, std::unordered_set<std::string>> required;
  39. };
  40. /**
  41. * @brief A constraint on the Object type with the following characteristic(s):
  42. * @code{.cpp}
  43. * arg.size() <= value
  44. * @endcode
  45. *
  46. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.1
  47. */
  48. struct MaxPropertiesConstraint {
  49. size_t value = 0;
  50. };
  51. /**
  52. * @brief A constraint on the Object type with the following characteristic(s):
  53. * @code{.cpp}
  54. * arg.size() >= value
  55. * @endcode
  56. *
  57. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.2
  58. */
  59. struct MinPropertiesConstraint {
  60. size_t value = 0;
  61. };
  62. /**
  63. * @brief A constraint on the Object type.
  64. * Validates properties whose names match a given regex against the subschema.
  65. * Multiple patterns allowed to match to the same property, meaning that a
  66. * property can be validated against more than one subschema.
  67. *
  68. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2.2
  69. */
  70. struct PatternPropertiesConstraint {
  71. std::vector<std::pair<std::string, schema::Node const *>> properties;
  72. };
  73. /**
  74. * @brief A constraint on the Object type.
  75. * Validates named properties against their matching subschemas. Does not
  76. * intrinsically require that the property be present in the Object without
  77. * use of {@see RequiredConstraint}.
  78. *
  79. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2.1
  80. */
  81. struct PropertiesConstraint {
  82. std::map<std::string, schema::Node const *> properties;
  83. };
  84. /**
  85. * @brief A constraint on the individual keys of an Object type. Since each key
  86. * is a string, key_schema should be a schema which validates against the
  87. * String type.
  88. * @code{.py}
  89. * for key in arg.keys():
  90. * key_schema.validate(key)
  91. * @endcode
  92. *
  93. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2.4
  94. */
  95. struct PropertyNamesConstraint {
  96. schema::Node const * key_schema = nullptr;
  97. };
  98. /**
  99. * @brief A constraint on the Object type.
  100. * Validates that the object contains all of the stored properties.
  101. * @code{.py}
  102. * all(p in arg for p in properties)
  103. * @endcode
  104. *
  105. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.3
  106. */
  107. struct RequiredConstraint {
  108. std::unordered_set<std::string> properties;
  109. };
  110. /**
  111. * @brief A constraint on the Object type.
  112. * Given an argument object, every property which is not evaluated by another
  113. * schema {@see jvalidate::Status::Noop} will be validated against subschema.
  114. *
  115. * https://json-schema.org/draft/2020-12/json-schema-core#section-11.3
  116. */
  117. struct UnevaluatedPropertiesConstraint {
  118. schema::Node const * subschema = nullptr;
  119. };
  120. }