File Coverage

blib/lib/JSON/Schema/Error.pm
Criterion Covered Total %
statement 11 23 47.8
branch n/a
condition n/a
subroutine 4 10 40.0
pod 0 6 0.0
total 15 39 38.4


line stmt bran cond sub pod time code
1             package JSON::Schema::Error;
2              
3 9     9   265 use 5.010;
  9         33  
4 9     9   51 use strict;
  9         16  
  9         290  
5 9     9   46 use overload '""' => \&to_string;
  9         16  
  9         178  
6              
7 9     9   514 use JSON::Path;
  9         13  
  9         114  
8              
9             our $AUTHORITY = 'cpan:TOBYINK';
10             our $VERSION = '0.016';
11              
12             sub new
13             {
14 0     0 0   my ($class, $e) = @_;
15 0           return bless $e, $class;
16             }
17              
18             sub property
19             {
20 0     0 0   my ($self) = @_;
21 0           return JSON::Path->new($self->{property});
22             }
23              
24             sub message
25             {
26 0     0 0   my ($self) = @_;
27 0           return $self->{message};
28             }
29              
30             sub title
31             {
32 0     0 0   my ($self) = @_;
33 0           return $self->{title};
34             }
35              
36             sub description
37             {
38 0     0 0   my ($self) = @_;
39 0           return $self->{description};
40             }
41              
42             sub to_string
43             {
44 0     0 0   my ($self) = @_;
45 0           return sprintf("%s: %s", $self->property, $self->message);
46             }
47              
48             1;
49              
50             __END__
51              
52             =head1 NAME
53              
54             JSON::Schema::Error - an error that occurred when checking an instance against a schema
55              
56             =head1 SYNOPSIS
57              
58             my $validator = JSON::Schema->new($schema);
59             my $json = from_json( ... );
60             my $result = $validator->validate($json);
61            
62             if ($result)
63             {
64             print "Valid!\n";
65             }
66             else
67             {
68             print "Errors\n";
69             print " - $_\n" foreach $result->errors;
70             }
71              
72             =head1 DESCRIPTION
73              
74             L<JSON::Schema::Error> is returned by the L<JSON::Schema::Result> C<errors>
75             method. It uses L<overload> to mimic a string. That is:
76              
77             print $error;
78              
79             Will print something sensible.
80              
81             There's also a two methods C<property> and C<message> which return the
82             JSONPath to the node that caused the error, and the error message respectively.
83              
84             Lastly, there's methods C<title> and C<description> which return the title
85             and description of the offending property, as given in the schema.
86              
87             =head1 SEE ALSO
88              
89             L<JSON::Schema>, L<JSON::Schema::Result>.
90              
91             =head1 AUTHOR
92              
93             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
94              
95             =head1 COPYRIGHT AND LICENCE
96              
97             Copyright 2010-2012 Toby Inkster.
98              
99             This module is tri-licensed. It is available under the X11 (a.k.a. MIT)
100             licence; you can also redistribute it and/or modify it under the same
101             terms as Perl itself.
102              
103             =head2 a.k.a. "The MIT Licence"
104              
105             Permission is hereby granted, free of charge, to any person obtaining a copy
106             of this software and associated documentation files (the "Software"), to deal
107             in the Software without restriction, including without limitation the rights
108             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
109             copies of the Software, and to permit persons to whom the Software is
110             furnished to do so, subject to the following conditions:
111              
112             The above copyright notice and this permission notice shall be included in
113             all copies or substantial portions of the Software.
114              
115             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
116             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
117             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
118             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
119             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
120             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
121             THE SOFTWARE.
122              
123             =cut