general_constraint.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <cstdlib>
  3. #include <memory>
  4. #include <set>
  5. #include <vector>
  6. #include <jvalidate/forward.h>
  7. namespace jvalidate::constraint {
  8. /**
  9. * @brief A constraint on any JSON document.
  10. * Validates that the given document is validated by the EVERY one of the
  11. * child schemas.
  12. * @code{.py}
  13. * all(c.validate(arg) for c in children)
  14. * @endcode
  15. *
  16. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.1.1
  17. */
  18. struct AllOfConstraint {
  19. std::vector<SubConstraint> children;
  20. };
  21. /**
  22. * @brief A constraint on any JSON document.
  23. * Validates that the given document is validated by the AT LEAST one of the
  24. * child schemas.
  25. * @code{.py}
  26. * any(c.validate(arg) for c in children)
  27. * @endcode
  28. *
  29. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.1.2
  30. */
  31. struct AnyOfConstraint {
  32. std::vector<SubConstraint> children;
  33. };
  34. /**
  35. * @brief A constraint on any JSON document.
  36. * Validates that the input json is exactly the single stored value.
  37. * Stored values can be simple scalars (bool, number, string, null), or
  38. * complicated documents.
  39. * @code{.py}
  40. * arg == value
  41. * @endcode
  42. *
  43. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.3
  44. */
  45. struct ConstConstraint {
  46. std::unique_ptr<adapter::Const const> value;
  47. };
  48. /**
  49. * @brief A constraint on any JSON document.
  50. * Validates that the input json is one of the stored values.
  51. * Stored values can be simple scalars (bool, number, string, null), or
  52. * complicated documents.
  53. * @code{.py}
  54. * arg in enumeration
  55. * @endcode
  56. *
  57. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.2
  58. */
  59. struct EnumConstraint {
  60. std::vector<std::unique_ptr<adapter::Const const>> enumeration;
  61. };
  62. /**
  63. * @brief A constraint on any JSON document.
  64. * Validates that the given document is validated by the EXACTLY one of the
  65. * child schemas.
  66. *
  67. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.1.3
  68. */
  69. struct OneOfConstraint {
  70. std::vector<schema::Node const *> children;
  71. };
  72. /**
  73. * @brief A constraint on any JSON document.
  74. * Validates the document against the if_constraint. If the constraint returns
  75. * Success or Noop, then the document will be validated against the
  76. * then_constraint. Otherwise, it will be validated against the else_constraint.
  77. *
  78. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.2
  79. */
  80. struct ConditionalConstraint {
  81. schema::Node const * if_constraint = nullptr;
  82. schema::Node const * then_constraint = nullptr;
  83. schema::Node const * else_constraint = nullptr;
  84. };
  85. /**
  86. * @brief A constraint on any JSON document.
  87. * Validates that the given document is NOT validated by the child schema.
  88. * A result of {@see jvalidate::Status::Noop} is kept as a Noop.
  89. *
  90. * https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.1.4
  91. */
  92. struct NotConstraint {
  93. SubConstraint child;
  94. };
  95. /**
  96. * @brief A constraint on any JSON document.
  97. * Validates that the type of the document is one of the given types.
  98. *
  99. * https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.1
  100. */
  101. struct TypeConstraint {
  102. std::set<adapter::Type> types;
  103. };
  104. }