line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Validator::JSONSchema; |
2
|
1
|
|
|
1
|
|
69126
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
171434
|
|
|
1
|
|
|
|
|
6
|
|
3
|
1
|
|
|
1
|
|
247
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
292
|
use Catmandu::Util qw(:is :check); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
359
|
|
5
|
1
|
|
|
1
|
|
574
|
use JSON::Validator; |
|
1
|
|
|
|
|
221244
|
|
|
1
|
|
|
|
|
203
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = "0.12"; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with qw(Catmandu::Validator); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has schema => ( |
12
|
|
|
|
|
|
|
is => 'ro', |
13
|
|
|
|
|
|
|
required => 1 |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has _validator => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
lazy => 1, |
19
|
|
|
|
|
|
|
builder => sub { |
20
|
2
|
|
|
2
|
|
24
|
my $validator = JSON::Validator->new; |
21
|
2
|
|
|
|
|
41
|
$validator->schema($_[0]->schema); |
22
|
2
|
|
|
|
|
941
|
$validator; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub validate_data { |
27
|
5
|
|
|
5
|
0
|
3754
|
my($self, $hash)=@_; |
28
|
|
|
|
|
|
|
|
29
|
5
|
|
|
|
|
8
|
my $errors = undef; |
30
|
|
|
|
|
|
|
|
31
|
5
|
|
|
|
|
69
|
my @result = $self->_validator->validate($hash); |
32
|
|
|
|
|
|
|
|
33
|
5
|
100
|
|
|
|
1724
|
if (@result) { |
34
|
|
|
|
|
|
|
$errors = [ |
35
|
|
|
|
|
|
|
map { |
36
|
3
|
|
|
|
|
6
|
+{ |
37
|
3
|
|
|
|
|
9
|
property => $_->path(), |
38
|
|
|
|
|
|
|
message => $_->message() |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
} @result |
41
|
|
|
|
|
|
|
]; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
|
|
|
100
|
$errors; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
__END__ |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Catmandu::Validator::JSONSchema - An implementation of Catmandu::Validator to support JSON Schema |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=begin markdown |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# STATUS |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
[![Build Status](https://travis-ci.org/LibreCat/Catmandu-Validator-JSONSchema.svg?branch=master)](https://travis-ci.org/LibreCat/Catmandu-Validator-JSONSchema) |
59
|
|
|
|
|
|
|
[![Coverage](https://coveralls.io/repos/LibreCat/Catmandu-Validator-JSONSchema/badge.svg?branch=master)](https://coveralls.io/r/LibreCat/Catmandu-Validator-JSONSchema) |
60
|
|
|
|
|
|
|
[![CPANTS kwalitee](http://cpants.cpanauthors.org/dist/Catmandu-Validator-JSONSchema.png)](http://cpants.cpanauthors.org/dist/Catmandu-Validator-JSONSchema) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=end markdown |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 SYNOPSIS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
use Catmandu::Validator::JSONSchema; |
67
|
|
|
|
|
|
|
use Data::Dumper; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $validator = Catmandu::Validator::JSONSchema->new( |
70
|
|
|
|
|
|
|
schema => { |
71
|
|
|
|
|
|
|
"properties"=> { |
72
|
|
|
|
|
|
|
"_id"=> { |
73
|
|
|
|
|
|
|
"type"=> "string", |
74
|
|
|
|
|
|
|
required => 1 |
75
|
|
|
|
|
|
|
}, |
76
|
|
|
|
|
|
|
"title"=> { |
77
|
|
|
|
|
|
|
"type"=> "string", |
78
|
|
|
|
|
|
|
required => 1 |
79
|
|
|
|
|
|
|
}, |
80
|
|
|
|
|
|
|
"author"=> { |
81
|
|
|
|
|
|
|
"type"=> "array", |
82
|
|
|
|
|
|
|
"items" => { |
83
|
|
|
|
|
|
|
"type" => "string" |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
minItems => 1, |
86
|
|
|
|
|
|
|
uniqueItems => 1 |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
}, |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $object = { |
93
|
|
|
|
|
|
|
_id => "rug01:001963301", |
94
|
|
|
|
|
|
|
title => "In gesprek met Etienne Vermeersch : een zoektocht naar waarheid", |
95
|
|
|
|
|
|
|
author => [ |
96
|
|
|
|
|
|
|
"Etienne Vermeersch", |
97
|
|
|
|
|
|
|
"Dirk Verhofstadt" |
98
|
|
|
|
|
|
|
] |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
unless($validator->validate($object)){ |
102
|
|
|
|
|
|
|
print Dumper($validator->last_errors()); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 CONFIGURATION |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=over |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item schema |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
JSON Schema given as hash reference, filename, or URL. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=back |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NOTE |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This module uses L<JSON::Validator>. Therefore the behaviour of your schema |
118
|
|
|
|
|
|
|
should apply to draft 0i4 of the json schema: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
L<http://json-schema.org/draft-04/schema> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
L<http://tools.ietf.org/html/draft-zyp-json-schema-04> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L<Catmandu::Validator> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L<http://json-schema.org> |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Nicolas Franck, C<< <nicolas.franck at ugent.be> >> |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
137
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
138
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |