validation_result.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #pragma once
  2. #include <map>
  3. #include <ostream>
  4. #include <utility>
  5. #include <variant>
  6. #include <vector>
  7. #include <jvalidate/detail/pointer.h>
  8. #include <jvalidate/forward.h>
  9. namespace jvalidate {
  10. class ValidationResult {
  11. public:
  12. // Only allow ValidationVisitor to construct the elements of a validation result
  13. template <Adapter, RegexEngine> friend class ValidationVisitor;
  14. using DocPointer = detail::Pointer;
  15. using SchemaPointer = detail::Pointer;
  16. using Annotation = std::variant<std::string, std::vector<std::string>>;
  17. /**
  18. * @brief The result info at any given (DocPointer, SchemaPointer) path.
  19. * The key for errors/annotations represents the leaf element of SchemaPointer
  20. * instead of including it in the map.
  21. *
  22. * This allows better locality of error info. For example:
  23. * {
  24. * "valid": false,
  25. * "evaluationPath": "/definitions/EvenPercent",
  26. * "instanceLocation": "/foo/bar/percentages/2",
  27. * "errors": {
  28. * "max": "105 > 100",
  29. * "multipleOf": "105 is not a multiple of 2"
  30. * }
  31. * }
  32. */
  33. struct LocalResult {
  34. bool valid;
  35. std::map<std::string, Annotation> errors;
  36. std::map<std::string, Annotation> annotations;
  37. };
  38. struct indent {
  39. indent(int i) : i(i) {}
  40. friend std::ostream & operator<<(std::ostream & os, indent id) {
  41. while (id.i-- > 0)
  42. os << " ";
  43. return os;
  44. }
  45. int i;
  46. };
  47. private:
  48. bool valid_;
  49. std::map<DocPointer, std::map<SchemaPointer, LocalResult>> results_;
  50. public:
  51. /**
  52. * @brief Writes this object to an osteam in the list format as described in
  53. * https://json-schema.org/blog/posts/interpreting-output
  54. * This means that the json-schema for a ValidationResult looks like this:
  55. * {
  56. * "$defs": {
  57. * "Pointer": {
  58. * "format": "json-pointer",
  59. * "type": "string"
  60. * },
  61. * "Annotation": {
  62. * "items": { "type": "string" },
  63. * "type": [ "string", "array" ]
  64. * }
  65. * },
  66. * "properties": {
  67. * "valid": { "type": "boolean" },
  68. * "details": {
  69. * "items": {
  70. * "properties": {
  71. * "valid": { "type": "boolean" },
  72. * "evaluationPath": { "$ref": "#/$defs/Pointer" },
  73. * "instanceLocation": { "$ref": "#/$defs/Pointer" },
  74. * "annotations": { "$ref": "#/$defs/Annotation" },
  75. * "errors": { "$ref": "#/$defs/Annotation" }
  76. * }
  77. * "type": "object"
  78. * },
  79. * "type": "array"
  80. * }
  81. * }
  82. * "type": "object"
  83. * }
  84. */
  85. friend std::ostream & operator<<(std::ostream & os, ValidationResult const & result) {
  86. char const * div = "\n";
  87. os << "{\n" << indent(1) << R"("valid": )" << (result.valid_ ? "true" : "false") << ',' << '\n';
  88. os << indent(1) << R"("details": [)";
  89. for (auto const & [doc_path, by_schema] : result.results_) {
  90. for (auto const & [schema_path, local] : by_schema) {
  91. os << std::exchange(div, ",\n") << indent(2) << '{' << '\n';
  92. os << indent(3) << R"("valid": )" << (local.valid ? "true" : "false") << ',' << '\n';
  93. os << indent(3) << R"("evaluationPath": ")" << schema_path << '"' << ',' << '\n';
  94. os << indent(3) << R"("instanceLocation": ")" << doc_path << '"';
  95. print(os, local.annotations, "annotations", 3);
  96. print(os, local.errors, "errors", 3);
  97. os << '\n' << indent(2) << '}';
  98. }
  99. }
  100. return os << '\n' << indent(1) << ']' << '\n' << '}';
  101. }
  102. static void print(std::ostream & os, std::map<std::string, Annotation> const & named,
  103. std::string_view name, int const i) {
  104. if (named.empty()) {
  105. return;
  106. }
  107. os << ',' << '\n';
  108. os << indent(i) << '"' << name << '"' << ':' << ' ' << '{' << '\n';
  109. for (auto const & [key, anno] : named) {
  110. os << indent(i + 1) << '"' << key << '"' << ':' << ' ';
  111. if (auto const * str = std::get_if<0>(&anno)) {
  112. os << '"' << *str << '"';
  113. } else if (auto const * vec = std::get_if<1>(&anno)) {
  114. os << '[';
  115. char const * div = "\n";
  116. for (size_t n = 0; n < vec->size(); ++n) {
  117. os << std::exchange(div, ",\n") << indent(i + 2) << '"' << vec->at(n) << '"';
  118. }
  119. os << '\n' << indent(i + 1) << ']';
  120. }
  121. os << '\n';
  122. }
  123. os << indent(i) << '}';
  124. }
  125. /**
  126. * @brief Are there any validation details associated with the given document
  127. * location and schema section.
  128. *
  129. * @param where A path into the document being validated
  130. * @param schema_path The schema path (not counting the leaf element that
  131. * actually evaluates the document)
  132. *
  133. * @return true if the schema path has produced an annotation or error for the
  134. * document path
  135. */
  136. bool has(detail::Pointer const & where, detail::Pointer const & schema_path) const {
  137. return has(where) && results_.at(where).contains(schema_path);
  138. }
  139. /**
  140. * @brief Are there any validation details associated with the given document
  141. * location
  142. *
  143. * @param where A path into the document being validated
  144. *
  145. * @return true if any rule has produced an annotation or error for the
  146. * document path
  147. */
  148. bool has(detail::Pointer const & where) const { return results_.contains(where); }
  149. /**
  150. * @brief Extracts the annotation for requested document and schema location, if it exists
  151. *
  152. * @param where A path into the document being validated
  153. * @param schema_path The schema path, without its leaf element
  154. * @param name The leaf schema path (i.e. the rule being evaluated).
  155. * @pre name.empty() == schema_path.empty()
  156. *
  157. * @returns An Annotation for the given path info provided, or nullptr if no annotation exists
  158. */
  159. Annotation const * annotation(detail::Pointer const & where, detail::Pointer const & schema_path,
  160. std::string const & name) const {
  161. if (not results_.contains(where)) {
  162. return nullptr;
  163. }
  164. auto const & by_schema = results_.at(where);
  165. if (not by_schema.contains(schema_path)) {
  166. return nullptr;
  167. }
  168. auto const & local = by_schema.at(schema_path);
  169. if (not local.annotations.contains(name)) {
  170. return nullptr;
  171. }
  172. return &local.annotations.at(name);
  173. }
  174. /**
  175. * @brief Extracts the error for requested document and schema location, if it exists
  176. *
  177. * @param where A path into the document being validated
  178. * @param schema_path The schema path, without its leaf element
  179. * @param name The leaf schema path (i.e. the rule being evaluated).
  180. * @pre name.empty() == schema_path.empty()
  181. *
  182. * @returns An Annotation for the given path info provided, or nullptr if no annotation exists
  183. */
  184. Annotation const * error(detail::Pointer const & where, detail::Pointer const & schema_path,
  185. std::string const & name) const {
  186. if (not results_.contains(where)) {
  187. return nullptr;
  188. }
  189. auto const & by_schema = results_.at(where);
  190. if (not by_schema.contains(schema_path)) {
  191. return nullptr;
  192. }
  193. auto const & local = by_schema.at(schema_path);
  194. if (not local.errors.contains(name)) {
  195. return nullptr;
  196. }
  197. return &local.errors.at(name);
  198. }
  199. private:
  200. /**
  201. * @brief Transfer the contents of another ValidationResult into this one using
  202. * {@see std::map::merge} to transfer the data minimizing the need for copy/move.
  203. *
  204. * @param result The ValidationResult being consumed
  205. */
  206. void merge(ValidationResult && result) & {
  207. for (auto && [where, by_schema] : result.results_) {
  208. for (auto && [schema_path, local] : by_schema) {
  209. results_[where][schema_path].annotations.merge(local.annotations);
  210. results_[where][schema_path].errors.merge(local.errors);
  211. }
  212. }
  213. }
  214. /**
  215. * @brief Declare that the document is accepted/rejected by the given schema
  216. *
  217. * @param where A path into the document being validated
  218. * @param schema_path The schema path
  219. * @param valid Is this location valid according to the schema
  220. */
  221. void valid(detail::Pointer const & where, detail::Pointer const & schema_path, bool valid) {
  222. if (has(where, schema_path)) {
  223. results_[where][schema_path].valid = valid;
  224. }
  225. if (where.empty() && schema_path.empty()) {
  226. valid_ = valid;
  227. }
  228. }
  229. /**
  230. * @brief Attach an error message for part of the document.
  231. * Because of the existance of things like "not" schemas, error() can also be
  232. * called to add an Annotation for a gate that is passed, but was within a
  233. * "not" schema.
  234. *
  235. * @param where A path into the document being validated
  236. * @param schema_path The schema path, without its leaf element
  237. * @param name The leaf schema path (i.e. the rule being evaluated).
  238. * @pre name.empty() == schema_path.empty()
  239. * @param message The annotation(s) being placed as an error
  240. */
  241. void error(detail::Pointer const & where, detail::Pointer const & schema_path,
  242. std::string const & name, Annotation message) {
  243. if (std::visit([](auto const & v) { return v.empty(); }, message)) {
  244. return;
  245. }
  246. results_[where][schema_path].errors.emplace(name, std::move(message));
  247. }
  248. /**
  249. * @brief Attach some contextual annotations for part of the document
  250. *
  251. * @param where A path into the document being validated
  252. * @param schema_path The schema path, without its leaf element
  253. * @param name The leaf schema path (i.e. the rule being evaluated).
  254. * @pre name.empty() == schema_path.empty()
  255. * @param message The annotation(s) being placed for context
  256. */
  257. void annotate(detail::Pointer const & where, detail::Pointer const & schema_path,
  258. std::string const & name, Annotation message) {
  259. if (std::visit([](auto const & v) { return v.empty(); }, message)) {
  260. return;
  261. }
  262. results_[where][schema_path].annotations.emplace(name, std::move(message));
  263. }
  264. };
  265. }