From b28c15adea57bc9556cb00a7611536b5899dc67c Mon Sep 17 00:00:00 2001
From: Patrick Boettcher
Date: Thu, 9 Apr 2020 15:50:25 +0200
Subject: [PATCH] fix #101: throw an exception if JSON-type of schema is not
one of the expected types
---
src/json-validator.cpp | 2 +-
test/issue-101/CMakeLists.txt | 6 ++++++
test/issue-101/instance.json | 1 +
test/issue-101/schema.json | 12 ++++++++++++
4 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 test/issue-101/CMakeLists.txt
create mode 100644 test/issue-101/instance.json
create mode 100644 test/issue-101/schema.json
diff --git a/src/json-validator.cpp b/src/json-validator.cpp
index 54a94c7..8a1e834 100644
--- a/src/json-validator.cpp
+++ b/src/json-validator.cpp
@@ -1163,7 +1163,7 @@ std::shared_ptr schema::make(json &schema,
schema.erase("title");
schema.erase("description");
} else {
- return nullptr; // TODO error/throw? when schema is invalid
+ throw std::invalid_argument("invalid JSON-type for a schema for " + uris[0].to_string() + ", expected: boolean or object");
}
for (auto &uri : uris) { // for all URIs this schema is referenced by
diff --git a/test/issue-101/CMakeLists.txt b/test/issue-101/CMakeLists.txt
new file mode 100644
index 0000000..41ad0c9
--- /dev/null
+++ b/test/issue-101/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_test_simple_schema(Issue::101
+ ${CMAKE_CURRENT_SOURCE_DIR}/schema.json
+ ${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
+set_tests_properties(Issue::101
+ PROPERTIES
+ WILL_FAIL 1)
diff --git a/test/issue-101/instance.json b/test/issue-101/instance.json
new file mode 100644
index 0000000..fb5716b
--- /dev/null
+++ b/test/issue-101/instance.json
@@ -0,0 +1 @@
+{"top": 1}
diff --git a/test/issue-101/schema.json b/test/issue-101/schema.json
new file mode 100644
index 0000000..19fad64
--- /dev/null
+++ b/test/issue-101/schema.json
@@ -0,0 +1,12 @@
+{
+ "$id": "http://xxx.local/schemas/mySchema.json",
+ "$schema": "http://json-schema.org/draft-07/schema#",
+
+ "type": "object",
+ "properties": {
+ "top": {
+ "type": "integer"
+ },
+ "required": ["top"]
+ }
+}