validation_result.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. #include <map>
  3. #include <ostream>
  4. #include <unordered_set>
  5. #include <vector>
  6. #include <jvalidate/detail/pointer.h>
  7. #include <jvalidate/forward.h>
  8. namespace jvalidate {
  9. class ValidationResult {
  10. public:
  11. template <Adapter A, RegexEngine RE> friend class ValidationVisitor;
  12. private:
  13. std::map<detail::Pointer, std::map<detail::Pointer, std::string>> annotations_;
  14. public:
  15. friend std::ostream & operator<<(std::ostream & os, ValidationResult const & result) {
  16. for (auto const & [where, annotations] : result.annotations_) {
  17. if (annotations.size() == 1) {
  18. auto const & [schema_path, message] = *annotations.begin();
  19. std::cout << where << ": " << schema_path << ": " << message << "\n";
  20. } else {
  21. std::cout << where << "\n";
  22. for (auto const & [schema_path, message] : annotations) {
  23. std::cout << " " << schema_path << ": " << message << "\n";
  24. }
  25. }
  26. }
  27. return os;
  28. }
  29. bool has_annotation(detail::Pointer const & where, detail::Pointer const & schema_path) const {
  30. return annotation(where, schema_path) != nullptr;
  31. }
  32. bool has_annotation(detail::Pointer const & where) const { return annotations_.contains(where); }
  33. std::string const * annotation(detail::Pointer const & where,
  34. detail::Pointer const & schema_path) const {
  35. if (not annotations_.contains(where)) {
  36. return nullptr;
  37. }
  38. auto const & anno = annotations_.at(where);
  39. if (not anno.contains(schema_path)) {
  40. return nullptr;
  41. }
  42. return &anno.at(schema_path);
  43. }
  44. private:
  45. void annotate(ValidationResult && result) {
  46. for (auto const & [where, errors] : result.annotations_) {
  47. for (auto const & [schema_path, message] : errors) {
  48. annotations_[where][schema_path] += message;
  49. }
  50. }
  51. }
  52. void annotate(detail::Pointer const & where, detail::Pointer const & schema_path,
  53. std::string const & message) {
  54. annotations_[where][schema_path] += message;
  55. }
  56. };
  57. }