File Coverage

blib/lib/JSON/Schema/AsType/Draft3.pm
Criterion Covered Total %
statement 65 66 98.4
branch 15 16 93.7
condition n/a
subroutine 20 20 100.0
pod n/a
total 100 102 98.0


line stmt bran cond sub pod time code
1             package JSON::Schema::AsType::Draft3;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Role processing draft3 JSON Schema
4             $JSON::Schema::AsType::Draft3::VERSION = '0.4.4';
5              
6 5     5   223058 use strict;
  5         36  
  5         249  
7 5     5   27 use warnings;
  5         12  
  5         408  
8              
9 5     5   514 use Moose::Role;
  5         470028  
  5         53  
10              
11 5     5   36517 use Type::Utils;
  5         47788  
  5         99  
12 5     5   10372 use Scalar::Util qw/ looks_like_number /;
  5         13  
  5         438  
13 5     5   32 use List::Util qw/ reduce pairmap pairs /;
  5         11  
  5         495  
14 5     5   532 use List::MoreUtils qw/ any all none uniq zip /;
  5         8385  
  5         56  
15              
16 5     5   10699 use JSON::Schema::AsType;
  5         18  
  5         220  
17              
18 5     5   33 use JSON;
  5         9  
  5         45  
19              
20 5     5   3406 use JSON::Schema::AsType::Draft3::Types '-all';
  5         26  
  5         79  
21 5     5   70881 use Types::Standard 'Optional';
  5         13  
  5         66  
