File Coverage

blib/lib/JSON/Schema/Result.pm
Criterion Covered Total %
statement 15 19 78.9
branch n/a
condition n/a
subroutine 6 7 85.7
pod 0 3 0.0
total 21 29 72.4


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