Skip to content

Commit f2a1c96

Browse files
authored
Implement a is_empty_schema utility function (#1833)
Fixes: #1832 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 8fac8ca commit f2a1c96

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/core/jsonschema/include/sourcemeta/core/jsonschema.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ auto schema_keyword_priority(std::string_view keyword,
104104
SOURCEMETA_CORE_JSONSCHEMA_EXPORT
105105
auto is_schema(const JSON &schema) -> bool;
106106

107+
/// @ingroup jsonschema
108+
///
109+
/// This function returns true if the given JSON instance is a schema
110+
/// semantically equivalent to the empty schema. For example:
111+
///
112+
/// ```cpp
113+
/// #include <sourcemeta/core/json.h>
114+
/// #include <sourcemeta/core/jsonschema.h>
115+
/// #include <cassert>
116+
///
117+
/// const sourcemeta::core::JSON document{true};
118+
/// assert(sourcemeta::core::is_empty_schema(document));
119+
/// ```
120+
SOURCEMETA_CORE_JSONSCHEMA_EXPORT
121+
auto is_empty_schema(const JSON &schema) -> bool;
122+
107123
/// @ingroup jsonschema
108124
///
109125
/// This function returns the URI identifier of the given schema, if any. For

src/core/jsonschema/jsonschema.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ auto sourcemeta::core::is_schema(const sourcemeta::core::JSON &schema) -> bool {
1313
return schema.is_object() || schema.is_boolean();
1414
}
1515

16+
// TODO: Make this function detect schemas only using identifier/comment
17+
// keywords, etc
18+
auto sourcemeta::core::is_empty_schema(const sourcemeta::core::JSON &schema)
19+
-> bool {
20+
return (schema.is_boolean() && schema.to_boolean()) ||
21+
(schema.is_object() && schema.empty());
22+
}
23+
1624
namespace {
1725

1826
auto id_keyword_guess(const sourcemeta::core::JSON &schema)

test/jsonschema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sourcemeta_googletest(NAMESPACE sourcemeta PROJECT core NAME jsonschema
1212
jsonschema_identify_draft0_test.cc
1313
jsonschema_identify_test.cc
1414
jsonschema_is_schema_test.cc
15+
jsonschema_is_empty_schema_test.cc
1516
jsonschema_frame_2020_12_test.cc
1617
jsonschema_frame_2019_09_test.cc
1718
jsonschema_frame_draft7_test.cc
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <sourcemeta/core/json.h>
4+
#include <sourcemeta/core/jsonschema.h>
5+
6+
TEST(JSONSchema, is_empty_schema_true) {
7+
const sourcemeta::core::JSON document{true};
8+
EXPECT_TRUE(sourcemeta::core::is_empty_schema(document));
9+
}
10+
11+
TEST(JSONSchema, is_empty_schema_false) {
12+
const sourcemeta::core::JSON document{false};
13+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
14+
}
15+
16+
TEST(JSONSchema, is_empty_schema_int) {
17+
const sourcemeta::core::JSON document{5};
18+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
19+
}
20+
21+
TEST(JSONSchema, is_empty_schema_real) {
22+
const sourcemeta::core::JSON document{5.3};
23+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
24+
}
25+
26+
TEST(JSONSchema, is_empty_schema_null) {
27+
const sourcemeta::core::JSON document{nullptr};
28+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
29+
}
30+
31+
TEST(JSONSchema, is_empty_schema_string) {
32+
const sourcemeta::core::JSON document{"foo"};
33+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
34+
}
35+
36+
TEST(JSONSchema, is_empty_schema_array) {
37+
const sourcemeta::core::JSON document =
38+
sourcemeta::core::parse_json("[ 1, 2, 3 ]");
39+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
40+
}
41+
42+
TEST(JSONSchema, is_empty_schema_empty_object) {
43+
const sourcemeta::core::JSON document = sourcemeta::core::parse_json("{}");
44+
EXPECT_TRUE(sourcemeta::core::is_empty_schema(document));
45+
}
46+
47+
TEST(JSONSchema, is_empty_schema_non_empty_object) {
48+
const sourcemeta::core::JSON document =
49+
sourcemeta::core::parse_json("{ \"type\": true }");
50+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
51+
}
52+
53+
TEST(JSONSchema, is_empty_schema_unknown_keyword) {
54+
const sourcemeta::core::JSON document =
55+
sourcemeta::core::parse_json("{ \"x-foo\": true }");
56+
EXPECT_FALSE(sourcemeta::core::is_empty_schema(document));
57+
}

0 commit comments

Comments
 (0)