Skip to content
Merged
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 @@ -82,6 +82,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/duplicate_examples.h
linter/enum_to_const.h
linter/equal_numeric_bounds_to_const.h
linter/forbid_empty_enum.h
linter/items_array_default.h
linter/items_schema_default.h
linter/multiple_of_default.h
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 @@ -110,6 +110,7 @@ inline auto APPLIES_TO_POINTERS(std::vector<Pointer> &&keywords)
#include "linter/duplicate_examples.h"
#include "linter/enum_to_const.h"
#include "linter/equal_numeric_bounds_to_const.h"
#include "linter/forbid_empty_enum.h"
#include "linter/items_array_default.h"
#include "linter/items_schema_default.h"
#include "linter/multiple_of_default.h"
Expand Down Expand Up @@ -226,6 +227,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<UnsatisfiableMaxContains>();
bundle.add<UnsatisfiableMinProperties>();
bundle.add<EnumToConst>();
bundle.add<ForbidEmptyEnum>();
bundle.add<TopLevelTitle>();
bundle.add<TopLevelDescription>();
bundle.add<TopLevelExamples>();
Expand Down
36 changes: 36 additions & 0 deletions src/extension/alterschema/linter/forbid_empty_enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ForbidEmptyEnum final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::true_type;
ForbidEmptyEnum()
: SchemaTransformRule{"forbid_empty_enum",
"An empty `enum` validates nothing and is "
"unsatisfiable"} {};

[[nodiscard]] auto
condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::core::Vocabularies &vocabularies,
const sourcemeta::core::SchemaFrame &frame,
const sourcemeta::core::SchemaFrame::Location &location,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
ONLY_CONTINUE_IF(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Validation,
Vocabularies::Known::JSON_Schema_2019_09_Validation,
Vocabularies::Known::JSON_Schema_Draft_7,
Vocabularies::Known::JSON_Schema_Draft_6,
Vocabularies::Known::JSON_Schema_Draft_4}) &&
schema.is_object() && !schema.defines("not") &&
schema.defines("enum") && schema.at("enum").is_array() &&
schema.at("enum").empty());
ONLY_CONTINUE_IF(!frame.has_references_through(location.pointer));
return APPLIES_TO_KEYWORDS("enum");
}

auto transform(JSON &schema, const Result &) const -> void override {
schema.at("enum").into(JSON::make_object());
schema.rename("enum", "not");
}
};
Loading
Loading