Skip to content

Commit b644ad6

Browse files
authored
Create rule that adds missing hash to metaschema uri's of drafts before 2019-09 (#1823)
Signed-off-by: karan-palan <karanpalan007@gmail.com>
1 parent 7a18592 commit b644ad6

10 files changed

+193
-0
lines changed

src/extension/alterschema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
2929
linter/dependent_required_tautology.h
3030
linter/equal_numeric_bounds_to_enum.h
3131
linter/maximum_real_for_integer.h
32+
linter/draft_official_dialect_without_empty_fragment.h
3233
linter/minimum_real_for_integer.h
3334
linter/single_type_array.h
3435
linter/enum_to_const.h

src/extension/alterschema/alterschema.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ contains_any(const Vocabularies &container,
4444
#include "linter/dependencies_property_tautology.h"
4545
#include "linter/dependent_required_default.h"
4646
#include "linter/dependent_required_tautology.h"
47+
#include "linter/draft_official_dialect_without_empty_fragment.h"
4748
#include "linter/duplicate_allof_branches.h"
4849
#include "linter/duplicate_anyof_branches.h"
4950
#include "linter/duplicate_enum_values.h"
@@ -84,6 +85,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
8485
// Common rules that apply to all modes
8586
bundle.add<ContentMediaTypeWithoutEncoding>();
8687
bundle.add<ContentSchemaWithoutMediaType>();
88+
bundle.add<DraftOfficialDialectWithoutEmptyFragment>();
8789
bundle.add<NonApplicableTypeSpecificKeywords>();
8890
bundle.add<UnnecessaryAllOfWrapperModern>();
8991
bundle.add<UnnecessaryAllOfWrapperDraft>();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class DraftOfficialDialectWithoutEmptyFragment final
2+
: public SchemaTransformRule {
3+
public:
4+
DraftOfficialDialectWithoutEmptyFragment()
5+
: SchemaTransformRule{"draft_official_dialect_without_empty_fragment",
6+
"The official dialect URI of Draft 7 and older "
7+
"versions must contain the empty fragment"} {};
8+
9+
[[nodiscard]] auto condition(const sourcemeta::core::JSON &schema,
10+
const sourcemeta::core::JSON &,
11+
const sourcemeta::core::Vocabularies &,
12+
const sourcemeta::core::SchemaFrame &,
13+
const sourcemeta::core::SchemaFrame::Location &,
14+
const sourcemeta::core::SchemaWalker &,
15+
const sourcemeta::core::SchemaResolver &) const
16+
-> sourcemeta::core::SchemaTransformRule::Result override {
17+
if (!schema.is_object() || !schema.defines("$schema") ||
18+
!schema.at("$schema").is_string()) {
19+
return false;
20+
}
21+
22+
const auto &schema_value = schema.at("$schema").to_string();
23+
return (schema_value == "http://json-schema.org/draft-07/schema" ||
24+
schema_value == "http://json-schema.org/draft-07/hyper-schema" ||
25+
schema_value == "http://json-schema.org/draft-06/schema" ||
26+
schema_value == "http://json-schema.org/draft-06/hyper-schema" ||
27+
schema_value == "http://json-schema.org/draft-04/schema" ||
28+
schema_value == "http://json-schema.org/draft-04/hyper-schema" ||
29+
schema_value == "http://json-schema.org/draft-03/schema" ||
30+
schema_value == "http://json-schema.org/draft-03/hyper-schema" ||
31+
schema_value == "http://json-schema.org/draft-02/schema" ||
32+
schema_value == "http://json-schema.org/draft-02/hyper-schema" ||
33+
schema_value == "http://json-schema.org/draft-01/schema" ||
34+
schema_value == "http://json-schema.org/draft-01/hyper-schema" ||
35+
schema_value == "http://json-schema.org/draft-00/schema" ||
36+
schema_value == "http://json-schema.org/draft-00/hyper-schema");
37+
}
38+
39+
auto transform(sourcemeta::core::JSON &schema) const -> void override {
40+
auto schema_value = schema.at("$schema").to_string();
41+
schema_value += "#";
42+
schema.at("$schema").into(sourcemeta::core::JSON{std::move(schema_value)});
43+
}
44+
};

test/alterschema/alterschema_lint_draft0_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,19 @@ TEST(AlterSchema_lint_draft0, boolean_true_1) {
192192

193193
EXPECT_EQ(document, expected);
194194
}
195+
196+
TEST(AlterSchema_lint_draft0, draft_official_dialect_without_empty_fragment_1) {
197+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
198+
"$schema": "http://json-schema.org/draft-00/schema",
199+
"type": "string"
200+
})JSON");
201+
202+
LINT_AND_FIX_FOR_READABILITY(document);
203+
204+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
205+
"$schema": "http://json-schema.org/draft-00/schema#",
206+
"type": "string"
207+
})JSON");
208+
209+
EXPECT_EQ(document, expected);
210+
}

test/alterschema/alterschema_lint_draft1_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,19 @@ TEST(AlterSchema_lint_draft1, equal_numeric_bounds_to_enum_2) {
522522

523523
EXPECT_EQ(document, expected);
524524
}
525+
526+
TEST(AlterSchema_lint_draft1, draft_official_dialect_without_empty_fragment_1) {
527+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
528+
"$schema": "http://json-schema.org/draft-01/schema",
529+
"type": "string"
530+
})JSON");
531+
532+
LINT_AND_FIX_FOR_READABILITY(document);
533+
534+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
535+
"$schema": "http://json-schema.org/draft-01/schema#",
536+
"type": "string"
537+
})JSON");
538+
539+
EXPECT_EQ(document, expected);
540+
}

