Skip to content

Commit ebdaaa0

Browse files
committed
check for then and add tests related to it
Signed-off-by: karan-palan <karanpalan007@gmail.com>
1 parent da42406 commit ebdaaa0

File tree

4 files changed

+203
-7
lines changed

4 files changed

+203
-7
lines changed

src/extension/alterschema/linter/else_false.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,35 @@ class ElseFalse final : public SchemaTransformRule {
1515
"https://json-schema.org/draft/2019-09/vocab/applicator",
1616
"http://json-schema.org/draft-07/schema#"}) &&
1717
schema.is_object() && schema.defines("if") &&
18-
schema.defines("else") && schema.at("else").is_boolean() &&
19-
!schema.at("else").to_boolean();
18+
schema.defines("else") && is_schema(schema.at("else")) &&
19+
schema.at("else").is_boolean() && !schema.at("else").to_boolean() &&
20+
(!schema.defines("then") ||
21+
(schema.at("then").is_boolean() && schema.at("then").to_boolean()));
2022
}
2123

2224
auto transform(JSON &schema) const -> void override {
23-
const auto if_schema = schema.at("if");
25+
auto if_schema = schema.at("if");
2426
schema.erase("if");
2527
schema.erase("else");
28+
if (schema.defines("then")) {
29+
schema.erase("then");
30+
}
2631
if (if_schema.is_object()) {
27-
for (const auto &entry : if_schema.as_object()) {
32+
for (auto &entry : if_schema.as_object()) {
2833
if (!schema.defines(entry.first)) {
29-
schema.assign(entry.first, entry.second);
34+
schema.assign(entry.first, std::move(entry.second));
3035
}
3136
}
32-
} else if (if_schema.is_boolean()) {
33-
schema = if_schema;
37+
} else if (if_schema.is_boolean() && !if_schema.to_boolean()) {
38+
auto metadata = JSON::make_object();
39+
if (schema.defines("$schema")) {
40+
metadata.assign("$schema", std::move(schema.at("$schema")));
41+
}
42+
if (schema.defines("$id")) {
43+
metadata.assign("$id", std::move(schema.at("$id")));
44+
}
45+
schema = std::move(metadata);
46+
schema.assign("not", JSON::make_object());
3447
}
3548
}
3649
};

test/alterschema/alterschema_lint_2019_09_test.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,3 +2376,64 @@ TEST(AlterSchema_lint_2019_09, else_false_1) {
23762376

23772377
EXPECT_EQ(document, expected);
23782378
}
2379+
2380+
TEST(AlterSchema_lint_2019_09, else_false_2) {
2381+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2382+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2383+
"if": {
2384+
"properties": {
2385+
"flag": {
2386+
"const": true
2387+
}
2388+
}
2389+
},
2390+
"then": true,
2391+
"else": false
2392+
})JSON");
2393+
2394+
LINT_AND_FIX_FOR_READABILITY(document);
2395+
2396+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2397+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2398+
"properties": {
2399+
"flag": {
2400+
"const": true
2401+
}
2402+
}
2403+
})JSON");
2404+
2405+
EXPECT_EQ(document, expected);
2406+
}
2407+
2408+
TEST(AlterSchema_lint_2019_09, else_false_3) {
2409+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2410+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2411+
"if": true,
2412+
"else": false
2413+
})JSON");
2414+
2415+
LINT_AND_FIX_FOR_READABILITY(document);
2416+
2417+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2418+
"$schema": "https://json-schema.org/draft/2019-09/schema"
2419+
})JSON");
2420+
2421+
EXPECT_EQ(document, expected);
2422+
}
2423+
2424+
TEST(AlterSchema_lint_2019_09, else_false_4) {
2425+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2426+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2427+
"if": false,
2428+
"else": false
2429+
})JSON");
2430+
2431+
LINT_AND_FIX_FOR_READABILITY(document);
2432+
2433+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2434+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2435+
"not": {}
2436+
})JSON");
2437+
2438+
EXPECT_EQ(document, expected);
2439+
}

