Skip to content

Commit 676ef6e

Browse files
add_schema now dies with a Result rather than listref of Errors
This is more consistent with other calls, and is easier to catch in applications because $@ is directly serializable (via the TO_JSON method) rather than having to call TO_JSON on each list element.
1 parent 6ffc3ee commit 676ef6e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Changes

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Revision history for JSON-Schema-Draft201909
22

33
{{$NEXT}}
44
- further improvements to the "terse" output format
5+
- add_schema will now die with a Result object rather than a
6+
listref of Error objects, when the document contains errors.
57

68
0.018 2020-12-07 18:22:07Z
79
- now can correctly evaluate schemas containing unevaluatedItems,

lib/JSON/Schema/Draft201909.pm

+14-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use JSON::MaybeXS;
1414
use Syntax::Keyword::Try 0.11;
1515
use Carp qw(croak carp);
1616
use List::Util 1.55 qw(pairs first uniqint);
17-
use Ref::Util 0.100 qw(is_ref is_plain_hashref is_plain_coderef is_plain_arrayref);
17+
use Ref::Util 0.100 qw(is_ref is_plain_hashref is_plain_coderef);
1818
use Mojo::URL;
1919
use Safe::Isa;
2020
use Path::Tiny;
@@ -100,7 +100,11 @@ sub add_schema {
100100
_evaluator => $self,
101101
);
102102

103-
die [ $document->errors ] if $document->has_errors;
103+
die JSON::Schema::Draft201909::Result->new(
104+
output_format => $self->output_format,
105+
result => 0,
106+
errors => [ $document->errors ],
107+
) if $document->has_errors;
104108

105109
if (not grep $_->{document} == $document, $self->_resource_values) {
106110
my $schema_content = $document->_serialized_schema
@@ -256,8 +260,8 @@ sub evaluate {
256260
$result = $self->_eval($data, $schema, $state);
257261
}
258262
catch {
259-
if (is_plain_arrayref($@)) {
260-
push @{$state->{errors}}, @{$@};
263+
if ($@->$_isa('JSON::Schema::Draft201909::Result')) {
264+
return $@;
261265
}
262266
elsif ($@->$_isa('JSON::Schema::Draft201909::Error')) {
263267
push @{$state->{errors}}, $@;
@@ -458,6 +462,8 @@ sub _get_or_load_resource {
458462
my $file = path(dist_dir('JSON-Schema-Draft201909'), $local_filename);
459463
my $schema = $self->_json_decoder->decode($file->slurp_raw);
460464
my $document = JSON::Schema::Draft201909::Document->new(schema => $schema, _evaluator => $self);
465+
466+
# this should be caught by the try/catch in evaluate()
461467
die [ $document->errors ] if $document->has_errors;
462468

463469
# we have already performed the appropriate collision checks, so we bypass them here
@@ -675,11 +681,10 @@ You B<MUST> call C<add_schema> for any external resources that a schema may refe
675681
before calling L</evaluate>, other than the standard metaschemas which are loaded from a local cache
676682
as needed.
677683
678-
Returns the L<JSON::Schema::Draft201909::Document> that contains the added schema, or C<undef>
679-
if the resource could not be found.
680-
681-
May die with a listref of L<JSON::Schema::Draft201909::Error> object(s), if there were errors in the
682-
document.
684+
Returns C<undef> if the resource could not be found;
685+
if there were errors in the document, will die with a L<JSON::Schema::Draft201909::Result> object
686+
containing the errors;
687+
otherwise returns the L<JSON::Schema::Draft201909::Document> that contains the added schema.
683688
684689
=head2 get
685690

0 commit comments

Comments
 (0)