validation_result.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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>> errors_;
  14. public:
  15. friend std::ostream & operator<<(std::ostream & os, ValidationResult const & result) {
  16. for (auto const & [where, errors] : result.errors_) {
  17. if (errors.size() == 1) {
  18. auto const & [schema_path, message] = *errors.begin();
  19. std::cout << where << ": " << schema_path << ": " << message << "\n";
  20. } else {
  21. std::cout << where << "\n";
  22. for (auto const & [schema_path, message] : errors) {
  23. std::cout << " " << schema_path << ": " << message << "\n";
  24. }
  25. }
  26. }
  27. return os;
  28. }
  29. private:
  30. void add_error(ValidationResult && result) {
  31. for (auto const & [where, errors] : result.errors_) {
  32. for (auto const & [schema_path, message] : errors) {
  33. errors_[where][schema_path] += message;
  34. }
  35. }
  36. }
  37. void add_error(detail::Pointer const & where, detail::Pointer const & schema_path,
  38. std::string const & message) {
  39. errors_[where][schema_path] += message;
  40. }
  41. };
  42. }