| 1 | package JSON::Schema::AsType::Draft3; | ||||||||||
| 2 | 5 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | # ABSTRACT: Role processing draft3 JSON Schema | ||||||||||
| 4 | 5 | $JSON::Schema::AsType::Draft3::VERSION = '0.4.4'; | |||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 5 | ▶ | use Type::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use Scalar::Util qw/ looks_like_number /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 5 | ▶ | use List::Util qw/ reduce pairmap pairs /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use List::MoreUtils qw/ any all none uniq zip /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 5 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 5 | ▶ | use JSON::Schema::AsType::Draft3::Types '-all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 5 | ▶ | use Types::Standard 'Optional'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 5 | with 'JSON::Schema::AsType::Draft4' => { | |||||||||
| 24 | -excludes => [qw/ _keyword_properties _keyword_required _keyword_type /] | ||||||||||
| 25 | }; | ||||||||||
| 26 | |||||||||||
| 27 | sub _keyword_properties { | ||||||||||
| 28 | 64 | ▶ | my( $self, $properties ) = @_; | ||||||||
|
Subroutine
_keyword_properties: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | my @props = pairmap { { | ||||||||||
| 31 | 324 | ▶ | my $schema = $self->sub_schema($b); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 32 | 324 | my $p = $schema->type; | |||||||||
| 33 | 324 | ▶ | $p = Optional[$p] unless $b->{required}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 324 | $a => $p | |||||||||
| 35 | 64 | }} %$properties; | |||||||||
| 36 | |||||||||||
| 37 | 64 | return Properties[@props]; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub _keyword_disallow { | ||||||||||
| 41 | 3 | ▶ | Disallow[ $_[0]->_keyword_type($_[1]) ]; | ||||||||
|
Subroutine
_keyword_disallow: called CC=1 SCAR=0.0
| |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | sub _keyword_extends { | ||||||||||
| 46 | 3 | ▶ | my( $self, $extends ) = @_; | ||||||||
|
Subroutine
_keyword_extends: called CC=2 SCAR=6.9
| |||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | my @extends = ref $extends eq 'ARRAY' ? @$extends : ( $extends ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 3 | return Extends[ map { $self->sub_schema($_)->type } @extends]; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | sub _keyword_type { | ||||||||||
| 54 | 564 | ▶ | my( $self, $struct_type ) = @_; | ||||||||
|
Subroutine
_keyword_type: called CC=6 SCAR=17.9
| |||||||||||
| 55 | |||||||||||
| 56 | my %type_map = map { | ||||||||||
| 57 | 564 | lc $_->name => $_ | |||||||||
| 58 | } Integer, Boolean, Number, String, Null, Object, Array; | ||||||||||
| 59 | |||||||||||
| 60 | 564 | ▶ | unless( $self->strict_string ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 9 | $type_map{number} = LaxNumber; | |||||||||
| 62 | 9 | $type_map{integer} = LaxInteger; | |||||||||
| 63 | 9 | $type_map{string} = LaxString; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | 564 | ▶ | return if $struct_type eq 'any'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 553 | ▶ | return $type_map{$struct_type} if $type_map{$struct_type}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 98 | ▶ | if( my @types = eval { @$struct_type } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 98 | ▶ | return reduce { $a | $b } map { ref $_ ? $self->sub_schema($_)->type : $self->_keyword_type($_) } @types; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | die "unknown type '$struct_type'"; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub _keyword_divisibleBy { | ||||||||||
| 79 | 3 | ▶ | my( $self, $divisibleBy ) = @_; | ||||||||
|
Subroutine
_keyword_divisibleBy: called CC=1 SCAR=0.0
| |||||||||||
| 80 | |||||||||||
| 81 | 3 | DivisibleBy[$divisibleBy]; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub _keyword_dependencies { | ||||||||||
| 85 | 13 | ▶ | my( $self, $dependencies ) = @_; | ||||||||
|
Subroutine
_keyword_dependencies: called CC=1 SCAR=0.0
| |||||||||||
| 86 | |||||||||||
| 87 | return Dependencies[ | ||||||||||
| 88 | 13 | ▶ | pairmap { $a => ref $b eq 'HASH' ? $self->sub_schema($b)->type : $b } %$dependencies | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 89 | ]; | ||||||||||
| 90 | |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | JSON::Schema::AsType->new( | ||||||||||
| 94 | draft_version => '3', | ||||||||||
| 95 | uri => "http${_}://json-schema.org/draft-03/schema", | ||||||||||
| 96 | 5 | 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 | 5 | 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 | ||||||||||