Skip to content

[WIP] feat(alterschema): [linter] create rule to remove empty fragment #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/if_without_then_else.h
linter/max_contains_without_contains.h
linter/min_contains_without_contains.h
linter/modern_official_dialect_with_empty_fragment.h
linter/then_without_if.h)

if(SOURCEMETA_CORE_INSTALL)
Expand Down
2 changes: 2 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static auto every_item_is_boolean(const T &container) -> bool {
#include "linter/maximum_real_for_integer.h"
#include "linter/min_contains_without_contains.h"
#include "linter/minimum_real_for_integer.h"
#include "linter/modern_official_dialect_with_empty_fragment.h"
#include "linter/multiple_of_default.h"
#include "linter/non_applicable_type_specific_keywords.h"
#include "linter/pattern_properties_default.h"
Expand Down Expand Up @@ -113,6 +114,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
bundle.add<DuplicateEnumValues>();
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
bundle.add<ModernOfficialDialectWithEmptyFragment>();
bundle.add<ExclusiveMaximumNumberAndMaximum>();
bundle.add<ExclusiveMinimumNumberAndMinimum>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ModernOfficialDialectWithEmptyFragment final
: public SchemaTransformRule {
public:
ModernOfficialDialectWithEmptyFragment()
: SchemaTransformRule{
"modern_official_dialect_with_empty_fragment",
"The official dialect URI of Draft 2019-09 and newer versions must "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"The official dialect URI of Draft 2019-09 and newer versions must "
"The official dialect URI of 2019-09 and newer versions must "

We don't say draft anymore for the newer ones!

"not contain the empty fragment"} {};

[[nodiscard]] auto condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::core::Vocabularies &,
const sourcemeta::core::SchemaFrame &,
const sourcemeta::core::SchemaFrame::Location &,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
if (!schema.is_object() || !schema.defines("$schema") ||
!schema.at("$schema").is_string()) {
return false;
}

const auto &schema_value = schema.at("$schema").to_string();
return (
schema_value == "https://json-schema.org/draft/2019-09/schema#" ||
schema_value == "https://json-schema.org/draft/2019-09/hyper-schema#" ||
schema_value == "https://json-schema.org/draft/2020-12/schema#" ||
schema_value == "https://json-schema.org/draft/2020-12/hyper-schema#");
}

auto transform(sourcemeta::core::JSON &schema) const -> void override {
auto schema_value = schema.at("$schema").to_string();
schema_value.pop_back();
schema.at("$schema").into(sourcemeta::core::JSON{std::move(schema_value)});
}
};