Skip to content

Commit 09db576

Browse files
authored
Fix linter rules to compile using the current default identifier (#469)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 59f4845 commit 09db576

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

src/linter/valid_default.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ auto ValidDefault::condition(
4040
location.dialect)};
4141
const auto schema_template{compile(subschema, walker, resolver,
4242
this->compiler_, Mode::FastValidation,
43-
location.dialect)};
43+
location.dialect, location.base)};
4444
const auto &instance{schema.at("default")};
4545
Evaluator evaluator;
4646
const std::string ref{"$ref"};

src/linter/valid_examples.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ auto ValidExamples::condition(
4848
location.dialect)};
4949
const auto schema_template{compile(subschema, walker, resolver,
5050
this->compiler_, Mode::FastValidation,
51-
location.dialect)};
51+
location.dialect, location.base)};
5252

5353
Evaluator evaluator;
5454
std::size_t cursor{0};

test/linter/linter_valid_default_test.cc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,79 @@ TEST(Linter, valid_default_9) {
351351

352352
EXPECT_EQ(schema, expected);
353353
}
354+
355+
TEST(Linter, valid_default_10) {
356+
sourcemeta::core::SchemaTransformer bundle;
357+
bundle.add<sourcemeta::blaze::ValidDefault>(
358+
sourcemeta::blaze::default_schema_compiler);
359+
360+
auto schema{sourcemeta::core::parse_json(R"JSON({
361+
"$schema": "https://json-schema.org/draft/2020-12/schema",
362+
"items": { "default": 10, "$ref": "ref.json" }
363+
})JSON")};
364+
365+
auto resolver = [](const std::string_view identifier)
366+
-> std::optional<sourcemeta::core::JSON> {
367+
if (identifier == "https://example.com/ref.json") {
368+
return sourcemeta::core::parse_json(R"JSON({
369+
"$schema": "https://json-schema.org/draft/2020-12/schema",
370+
"$id": "https://example.com/ref.json",
371+
"type": "integer"
372+
})JSON");
373+
}
374+
375+
return sourcemeta::core::schema_official_resolver(identifier);
376+
};
377+
378+
const auto result = bundle.apply(
379+
schema, sourcemeta::core::schema_official_walker, resolver,
380+
transformer_callback_error, std::nullopt, "https://example.com/root");
381+
382+
EXPECT_TRUE(result);
383+
384+
const auto expected{sourcemeta::core::parse_json(R"JSON({
385+
"$schema": "https://json-schema.org/draft/2020-12/schema",
386+
"items": { "default": 10, "$ref": "ref.json" }
387+
})JSON")};
388+
389+
EXPECT_EQ(schema, expected);
390+
}
391+
392+
TEST(Linter, valid_default_11) {
393+
sourcemeta::core::SchemaTransformer bundle;
394+
bundle.add<sourcemeta::blaze::ValidDefault>(
395+
sourcemeta::blaze::default_schema_compiler);
396+
397+
auto schema{sourcemeta::core::parse_json(R"JSON({
398+
"$schema": "https://json-schema.org/draft/2020-12/schema",
399+
"default": 10,
400+
"$ref": "ref.json"
401+
})JSON")};
402+
403+
auto resolver = [](const std::string_view identifier)
404+
-> std::optional<sourcemeta::core::JSON> {
405+
if (identifier == "https://example.com/ref.json") {
406+
return sourcemeta::core::parse_json(R"JSON({
407+
"$schema": "https://json-schema.org/draft/2020-12/schema",
408+
"$id": "https://example.com/ref.json",
409+
"type": "integer"
410+
})JSON");
411+
}
412+
413+
return sourcemeta::core::schema_official_resolver(identifier);
414+
};
415+
416+
const auto result = bundle.apply(
417+
schema, sourcemeta::core::schema_official_walker, resolver,
418+
transformer_callback_error, std::nullopt, "https://example.com/root");
419+
420+
EXPECT_TRUE(result);
421+
422+
const auto expected{sourcemeta::core::parse_json(R"JSON({
423+
"$schema": "https://json-schema.org/draft/2020-12/schema",
424+
"default": 10,
425+
"$ref": "ref.json"
426+
})JSON")};
427+
428+
EXPECT_EQ(schema, expected);
429+
}

