parser_context.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <optional>
  3. #include <jvalidate/detail/reference.h>
  4. #include <jvalidate/detail/vocabulary.h>
  5. #include <jvalidate/forward.h>
  6. namespace jvalidate::detail {
  7. template <Adapter A> class ReferenceManager;
  8. template <Adapter A> struct ParserContext {
  9. using Object = decltype(std::declval<A>().as_object());
  10. Schema & root;
  11. A schema;
  12. Vocabulary<A> const * vocab;
  13. ReferenceManager<A> & ref;
  14. std::optional<Object> parent = std::nullopt;
  15. Reference where = {};
  16. Reference dynamic_where = {};
  17. /**
  18. * @brief Obtain the ParserContext for an arbitrary schema location,
  19. * preserving the general context members of the root Schema, Vocabulary,
  20. * and ReferenceManager.
  21. *
  22. * @param new_schema The new schema JSON adapter
  23. * @param new_loc The json pointer to this new_schema relative to its document
  24. * root.
  25. * @param new_dyn The json pointer to this new_schema using dynamic reference
  26. * rules.
  27. * @param parent The parent of this schema, if that parent is an Object.
  28. */
  29. ParserContext rebind(A const & new_schema, Reference const & new_loc, Reference const & new_dyn,
  30. std::optional<Object> parent = std::nullopt) const {
  31. return {root, new_schema, vocab, ref, parent, new_loc, new_dyn};
  32. }
  33. /**
  34. * @brief Obtain the ParserContext for the child schema of the current
  35. * location. Will set the parent context, which is used by some constraints
  36. * like "contains" or "if".
  37. *
  38. * @param child The new child schema
  39. * @param key The object-key to the new schema
  40. */
  41. ParserContext child(A const & child, std::string const & key) const {
  42. return rebind(child, where / key, dynamic_where / key, schema.as_object());
  43. }
  44. /**
  45. * @brief Obtain the ParserContext for the child schema of the current
  46. * location. Will clear the parent schema context.
  47. *
  48. * @param child The new child schema
  49. * @param key The array-index to the new schema
  50. */
  51. ParserContext child(A const & child, size_t index) const {
  52. return rebind(child, where / index, dynamic_where / index);
  53. }
  54. /**
  55. * @brief Obtain the ParserContext for the a child schema with the same parent
  56. * as this context. Called when generating {@see ConditionalConstraint}.
  57. *
  58. * @param key The object-key to the new schema
  59. */
  60. ParserContext neighbor(std::string const & key) const {
  61. return rebind((*parent)[key], where.parent() / key, dynamic_where.parent() / key, parent);
  62. }
  63. schema::Node const * node() const;
  64. schema::Node const * always() const;
  65. schema::Node const * fixed_schema(bool accept) const;
  66. };
  67. }