test/alterschema/alterschema_lint_draft2_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,19 @@ TEST(AlterSchema_lint_draft2, equal_numeric_bounds_to_enum_2) {
522522

523523
EXPECT_EQ(document, expected);
524524
}
525+
526+
TEST(AlterSchema_lint_draft2, draft_official_dialect_without_empty_fragment_1) {
527+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
528+
"$schema": "http://json-schema.org/draft-02/schema",
529+
"type": "string"
530+
})JSON");
531+
532+
LINT_AND_FIX_FOR_READABILITY(document);
533+
534+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
535+
"$schema": "http://json-schema.org/draft-02/schema#",
536+
"type": "string"
537+
})JSON");
538+
539+
EXPECT_EQ(document, expected);
540+
}

test/alterschema/alterschema_lint_draft3_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,19 @@ TEST(AlterSchema_lint_draft3, equal_numeric_bounds_to_enum_2) {
579579

580580
EXPECT_EQ(document, expected);
581581
}
582+
583+
TEST(AlterSchema_lint_draft3, draft_official_dialect_without_empty_fragment_1) {
584+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
585+
"$schema": "http://json-schema.org/draft-03/schema",
586+
"type": "string"
587+
})JSON");
588+
589+
LINT_AND_FIX_FOR_READABILITY(document);
590+
591+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
592+
"$schema": "http://json-schema.org/draft-03/schema#",
593+
"type": "string"
594+
})JSON");
595+
596+
EXPECT_EQ(document, expected);
597+
}

test/alterschema/alterschema_lint_draft4_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,19 @@ TEST(AlterSchema_lint_draft4, unnecessary_allof_wrapper_properties_1) {
921921

922922
EXPECT_EQ(document, expected);
923923
}
924+
925+
TEST(AlterSchema_lint_draft4, draft_official_dialect_without_empty_fragment_1) {
926+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
927+
"$schema": "http://json-schema.org/draft-04/schema",
928+
"type": "string"
929+
})JSON");
930+
931+
LINT_AND_FIX_FOR_READABILITY(document);
932+
933+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
934+
"$schema": "http://json-schema.org/draft-04/schema#",
935+
"type": "string"
936+
})JSON");
937+
938+
EXPECT_EQ(document, expected);
939+
}

test/alterschema/alterschema_lint_draft6_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,19 @@ TEST(AlterSchema_lint_draft6, unnecessary_allof_wrapper_properties_1) {
11831183

11841184
EXPECT_EQ(document, expected);
11851185
}
1186+
1187+
TEST(AlterSchema_lint_draft6, draft_official_dialect_without_empty_fragment_1) {
1188+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1189+
"$schema": "http://json-schema.org/draft-06/schema",
1190+
"type": "string"
1191+
})JSON");
1192+
1193+
LINT_AND_FIX_FOR_READABILITY(document);
1194+
1195+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1196+
"$schema": "http://json-schema.org/draft-06/schema#",
1197+
"type": "string"
1198+
})JSON");
1199+
1200+
EXPECT_EQ(document, expected);
1201+
}

test/alterschema/alterschema_lint_draft7_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,3 +1283,53 @@ TEST(AlterSchema_lint_draft7, unnecessary_allof_wrapper_properties_1) {
12831283

12841284
EXPECT_EQ(document, expected);
12851285
}
1286+
1287+
TEST(AlterSchema_lint_draft7, draft_official_dialect_without_empty_fragment_1) {
1288+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1289+
"$schema": "http://json-schema.org/draft-07/schema",
1290+
"type": "string"
1291+
})JSON");
1292+
1293+
LINT_AND_FIX_FOR_READABILITY(document);
1294+
1295+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1296+
"$schema": "http://json-schema.org/draft-07/schema#",
1297+
"type": "string"
1298+
})JSON");
1299+
1300+
EXPECT_EQ(document, expected);
1301+
}
1302+
1303+
TEST(AlterSchema_lint_draft7,
1304+
draft_official_dialect_without_empty_fragment_hyper) {
1305+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1306+
"$schema": "http://json-schema.org/draft-07/hyper-schema",
1307+
"type": "string"
1308+
})JSON");
1309+
1310+
LINT_AND_FIX_FOR_READABILITY(document);
1311+
1312+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1313+
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
1314+
"type": "string"
1315+
})JSON");
1316+
1317+
EXPECT_EQ(document, expected);
1318+
}
1319+
1320+
TEST(AlterSchema_lint_draft7,
1321+
draft_official_dialect_without_empty_fragment_already_has_hash) {
1322+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1323+
"$schema": "http://json-schema.org/draft-07/schema#",
1324+
"type": "string"
1325+
})JSON");
1326+
1327+
LINT_AND_FIX_FOR_READABILITY(document);
1328+
1329+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1330+
"$schema": "http://json-schema.org/draft-07/schema#",
1331+
"type": "string"
1332+
})JSON");
1333+
1334+
EXPECT_EQ(document, expected);
1335+
}

0 commit comments

Comments
 (0)