test/alterschema/alterschema_lint_2020_12_test.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,3 +2610,64 @@ TEST(AlterSchema_lint_2020_12, else_false_1) {
26102610

26112611
EXPECT_EQ(document, expected);
26122612
}
2613+
2614+
TEST(AlterSchema_lint_2020_12, else_false_2) {
2615+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2616+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2617+
"if": {
2618+
"properties": {
2619+
"flag": {
2620+
"const": true
2621+
}
2622+
}
2623+
},
2624+
"then": true,
2625+
"else": false
2626+
})JSON");
2627+
2628+
LINT_AND_FIX_FOR_READABILITY(document);
2629+
2630+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2631+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2632+
"properties": {
2633+
"flag": {
2634+
"const": true
2635+
}
2636+
}
2637+
})JSON");
2638+
2639+
EXPECT_EQ(document, expected);
2640+
}
2641+
2642+
TEST(AlterSchema_lint_2020_12, else_false_3) {
2643+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2644+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2645+
"if": true,
2646+
"else": false
2647+
})JSON");
2648+
2649+
LINT_AND_FIX_FOR_READABILITY(document);
2650+
2651+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2652+
"$schema": "https://json-schema.org/draft/2020-12/schema"
2653+
})JSON");
2654+
2655+
EXPECT_EQ(document, expected);
2656+
}
2657+
2658+
TEST(AlterSchema_lint_2020_12, else_false_4) {
2659+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2660+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2661+
"if": false,
2662+
"else": false
2663+
})JSON");
2664+
2665+
LINT_AND_FIX_FOR_READABILITY(document);
2666+
2667+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2668+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2669+
"not": {}
2670+
})JSON");
2671+
2672+
EXPECT_EQ(document, expected);
2673+
}

test/alterschema/alterschema_lint_draft7_test.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,3 +1917,64 @@ TEST(AlterSchema_lint_draft7, else_false_1) {
19171917

19181918
EXPECT_EQ(document, expected);
19191919
}
1920+
1921+
TEST(AlterSchema_lint_draft7, else_false_2) {
1922+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1923+
"$schema": "http://json-schema.org/draft-07/schema#",
1924+
"if": {
1925+
"properties": {
1926+
"flag": {
1927+
"const": true
1928+
}
1929+
}
1930+
},
1931+
"then": true,
1932+
"else": false
1933+
})JSON");
1934+
1935+
LINT_AND_FIX_FOR_READABILITY(document);
1936+
1937+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1938+
"$schema": "http://json-schema.org/draft-07/schema#",
1939+
"properties": {
1940+
"flag": {
1941+
"const": true
1942+
}
1943+
}
1944+
})JSON");
1945+
1946+
EXPECT_EQ(document, expected);
1947+
}
1948+
1949+
TEST(AlterSchema_lint_draft7, else_false_3) {
1950+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1951+
"$schema": "http://json-schema.org/draft-07/schema#",
1952+
"if": true,
1953+
"else": false
1954+
})JSON");
1955+
1956+
LINT_AND_FIX_FOR_READABILITY(document);
1957+
1958+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1959+
"$schema": "http://json-schema.org/draft-07/schema#"
1960+
})JSON");
1961+
1962+
EXPECT_EQ(document, expected);
1963+
}
1964+
1965+
TEST(AlterSchema_lint_draft7, else_false_4) {
1966+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1967+
"$schema": "http://json-schema.org/draft-07/schema#",
1968+
"if": false,
1969+
"else": false
1970+
})JSON");
1971+
1972+
LINT_AND_FIX_FOR_READABILITY(document);
1973+
1974+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
1975+
"$schema": "http://json-schema.org/draft-07/schema#",
1976+
"not": {}
1977+
})JSON");
1978+
1979+
EXPECT_EQ(document, expected);
1980+
}

0 commit comments

Comments
 (0)