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