Browse Source

test: follow schema - not constraint path

Sam Jaffe 1 năm trước cách đây
mục cha
commit
74aeeb5d23
2 tập tin đã thay đổi với 19 bổ sung6 xóa
  1. 4 4
      include/jvalidate/validation_visitor.h
  2. 15 2
      tests/annotation_test.cxx

+ 4 - 4
include/jvalidate/validation_visitor.h

@@ -64,17 +64,17 @@ public:
     adapter::Type const type = document_.type();
     for (adapter::Type const accept : cons.types) {
       if (type == accept) {
-        return note(Status::Accept, "type ", type, " is one of [", cons.types, "]");
+        return note(Status::Accept, "type (", type, ") is one of [", cons.types, "]");
       }
       if (accept == adapter::Type::Number && type == adapter::Type::Integer) {
-        return note(Status::Accept, "type ", type, " is one of [", cons.types, "]");
+        return note(Status::Accept, "type (", type, ") is one of [", cons.types, "]");
       }
       if (accept == adapter::Type::Integer && type == adapter::Type::Number &&
           detail::is_json_integer(document_.as_number())) {
-        return note(Status::Accept, "type ", type, " is one of [", cons.types, "]");
+        return note(Status::Accept, "type (", type, ") is one of [", cons.types, "]");
       }
     }
-    return note(Status::Reject, "type ", type, " is not one of [", cons.types, "]");
+    return note(Status::Reject, "type (", type, ") is not one of [", cons.types, "]");
   }
 
   Status visit(constraint::ExtensionConstraint const & cons) const {

+ 15 - 2
tests/annotation_test.cxx

@@ -38,8 +38,9 @@ Json::Value operator""_json(char const * data, size_t len) {
   return value;
 }
 
-auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc) {
-  jvalidate::Schema const schema(schema_doc, Draft2020_12);
+auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
+              jvalidate::schema::Version version = Draft2020_12) {
+  jvalidate::Schema const schema(schema_doc, version);
 
   jvalidate::ValidationResult result;
   (void)jvalidate::Validator(schema).validate(instance_doc, &result);
@@ -109,6 +110,18 @@ TEST(Annotation, NotSchemaFlipsAnnotationRule) {
   EXPECT_THAT(result, AnnotationAt(""_jptr, "/not/minimum"_jptr, "6 >= 5"));
 }
 
+TEST(Annotation, PathFollowsSchemaNotObject) {
+  auto const schema = R"({
+    "disallow": "string"
+  })"_json;
+
+  auto const instance = R"("hello")"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance, Draft03);
+
+  EXPECT_THAT(result, AnnotationAt(""_jptr, "/disallow"_jptr, "type (string) is one of [string]"));
+}
+
 int main(int argc, char ** argv) {
   testing::InitGoogleMock(&argc, argv);
   return RUN_ALL_TESTS();