Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT blaze NAME alterschema
linter/properties_default.h
linter/property_names_default.h
linter/property_names_type_default.h
linter/require_schema_declaration.h
linter/simple_properties_identifiers.h
linter/then_empty.h
linter/title_description_equal.h
Expand Down
2 changes: 2 additions & 0 deletions src/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ auto WALK_UP_IN_PLACE_APPLICATORS(const JSON &root, const SchemaFrame &frame,
#include "linter/properties_default.h"
#include "linter/property_names_default.h"
#include "linter/property_names_type_default.h"
#include "linter/require_schema_declaration.h"
#include "linter/simple_properties_identifiers.h"
#include "linter/title_description_equal.h"
#include "linter/title_trailing_period.h"
Expand Down Expand Up @@ -482,6 +483,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<UnsatisfiableMinProperties>();
bundle.add<EnumToConst>();
bundle.add<ForbidEmptyEnum>();
bundle.add<RequireSchemaDeclaration>();
bundle.add<TopLevelTitle>();
bundle.add<TopLevelDescription>();
bundle.add<TopLevelExamples>();
Expand Down
32 changes: 32 additions & 0 deletions src/alterschema/linter/require_schema_declaration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class RequireSchemaDeclaration final : public SchemaTransformRule {
public:
using mutates = std::false_type;
using reframe_after_transform = std::false_type;
RequireSchemaDeclaration()
: SchemaTransformRule{
"require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword"} {};
[[nodiscard]] auto
condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::blaze::Vocabularies &vocabularies,
const sourcemeta::blaze::SchemaFrame &,
const sourcemeta::blaze::SchemaFrame::Location &location,
const sourcemeta::blaze::SchemaWalker &,
const sourcemeta::blaze::SchemaResolver &, const bool) const
-> SchemaTransformRule::Result override {
ONLY_CONTINUE_IF(location.pointer.empty());
ONLY_CONTINUE_IF(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Core,
Vocabularies::Known::JSON_Schema_2019_09_Core,
Vocabularies::Known::JSON_Schema_Draft_7,
Vocabularies::Known::JSON_Schema_Draft_6,
Vocabularies::Known::JSON_Schema_Draft_4,
Vocabularies::Known::JSON_Schema_Draft_3,
Vocabularies::Known::JSON_Schema_Draft_3_Hyper}));
ONLY_CONTINUE_IF(schema.is_object());
ONLY_CONTINUE_IF(!schema.defines("$schema"));
Comment thread
AcEKaycgR marked this conversation as resolved.
return APPLIES_TO_KEYWORDS("$schema");
}
};
44 changes: 44 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5982,3 +5982,47 @@ TEST(AlterSchema_lint_2019_09,
EXPECT_EQ(sourcemeta::core::to_string(outcome.locations.at(1)),
"/patternProperties/[[:digit:]]");
}

TEST(AlterSchema_lint_2019_09, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2019_09, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"https://json-schema.org/draft/2019-09/schema");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, but because the linter will analyse the schema, this ONLY ever works if you pass a default dialect, right? If so, we should be able to transform to fix?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though on the other side, I know people use defaultDialect in jsonschema.json for big collection of schemas, and that setup will start failing given this, so I'm not sure. I think the case we want to protect is where we don't even know the dialect of a schema, but sounds like that's not possible at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that the linter can only fire this rule when it already knows the dialect. Making it fixable makes sense then ,the fix would insert $schema with the resolved dialect URI. On the defaultDialect concern, I agree it could be noisy for users who manage a collection of schemas that way, but with auto-fix it becomes a one-shot cleanup. I can update the rule to be fixable if that is the direction you want.


EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
44 changes: 44 additions & 0 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13583,3 +13583,47 @@ TEST(AlterSchema_lint_2020_12, embedded_custom_metaschema) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2020_12, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"https://json-schema.org/draft/2020-12/schema");

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
42 changes: 42 additions & 0 deletions test/alterschema/alterschema_lint_draft3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3436,3 +3436,45 @@ TEST(AlterSchema_lint_draft3,
EXPECT_EQ(sourcemeta::core::to_string(outcome.locations.at(1)),
"/patternProperties/[[:digit:]]");
}

TEST(AlterSchema_lint_draft3, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
"title": "My Schema",
"description": "A schema",
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_draft3, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"http://json-schema.org/draft-03/schema#");

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
42 changes: 42 additions & 0 deletions test/alterschema/alterschema_lint_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3704,3 +3704,45 @@ TEST(AlterSchema_lint_draft4,
EXPECT_EQ(sourcemeta::core::to_string(outcome.locations.at(1)),
"/patternProperties/[[:digit:]]");
}

TEST(AlterSchema_lint_draft4, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "My Schema",
"description": "A schema",
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_draft4, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"http://json-schema.org/draft-04/schema#");

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
44 changes: 44 additions & 0 deletions test/alterschema/alterschema_lint_draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3812,3 +3812,47 @@ TEST(AlterSchema_lint_draft6,
EXPECT_EQ(sourcemeta::core::to_string(outcome.locations.at(1)),
"/patternProperties/[[:digit:]]");
}

TEST(AlterSchema_lint_draft6, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_draft6, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"http://json-schema.org/draft-06/schema#");

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
44 changes: 44 additions & 0 deletions test/alterschema/alterschema_lint_draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5352,3 +5352,47 @@ TEST(AlterSchema_lint_draft7,
EXPECT_EQ(sourcemeta::core::to_string(outcome.locations.at(1)),
"/patternProperties/[[:digit:]]");
}

TEST(AlterSchema_lint_draft7, require_schema_declaration_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_draft7, require_schema_declaration_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"title": "My Schema",
"description": "A schema",
"examples": [ "hello" ],
"type": "string"
})JSON");

std::vector<std::tuple<sourcemeta::core::Pointer, std::string, std::string,
sourcemeta::blaze::SchemaTransformRule::Result, bool>>
traces;
sourcemeta::blaze::SchemaTransformer bundle;
sourcemeta::blaze::add(bundle, sourcemeta::blaze::AlterSchemaMode::Linter);
const auto result = bundle.check(
document, sourcemeta::blaze::schema_walker, alterschema_test_resolver,
[&traces](const auto &pointer, const auto &name, const auto &message,
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"http://json-schema.org/draft-07/schema#");

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "require_schema_declaration",
"A schema should declare its dialect using the `$schema` "
"keyword",
false);
}
Loading