test/linter/linter_valid_examples_test.cc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,79 @@ TEST(Linter, valid_examples_10) {
390390

391391
EXPECT_EQ(schema, expected);
392392
}
393+
394+
TEST(Linter, valid_examples_11) {
395+
sourcemeta::core::SchemaTransformer bundle;
396+
bundle.add<sourcemeta::blaze::ValidDefault>(
397+
sourcemeta::blaze::default_schema_compiler);
398+
399+
auto schema{sourcemeta::core::parse_json(R"JSON({
400+
"$schema": "https://json-schema.org/draft/2020-12/schema",
401+
"items": { "examples": [ 10 ], "$ref": "ref.json" }
402+
})JSON")};
403+
404+
auto resolver = [](const std::string_view identifier)
405+
-> std::optional<sourcemeta::core::JSON> {
406+
if (identifier == "https://example.com/ref.json") {
407+
return sourcemeta::core::parse_json(R"JSON({
408+
"$schema": "https://json-schema.org/draft/2020-12/schema",
409+
"$id": "https://example.com/ref.json",
410+
"type": "integer"
411+
})JSON");
412+
}
413+
414+
return sourcemeta::core::schema_official_resolver(identifier);
415+
};
416+
417+
const auto result = bundle.apply(
418+
schema, sourcemeta::core::schema_official_walker, resolver,
419+
transformer_callback_error, std::nullopt, "https://example.com/root");
420+
421+
EXPECT_TRUE(result);
422+
423+
const auto expected{sourcemeta::core::parse_json(R"JSON({
424+
"$schema": "https://json-schema.org/draft/2020-12/schema",
425+
"items": { "examples": [ 10 ], "$ref": "ref.json" }
426+
})JSON")};
427+
428+
EXPECT_EQ(schema, expected);
429+
}
430+
431+
TEST(Linter, valid_examples_12) {
432+
sourcemeta::core::SchemaTransformer bundle;
433+
bundle.add<sourcemeta::blaze::ValidDefault>(
434+
sourcemeta::blaze::default_schema_compiler);
435+
436+
auto schema{sourcemeta::core::parse_json(R"JSON({
437+
"$schema": "https://json-schema.org/draft/2020-12/schema",
438+
"items": { "examples": [ 10 ], "$ref": "ref.json" },
439+
"$ref": "ref.json"
440+
})JSON")};
441+
442+
auto resolver = [](const std::string_view identifier)
443+
-> std::optional<sourcemeta::core::JSON> {
444+
if (identifier == "https://example.com/ref.json") {
445+
return sourcemeta::core::parse_json(R"JSON({
446+
"$schema": "https://json-schema.org/draft/2020-12/schema",
447+
"$id": "https://example.com/ref.json",
448+
"type": "integer"
449+
})JSON");
450+
}
451+
452+
return sourcemeta::core::schema_official_resolver(identifier);
453+
};
454+
455+
const auto result = bundle.apply(
456+
schema, sourcemeta::core::schema_official_walker, resolver,
457+
transformer_callback_error, std::nullopt, "https://example.com/root");
458+
459+
EXPECT_TRUE(result);
460+
461+
const auto expected{sourcemeta::core::parse_json(R"JSON({
462+
"$schema": "https://json-schema.org/draft/2020-12/schema",
463+
"items": { "examples": [ 10 ], "$ref": "ref.json" },
464+
"$ref": "ref.json"
465+
})JSON")};
466+
467+
EXPECT_EQ(schema, expected);
468+
}

0 commit comments

Comments
 (0)