-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcommand_validate.cc
More file actions
639 lines (579 loc) · 25.4 KB
/
command_validate.cc
File metadata and controls
639 lines (579 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include <sourcemeta/core/io.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonl.h>
#include <sourcemeta/core/jsonschema.h>
#include <sourcemeta/core/yaml.h>
#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/blaze/evaluator.h>
#include <sourcemeta/blaze/output.h>
#include <chrono> // std::chrono
#include <cmath> // std::sqrt
#include <iostream> // std::cerr
#include <string> // std::string
#include <string_view> // std::string_view
#include <utility> // std::as_const
#include "command.h"
#include "configuration.h"
#include "error.h"
#include "input.h"
#include "logger.h"
#include "resolver.h"
#include "utils.h"
namespace {
auto get_precompiled_schema_template_path(
const sourcemeta::core::Options &options)
-> std::optional<std::filesystem::path> {
if (options.contains("template") && !options.at("template").empty()) {
return options.at("template").front();
} else {
return std::nullopt;
}
}
auto get_schema_template(const sourcemeta::core::JSON &bundled,
const sourcemeta::core::SchemaResolver &resolver,
const sourcemeta::core::SchemaFrame &frame,
const std::string &entrypoint_uri,
const bool fast_mode,
const sourcemeta::core::Options &options)
-> sourcemeta::blaze::Template {
const auto precompiled{get_precompiled_schema_template_path(options)};
if (precompiled.has_value()) {
sourcemeta::jsonschema::LOG_VERBOSE(options)
<< "Parsing pre-compiled schema template: "
<< sourcemeta::core::weakly_canonical(precompiled.value()).string()
<< "\n";
const auto schema_template{
sourcemeta::core::read_yaml_or_json(precompiled.value())};
const auto precompiled_result{
sourcemeta::blaze::from_json(schema_template)};
if (precompiled_result.has_value()) {
return precompiled_result.value();
} else {
sourcemeta::jsonschema::LOG_WARNING()
<< "Failed to parse pre-compiled schema template. "
"Compiling from scratch\n";
}
}
return sourcemeta::blaze::compile(
bundled, sourcemeta::core::schema_walker, resolver,
sourcemeta::blaze::default_schema_compiler, frame, entrypoint_uri,
fast_mode ? sourcemeta::blaze::Mode::FastValidation
: sourcemeta::blaze::Mode::Exhaustive);
}
auto parse_loop(const sourcemeta::core::Options &options) -> std::uint64_t {
if (options.contains("loop")) {
return std::stoull(options.at("loop").front().data());
} else {
return 1;
}
}
// validate instance in a loop to measure avg and stdev
auto run_loop(sourcemeta::blaze::Evaluator &evaluator,
const sourcemeta::blaze::Template &schema_template,
const sourcemeta::core::JSON &instance,
const std::string &instance_path, const int64_t instance_index,
const uint64_t loop) -> bool {
const auto iterations = static_cast<double>(loop);
double sum = 0.0, sum2 = 0.0, empty = 0.0;
bool result = true;
// Overhead evaluation, if not to optimize out!
for (auto index = loop; index; index--) {
const auto start{std::chrono::high_resolution_clock::now()};
const auto end{std::chrono::high_resolution_clock::now()};
empty +=
static_cast<double>(
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count()) /
1000.0;
}
empty /= iterations;
// Actual performance loop
for (auto index = loop; index; index--) {
const auto start{std::chrono::high_resolution_clock::now()};
result = evaluator.validate(schema_template, instance);
const auto end{std::chrono::high_resolution_clock::now()};
const auto raw_delay =
static_cast<double>(
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count()) /
1000.0;
const auto delay = std::max(0.0, raw_delay - empty);
sum += delay;
sum2 += delay * delay;
}
// Display json source, result and performance
auto avg = sum / iterations;
auto stdev = loop == 1 ? 0.0 : std::sqrt(sum2 / iterations - avg * avg);
std::cout << instance_path;
if (instance_index >= 0)
std::cout << "[" << instance_index << "]";
std::cout << std::fixed;
std::cout.precision(3);
std::cout << ": " << (result ? "PASS" : "FAIL") << " " << avg << " +- "
<< stdev << " us (" << empty << ")\n";
return result;
}
// Returns false if iteration should stop
auto process_entry(
const sourcemeta::jsonschema::InputJSON &entry,
sourcemeta::blaze::Evaluator &evaluator,
const sourcemeta::blaze::Template &schema_template,
const sourcemeta::jsonschema::CustomResolver &custom_resolver,
const sourcemeta::core::SchemaFrame &frame, bool benchmark,
std::uint64_t benchmark_loop, bool trace, bool fast_mode, bool json_output,
bool schema_from_stdin, const std::filesystem::path &schema_resolution_base,
const sourcemeta::core::Options &options, bool &result) -> bool {
std::ostringstream error;
sourcemeta::blaze::SimpleOutput output{entry.second};
sourcemeta::blaze::TraceOutput trace_output{
sourcemeta::core::schema_walker, custom_resolver,
sourcemeta::jsonschema::trace_callback(entry.positions, std::cout),
sourcemeta::core::empty_weak_pointer, frame};
bool subresult{true};
if (benchmark) {
subresult = run_loop(evaluator, schema_template, entry.second, entry.first,
entry.multidocument
? static_cast<std::int64_t>(entry.index + 1)
: static_cast<std::int64_t>(-1),
benchmark_loop);
if (!subresult) {
error << "error: Schema validation failure\n";
result = false;
}
} else if (trace) {
subresult = evaluator.validate(schema_template, entry.second,
std::ref(trace_output));
} else if (fast_mode) {
subresult = evaluator.validate(schema_template, entry.second);
} else if (!json_output) {
subresult =
evaluator.validate(schema_template, entry.second, std::ref(output));
}
if (benchmark) {
return true;
} else if (trace) {
result = result && subresult;
} else if (json_output) {
if (!entry.multidocument) {
std::cerr << entry.first << "\n";
}
const auto suboutput{sourcemeta::blaze::standard(
evaluator, schema_template, entry.second,
fast_mode ? sourcemeta::blaze::StandardOutput::Flag
: sourcemeta::blaze::StandardOutput::Basic,
entry.positions)};
assert(suboutput.is_object());
assert(suboutput.defines("valid"));
assert(suboutput.at("valid").is_boolean());
sourcemeta::core::prettify(suboutput, std::cout);
std::cout << "\n";
if (!suboutput.at("valid").to_boolean()) {
result = false;
if (entry.multidocument) {
return false;
}
}
} else if (subresult) {
sourcemeta::jsonschema::LOG_VERBOSE(options) << "ok: " << entry.first;
if (entry.multidocument) {
sourcemeta::jsonschema::LOG_VERBOSE(options)
<< " (entry #" << entry.index + 1 << ")";
}
sourcemeta::jsonschema::LOG_VERBOSE(options)
<< "\n matches "
<< (schema_from_stdin
? "/dev/stdin"
: sourcemeta::core::weakly_canonical(schema_resolution_base)
.string())
<< "\n";
sourcemeta::jsonschema::print_annotations(output, options, entry.positions,
std::cerr);
} else {
std::cerr << "fail: " << entry.first;
if (entry.multidocument) {
std::cerr << " (entry #" << entry.index + 1 << ")\n\n";
sourcemeta::core::prettify(entry.second, std::cerr);
std::cerr << "\n\n";
} else {
std::cerr << "\n";
}
std::cerr << error.str();
sourcemeta::jsonschema::print(output, entry.positions, std::cerr);
result = false;
if (entry.multidocument) {
return false;
}
}
return true;
}
} // namespace
auto sourcemeta::jsonschema::validate(const sourcemeta::core::Options &options)
-> void {
if (options.positional().size() < 1) {
throw PositionalArgumentError{
"This command expects a path to a schema and a path to an\n"
"instance to validate against the schema",
"jsonschema validate path/to/schema.json path/to/instance.json"};
}
const auto &schema_path{options.positional().at(0)};
const bool schema_from_stdin = (schema_path == "-");
// Centralized duplicate stdin check for all positional arguments
check_no_duplicate_stdin(options.positional());
if (!schema_from_stdin && std::filesystem::is_directory(schema_path)) {
throw std::filesystem::filesystem_error{
"The input was supposed to be a file but it is a directory",
schema_path, std::make_error_code(std::errc::is_a_directory)};
}
if (options.contains("path") && !options.at("path").empty() &&
options.contains("template") && !options.at("template").empty()) {
throw OptionConflictError{
"The --path option cannot be used with --template"};
}
const auto schema_config_base{schema_from_stdin
? std::filesystem::current_path()
: std::filesystem::path(schema_path)};
const auto schema_resolution_base{
schema_from_stdin ? stdin_path() : std::filesystem::path(schema_path)};
const auto configuration_path{find_configuration(schema_config_base)};
const auto &configuration{
read_configuration(options, configuration_path, schema_config_base)};
const auto dialect{default_dialect(options, configuration)};
auto schema = schema_from_stdin
? read_from_stdin().document
: sourcemeta::core::read_yaml_or_json(schema_path);
std::string path_pointer_string;
if (options.contains("path") && !options.at("path").empty()) {
sourcemeta::core::Pointer pointer;
try {
pointer =
sourcemeta::core::to_pointer(std::string{options.at("path").front()});
} catch (const sourcemeta::core::PointerParseError &) {
throw PositionalArgumentError{
"The JSON Pointer is not valid",
"jsonschema validate path/to/schema.json path/to/instance.json "
"--path '/components/schemas/User'"};
}
path_pointer_string = sourcemeta::core::to_string(pointer);
const auto *const result = sourcemeta::core::try_get(schema, pointer);
if (!result) {
throw PathResolutionError{schema_resolution_base, path_pointer_string};
}
sourcemeta::core::JSON subschema{*result};
schema = std::move(subschema);
if (!sourcemeta::core::is_schema(schema)) {
throw NotSchemaError{schema_from_stdin ? stdin_path()
: schema_resolution_base,
path_pointer_string};
}
} else if (!sourcemeta::core::is_schema(schema)) {
throw NotSchemaError{schema_from_stdin ? stdin_path()
: schema_resolution_base};
}
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
const auto fast_mode{options.contains("fast")};
const auto benchmark{options.contains("benchmark")};
const auto benchmark_loop{parse_loop(options)};
if (benchmark_loop == 0) {
throw OptionConflictError{"The loop number cannot be zero"};
}
const auto trace{options.contains("trace")};
const auto json_output{options.contains("json")};
if (options.contains("entrypoint") && !options.at("entrypoint").empty() &&
options.contains("template") && !options.at("template").empty()) {
throw OptionConflictError{
"The --entrypoint option cannot be used with --template"};
}
const auto schema_default_id{
sourcemeta::jsonschema::default_id(schema_resolution_base)};
const sourcemeta::core::JSON bundled{[&]() {
try {
return sourcemeta::core::bundle(
std::as_const(schema), sourcemeta::core::schema_walker,
custom_resolver, dialect, schema_default_id);
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaKeywordError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaFrameError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaReferenceError &error) {
if (!path_pointer_string.empty()) {
throw PathSchemaReferenceError{
schema_resolution_base, std::string{error.identifier()},
error.location(), path_pointer_string, error.what()};
}
throw sourcemeta::core::FileError<sourcemeta::core::SchemaReferenceError>(
schema_resolution_base, std::string{error.identifier()},
error.location(), error.what());
} catch (const sourcemeta::core::SchemaRelativeMetaschemaResolutionError
&error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaResolutionError>(schema_resolution_base,
error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownDialectError>(schema_resolution_base);
} catch (const sourcemeta::core::SchemaError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaError>(
schema_resolution_base, error.what());
} catch (
const sourcemeta::core::SchemaReferenceObjectResourceError &error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaReferenceObjectResourceError>(
schema_resolution_base, error.identifier());
}
}()};
sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::References};
try {
frame.analyse(bundled, sourcemeta::core::schema_walker, custom_resolver,
dialect, schema_default_id);
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaKeywordError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaFrameError>(
schema_resolution_base, error);
} catch (
const sourcemeta::core::SchemaRelativeMetaschemaResolutionError &error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaResolutionError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownDialectError>(schema_resolution_base);
} catch (const sourcemeta::core::SchemaError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaError>(
schema_resolution_base, error.what());
}
std::string entrypoint_uri{frame.root()};
if (options.contains("entrypoint") && !options.at("entrypoint").empty()) {
try {
entrypoint_uri = resolve_entrypoint(
frame, std::string{options.at("entrypoint").front()});
} catch (const sourcemeta::blaze::CompilerInvalidEntryPoint &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::CompilerInvalidEntryPoint>(schema_resolution_base,
error);
}
}
const auto schema_template{[&]() {
try {
return get_schema_template(bundled, custom_resolver, frame,
entrypoint_uri, fast_mode, options);
} catch (const sourcemeta::blaze::CompilerInvalidEntryPoint &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::CompilerInvalidEntryPoint>(schema_resolution_base,
error);
} catch (const sourcemeta::blaze::CompilerInvalidRegexError &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::CompilerInvalidRegexError>(schema_resolution_base,
error);
} catch (
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaKeywordError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaFrameError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaReferenceError &error) {
if (!path_pointer_string.empty()) {
throw PathSchemaReferenceError{
schema_resolution_base, std::string{error.identifier()},
error.location(), path_pointer_string, error.what()};
}
throw sourcemeta::core::FileError<sourcemeta::core::SchemaReferenceError>(
schema_resolution_base, std::string{error.identifier()},
error.location(), error.what());
} catch (const sourcemeta::core::SchemaRelativeMetaschemaResolutionError
&error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
schema_resolution_base, error);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaResolutionError>(schema_resolution_base,
error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownDialectError>(schema_resolution_base);
} catch (const sourcemeta::core::SchemaVocabularyError &error) {
throw sourcemeta::core::FileError<
sourcemeta::core::SchemaVocabularyError>(
schema_resolution_base, std::string{error.uri()}, error.what());
} catch (const sourcemeta::core::SchemaError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaError>(
schema_resolution_base, error.what());
}
}()};
sourcemeta::blaze::Evaluator evaluator;
bool result{true};
std::vector<std::string_view> instance_arguments;
if (options.positional().size() > 1) {
instance_arguments.assign(options.positional().cbegin() + 1,
options.positional().cend());
}
if (trace && benchmark) {
throw OptionConflictError{
"The `--trace/-t` and `--benchmark/-b` options are mutually exclusive"};
}
if (trace && instance_arguments.size() > 1) {
throw OptionConflictError{
"The `--trace/-t` option is only allowed given a single instance"};
}
if (benchmark && instance_arguments.size() > 1) {
throw OptionConflictError{
"The `--benchmark/-b` option is only allowed given a single instance"};
}
if (instance_arguments.empty()) {
if (trace) {
throw OptionConflictError{
"The `--trace/-t` option is only allowed given a single instance"};
}
if (benchmark) {
throw OptionConflictError{"The `--benchmark/-b` option is only allowed "
"given a single instance"};
}
for (const auto &entry : for_each_json({}, options)) {
if (!process_entry(entry, evaluator, schema_template, custom_resolver,
frame, benchmark, benchmark_loop, trace, fast_mode,
json_output, schema_from_stdin, schema_resolution_base,
options, result)) {
break;
}
}
} else {
for (const auto &instance_path_view : instance_arguments) {
const std::filesystem::path instance_path{instance_path_view};
if (trace && instance_path.extension() == ".jsonl") {
throw OptionConflictError{
"The `--trace/-t` option is only allowed given a single instance"};
}
if (trace && instance_path_view != "-" &&
std::filesystem::is_directory(instance_path)) {
throw OptionConflictError{
"The `--trace/-t` option is only allowed given a single instance"};
}
if (benchmark && instance_path_view != "-" &&
std::filesystem::is_directory(instance_path)) {
throw OptionConflictError{"The `--benchmark/-b` option is only allowed "
"given a single instance"};
}
if (instance_path_view == "-" ||
std::filesystem::is_directory(instance_path) ||
instance_path.extension() == ".jsonl" ||
instance_path.extension() == ".yaml" ||
instance_path.extension() == ".yml") {
for (const auto &entry : for_each_json({instance_path_view}, options)) {
if (!process_entry(entry, evaluator, schema_template, custom_resolver,
frame, benchmark, benchmark_loop, trace, fast_mode,
json_output, schema_from_stdin,
schema_resolution_base, options, result)) {
break;
}
}
} else {
sourcemeta::core::PointerPositionTracker tracker;
auto property_storage = std::make_shared<std::deque<std::string>>();
const bool track_positions{(!fast_mode && !benchmark) || trace};
const auto instance{[&]() -> sourcemeta::core::JSON {
if (track_positions) {
sourcemeta::core::JSON document{sourcemeta::core::JSON{nullptr}};
auto callback = make_position_callback(tracker, property_storage);
sourcemeta::core::read_yaml_or_json(instance_path, document,
callback);
return document;
}
return sourcemeta::core::read_yaml_or_json(instance_path);
}()};
std::ostringstream error;
sourcemeta::blaze::SimpleOutput output{instance};
sourcemeta::blaze::TraceOutput trace_output{
sourcemeta::core::schema_walker, custom_resolver,
trace_callback(tracker, std::cout),
sourcemeta::core::empty_weak_pointer, frame};
bool subresult{true};
if (benchmark) {
subresult =
run_loop(evaluator, schema_template, instance,
instance_path.string(), (int64_t)-1, benchmark_loop);
if (!subresult) {
error << "error: Schema validation failure\n";
result = false;
}
} else if (trace) {
subresult = evaluator.validate(schema_template, instance,
std::ref(trace_output));
} else if (fast_mode) {
subresult = evaluator.validate(schema_template, instance);
} else if (!json_output) {
subresult =
evaluator.validate(schema_template, instance, std::ref(output));
}
if (trace) {
result = result && subresult;
} else if (json_output) {
const auto suboutput{sourcemeta::blaze::standard(
evaluator, schema_template, instance,
fast_mode ? sourcemeta::blaze::StandardOutput::Flag
: sourcemeta::blaze::StandardOutput::Basic,
tracker)};
assert(suboutput.is_object());
assert(suboutput.defines("valid"));
assert(suboutput.at("valid").is_boolean());
if (!suboutput.at("valid").to_boolean()) {
result = false;
}
sourcemeta::core::prettify(suboutput, std::cout);
std::cout << "\n";
} else if (subresult) {
LOG_VERBOSE(options)
<< "ok: "
<< sourcemeta::core::weakly_canonical(instance_path).string()
<< "\n matches "
<< (schema_from_stdin ? "/dev/stdin"
: sourcemeta::core::weakly_canonical(
schema_resolution_base)
.string())
<< "\n";
print_annotations(output, options, tracker, std::cerr);
} else {
std::cerr
<< "fail: "
<< sourcemeta::core::weakly_canonical(instance_path).string()
<< "\n";
std::cerr << error.str();
print(output, tracker, std::cerr);
result = false;
}
}
}
}
if (!result) {
throw Fail{EXIT_EXPECTED_FAILURE};
}
}