array_constraint.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <cstdlib>
  3. #include <optional>
  4. #include <vector>
  5. #include <jvalidate/forward.h>
  6. namespace jvalidate::constraint {
  7. /**
  8. * @brief A constraint on the Array type.
  9. * Given an argument array, every element after applies_after_nth is
  10. * validated against subschema. applies_after_nth is made to be used in
  11. * conjunction with {@see TupleConstraint} and cannot be manually specified.
  12. *
  13. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.2
  14. */
  15. struct AdditionalItemsConstraint {
  16. schema::Node const * subschema = nullptr;
  17. size_t applies_after_nth = 0;
  18. };
  19. /**
  20. * @brief A constraint on the Array type, that counts the number of elements
  21. * in the input array that match subschema and compares that number against
  22. * minimum and maximum. If minimum is unset, then it is equivalent to a minimum
  23. * of 1. If maximum is unset, then it is equivalent to a maximum of INF. As an
  24. * optimization, validation can be stopped after minimum matches if maximum is
  25. * unset.
  26. *
  27. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.3
  28. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.4
  29. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.5
  30. */
  31. struct ContainsConstraint {
  32. schema::Node const * subschema = nullptr;
  33. std::optional<size_t> minimum = std::nullopt;
  34. std::optional<size_t> maximum = std::nullopt;
  35. };
  36. /**
  37. * @brief A constraint on the Array type with the following characteristic(s):
  38. * @code{.cpp}
  39. * arg.size() <= value
  40. * @endcode
  41. *
  42. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.1
  43. */
  44. struct MaxItemsConstraint {
  45. size_t value = 0;
  46. };
  47. /**
  48. * @brief A constraint on the Array type with the following characteristic(s):
  49. * @code{.cpp}
  50. * arg.size() >= value
  51. * @endcode
  52. *
  53. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.2
  54. */
  55. struct MinItemsConstraint {
  56. size_t value = 0;
  57. };
  58. /**
  59. * @brief A constraint on the Array type.
  60. * Given an argument array, the Nth element of that Array is validated against
  61. * the Nth element of items.
  62. *
  63. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.1
  64. */
  65. struct TupleConstraint {
  66. std::vector<schema::Node const *> items;
  67. };
  68. /**
  69. * @brief A constraint on the Array type.
  70. * Given an argument array, every element which is not evaluated by another
  71. * schema {@see jvalidate::Status::Noop} will be validated against subschema.
  72. *
  73. * https://json-schema.org/draft/2020-12/json-schema-core#section-11.2
  74. */
  75. struct UnevaluatedItemsConstraint {
  76. schema::Node const * subschema = nullptr;
  77. };
  78. /**
  79. * @brief A constraint on the Array type that affirms that each element
  80. * of the array is unique.
  81. *
  82. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.3
  83. */
  84. struct UniqueItemsConstraint {};
  85. }