Skip to content

Commit 639d617

Browse files
authored
Support removing rules by name from the schema transformer (#1590)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 7154ac7 commit 639d617

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaTransformer {
198198
this->rules.emplace(rule->name(), std::move(rule));
199199
}
200200

201+
/// Remove a rule from the bundle
202+
auto remove(const std::string &name) -> bool;
203+
201204
/// Apply the bundle of rules to a schema
202205
auto
203206
apply(JSON &schema, const SchemaWalker &walker,

src/core/jsonschema/transformer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ auto SchemaTransformer::apply(
138138
}
139139
}
140140

141+
auto SchemaTransformer::remove(const std::string &name) -> bool {
142+
return this->rules.erase(name) > 0;
143+
}
144+
141145
auto SchemaTransformer::check(
142146
const JSON &schema, const SchemaWalker &walker,
143147
const SchemaResolver &resolver,

test/jsonschema/jsonschema_transformer_test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,31 @@ TEST(JSONSchema_transformer, check_with_default_dialect) {
539539
EXPECT_EQ(std::get<1>(entries.at(1)), "example_rule_2");
540540
EXPECT_EQ(std::get<2>(entries.at(1)), "Keyword bar is not permitted");
541541
}
542+
543+
TEST(JSONSchema_transformer, remove_rule_by_name) {
544+
sourcemeta::core::SchemaTransformer bundle;
545+
bundle.add<ExampleRule1>();
546+
bundle.add<ExampleRule2>();
547+
548+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
549+
"$schema": "https://json-schema.org/draft/2020-12/schema",
550+
"foo": "bar",
551+
"bar": "baz",
552+
"qux": "xxx"
553+
})JSON");
554+
555+
EXPECT_TRUE(bundle.remove("example_rule_2"));
556+
EXPECT_FALSE(bundle.remove("example_rule_2"));
557+
EXPECT_FALSE(bundle.remove("i_dont_exist"));
558+
559+
bundle.apply(document, sourcemeta::core::schema_official_walker,
560+
sourcemeta::core::schema_official_resolver);
561+
562+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
563+
"$schema": "https://json-schema.org/draft/2020-12/schema",
564+
"bar": "baz",
565+
"qux": "xxx"
566+
})JSON");
567+
568+
EXPECT_EQ(document, expected);
569+
}

0 commit comments

Comments
 (0)