extension_constraint.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <memory>
  3. #include <jvalidate/forward.h>
  4. #include <jvalidate/status.h>
  5. namespace jvalidate::constraint {
  6. /**
  7. * @brief A plugin class that allows the for Domain-Specific Grammers to be
  8. * implemented for a json-schema.
  9. *
  10. * These custom Constraints are implemented by creating two extension classes:
  11. * - A constraint inheriting from {@see jvalidate::ConstraintBase}, which in
  12. * turn inherits from ExtensionConstraint::Impl. This acts as a simple data
  13. * wrapper for describing the Constraint.
  14. * - A visitor inheriting from {@see jvalidate::extension::Visitor}.
  15. *
  16. * https://json-schema.org/draft/2020-12/json-schema-core#section-6.5
  17. */
  18. struct ExtensionConstraint {
  19. public:
  20. struct Impl {
  21. virtual ~Impl() = default;
  22. virtual Status visit(extension::VisitorBase const &) const = 0;
  23. };
  24. public:
  25. /**
  26. * @brief Convenience function for constructing this ExtensionConstraint from
  27. * the concrete underlying extension in the format that is required for use
  28. * with {@see jvalidate::ConstraintFactory}.
  29. *
  30. * @tparam T A concrete type subtyping ExtensionConstraint::Impl
  31. * @tparam Args The argument types for initializing the object
  32. *
  33. * @params args... The constructor arguments for T
  34. *
  35. * @return A unique_ptr to a new ExtensionConstraint of underlying type T,
  36. * wrapped in the Constraint variant type.
  37. */
  38. template <typename T, typename... Args> static std::unique_ptr<Constraint> make(Args &&... args) {
  39. return std::make_unique<Constraint>(
  40. ExtensionConstraint{std::make_unique<T>(std::forward<Args>(args)...)});
  41. }
  42. public:
  43. std::unique_ptr<Impl> pimpl;
  44. };
  45. }