Browse Source

refactor: add description as an annotation

Sam Jaffe 3 weeks ago
parent
commit
3a9daae5eb

+ 1 - 2
include/jvalidate/schema.h

@@ -1,9 +1,7 @@
 #pragma once
 
 #include <memory>
-#include <type_traits>
 #include <unordered_map>
-#include <vector>
 
 #include <jvalidate/adapter.h>
 #include <jvalidate/constraint.h>
@@ -72,6 +70,7 @@ public:
   std::optional<std::string> const & rejects_all() const { return rejects_all_; }
   std::optional<schema::Node const *> reference_schema() const { return reference_; }
 
+  std::string const & description() const { return description_; }
   bool requires_result_context() const { return not post_constraints_.empty(); }
   auto const & constraints() const { return constraints_; }
   auto const & post_constraints() const { return post_constraints_; }

+ 2 - 1
include/jvalidate/validation_result.h

@@ -36,7 +36,7 @@ public:
    * }
    */
   struct LocalResult {
-    bool valid;
+    bool valid = true;
     std::map<std::string, Annotation> errors;
     std::map<std::string, Annotation> annotations;
   };
@@ -239,6 +239,7 @@ private:
   void merge(ValidationResult && result) & {
     for (auto && [where, by_schema] : result.results_) {
       for (auto && [schema_path, local] : by_schema) {
+        results_[where][schema_path].valid &= local.valid;
         results_[where][schema_path].annotations.merge(local.annotations);
         results_[where][schema_path].errors.merge(local.errors);
       }

+ 5 - 2
include/jvalidate/validation_visitor.h

@@ -2,7 +2,6 @@
 
 #include <tuple>
 #include <type_traits>
-#include <unordered_map>
 #include <vector>
 
 #include <jvalidate/compat/enumerate.h>
@@ -113,7 +112,7 @@ public:
       return document.equals(frozen, cfg_.strict_equality);
     };
     if (cons.value->apply(is_equal)) {
-      return result(Status::Accept);
+      return result(Status::Accept, "matches value");
     }
     return result(Status::Reject, cons.value, " was expected");
   }
@@ -628,6 +627,10 @@ public:
       rval = validate_subschema(*ref, document, "$ref");
     }
 
+    if (result_ && !schema_->description().empty()) {
+      result_->annotate(where_, schema_path_, "description", schema_->description());
+    }
+
     detail::Pointer const current_schema = schema_path_;
     for (auto const & [key, p_constraint] : schema_->constraints()) {
       BREAK_EARLY_IF_NO_RESULT_TREE();