22              
23             with 'JSON::Schema::AsType::Draft4' => {
24             -excludes => [qw/ _keyword_properties _keyword_required _keyword_type /]
25             };
26              
27             sub _keyword_properties {
28 64     64   245 my( $self, $properties ) = @_;
29              
30             my @props = pairmap { {
31 324     324   29331 my $schema = $self->sub_schema($b);
  324         1545  
32 324         220864 my $p = $schema->type;
33 324 100       4731 $p = Optional[$p] unless $b->{required};
34 324         137606 $a => $p
35 64         866 }} %$properties;
36              
37 64         4879 return Properties[@props];
38             }
39              
40             sub _keyword_disallow {
41 3     3   20 Disallow[ $_[0]->_keyword_type($_[1]) ];
42             }
43              
44              
45             sub _keyword_extends {
46 3     3   9 my( $self, $extends ) = @_;
47              
48 3 100       14 my @extends = ref $extends eq 'ARRAY' ? @$extends : ( $extends );
49              
50 3         10 return Extends[ map { $self->sub_schema($_)->type } @extends];
  4         172  
51             }
52              
53             sub _keyword_type {
54 564     564   1546 my( $self, $struct_type ) = @_;
55              
56             my %type_map = map {
57 564         2892 lc $_->name => $_
  3948         32209  
58             } Integer, Boolean, Number, String, Null, Object, Array;
59              
60 564 100       24822 unless( $self->strict_string ) {
61 9         63 $type_map{number} = LaxNumber;
62 9         41 $type_map{integer} = LaxInteger;
63 9         34 $type_map{string} = LaxString;
64             }
65              
66              
67 564 100       6126 return if $struct_type eq 'any';
68              
69 553 100       2959 return $type_map{$struct_type} if $type_map{$struct_type};
70              
71 98 50       255 if( my @types = eval { @$struct_type } ) {
  98         595  
72 98 100   107   718 return reduce { $a | $b } map { ref $_ ? $self->sub_schema($_)->type : $self->_keyword_type($_) } @types;
  107         11171  
  205         3676  
73             }
74              
75 0         0 die "unknown type '$struct_type'";
76             }
77              
78             sub _keyword_divisibleBy {
79 3     3   8 my( $self, $divisibleBy ) = @_;
80              
81 3         16 DivisibleBy[$divisibleBy];
82             }
83              
84             sub _keyword_dependencies {
85 13     13   39 my( $self, $dependencies ) = @_;
86              
87             return Dependencies[
88 13 100   23   161 pairmap { $a => ref $b eq 'HASH' ? $self->sub_schema($b)->type : $b } %$dependencies
  23         152  
89             ];
90              
91             }
92              
93             JSON::Schema::AsType->new(
94             draft_version => '3',
95             uri => "http${_}://json-schema.org/draft-03/schema",
96             schema => from_json <<'END_JSON' )->type for '', 's';
97             {
98             "$schema": "http://json-schema.org/draft-03/schema#",
99             "id": "http://json-schema.org/draft-03/schema#",
100             "type": "object",
101            
102             "properties": {
103             "type": {
104             "type": [ "string", "array" ],
105             "items": {
106             "type": [ "string", { "$ref": "#" } ]
107             },
108             "uniqueItems": true,
109             "default": "any"
110             },
111            
112             "properties": {
113             "type": "object",
114             "additionalProperties": { "$ref": "#" },
115             "default": {}
116             },
117            
118             "patternProperties": {
119             "type": "object",
120             "additionalProperties": { "$ref": "#" },
121             "default": {}
122             },
123            
124             "additionalProperties": {
125             "type": [ { "$ref": "#" }, "boolean" ],
126             "default": {}
127             },
128            
129             "items": {
130             "type": [ { "$ref": "#" }, "array" ],
131             "items": { "$ref": "#" },
132             "default": {}
133             },
134            
135             "additionalItems": {
136             "type": [ { "$ref": "#" }, "boolean" ],
137             "default": {}
138             },
139            
140             "required": {
141             "type": "boolean",
142             "default": false
143             },
144            
145             "dependencies": {
146             "type": "object",
147             "additionalProperties": {
148             "type": [ "string", "array", { "$ref": "#" } ],
149             "items": {
150             "type": "string"
151             }
152             },
153             "default": {}
154             },
155            
156             "minimum": {
157             "type": "number"
158             },
159            
160             "maximum": {
161             "type": "number"
162             },
163            
164             "exclusiveMinimum": {
165             "type": "boolean",
166             "default": false
167             },
168            
169             "exclusiveMaximum": {
170             "type": "boolean",
171             "default": false
172             },
173            
174             "minItems": {
175             "type": "integer",
176             "minimum": 0,
177             "default": 0
178             },
179            
180             "maxItems": {
181             "type": "integer",
182             "minimum": 0
183             },
184            
185             "uniqueItems": {
186             "type": "boolean",
187             "default": false
188             },
189            
190             "pattern": {
191             "type": "string",
192             "format": "regex"
193             },
194            
195             "minLength": {
196             "type": "integer",
197             "minimum": 0,
198             "default": 0
199             },
200            
201             "maxLength": {
202             "type": "integer"
203             },
204            
205             "enum": {
206             "type": "array",
207             "minItems": 1,
208             "uniqueItems": true
209             },
210            
211             "default": {
212             "type": "any"
213             },
214            
215             "title": {
216             "type": "string"
217             },
218            
219             "description": {
220             "type": "string"
221             },
222            
223             "format": {
224             "type": "string"
225             },
226            
227             "divisibleBy": {
228             "type": "number",
229             "minimum": 0,
230             "exclusiveMinimum": true,
231             "default": 1
232             },
233            
234             "disallow": {
235             "type": [ "string", "array" ],
236             "items": {
237             "type": [ "string", { "$ref": "#" } ]
238             },
239             "uniqueItems": true
240             },
241            
242             "extends": {
243             "type": [ { "$ref": "#" }, "array" ],
244             "items": { "$ref": "#" },
245             "default": {}
246             },
247            
248             "id": {
249             "type": "string",
250             "format": "uri"
251             },
252            
253             "$ref": {
254             "type": "string",
255             "format": "uri"
256             },
257            
258             "$schema": {
259             "type": "string",
260             "format": "uri"
261             }
262             },
263            
264             "dependencies": {
265             "exclusiveMinimum": "minimum",
266             "exclusiveMaximum": "maximum"
267             },
268            
269             "default": {}
270             }
271             END_JSON
272              
273             1;
274              
275             __END__
276              
277             =pod
278              
279             =encoding UTF-8
280              
281             =head1 NAME
282              
283             JSON::Schema::AsType::Draft3 - Role processing draft3 JSON Schema
284              
285             =head1 VERSION
286              
287             version 0.4.4
288              
289             =head1 DESCRIPTION
290              
291             This role is not intended to be used directly. It is used internally
292             by L<JSON::Schema::AsType> objects.
293              
294             Importing this module auto-populate the Draft3 schema in the
295             L<JSON::Schema::AsType> schema cache.
296              
297             =head1 AUTHOR
298              
299             Yanick Champoux <yanick@babyl.dyndns.org>
300              
301             =head1 COPYRIGHT AND LICENSE
302              
303             This software is copyright (c) 2024 by Yanick Champoux.
304              
305             This is free software; you can redistribute it and/or modify it under
306             the same terms as the Perl 5 programming language system itself.
307              
308             =cut