object_constraint.h 4.0 KB

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