| 1 | package JSON::Schema::AsType::Draft4::Keywords; | ||||||||||||
| 2 | 32 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||
| 3 | 32 | $JSON::Schema::AsType::Draft4::Keywords::VERSION = '1.0.0'; | |||||||||||
| 4 | # ABSTRACT: Draft4 keywords | ||||||||||||
| 5 | |||||||||||||
| 6 | |||||||||||||
| 7 | 32 | ▶ | use 5.42.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 32 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 32 | ▶ | use feature qw/ signatures /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 32 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 32 | ▶ | use Type::Utils; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 32 | ▶ | use Scalar::Util qw/ looks_like_number /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 32 | ▶ | use List::Util qw/ reduce pairmap pairs /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 32 | ▶ | use List::MoreUtils qw/ any all none uniq zip /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | use Types::Standard | ||||||||||||
| 19 | 32 | ▶ | qw/InstanceOf HashRef StrictNum Any Str ArrayRef Int slurpy Dict Optional slurpy /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 32 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 32 | ▶ | use JSON::Schema::AsType; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 32 | ▶ | use JSON::Schema::AsType::Draft4::Types '-all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | |||||||||||||
| 27 | __PACKAGE__->meta->add_method( | ||||||||||||
| 28 | 94 | ▶ | '_keyword_$ref' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_keyword_$ref: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 94 | my ( $self, $ref ) = @_; | |||||||||||
| 30 | |||||||||||||
| 31 | 94 | my $schema; | |||||||||||
| 32 | |||||||||||||
| 33 | return Type::Tiny->new( | ||||||||||||
| 34 | name => 'Ref', | ||||||||||||
| 35 | display_name => "Ref($ref)", | ||||||||||||
| 36 | 19994 | ▶ | constraint => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||
| 37 | 19994 | ▶ | local $::DEEP = ( $::DEEP // 0 ) + 1; | ||||||||||
|
Condition100%2/2
$main::DEEP // 0
MC/DC100%1/1
$main::DEEP // 0
$main::DEEP
Truth table100%2/2
A
A= $main::DEEP
| |||||||||||||
| 38 | 19994 | ▶ | die if $::DEEP > 10; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 19994 | my $v = $_; | |||||||||||
| 40 | |||||||||||||
| 41 | 19994 | ▶ | unless ($schema) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 457 | $schema = $self->resolve_reference($ref); | |||||||||||
| 43 | 453 | $schema = | |||||||||||
| 44 | $self->sub_schema( $schema->schema, $schema->uri ); | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 19990 | ▶ | return $schema->base_type->check($v) || 0; | ||||||||||
|
Condition100%2/2
$schema->base_type->check($v) || 0
MC/DC100%1/1
$schema->base_type->check($v) || 0
$schema->base_type->check($v)
Truth table100%2/2
A
A= $schema->base_type->check($v)
| |||||||||||||
| 48 | }, | ||||||||||||
| 49 | 0 | ▶ | message => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 50 | join "\n", | ||||||||||||
| 51 | "ref schema is " | ||||||||||||
| 52 | . to_json( $schema->schema, { allow_nonref => 1 } ), | ||||||||||||
| 53 | 0 | @{ $schema->validate_explain($_) }; | |||||||||||
| 54 | } | ||||||||||||
| 55 | 94 | ); | |||||||||||
| 56 | } | ||||||||||||
| 57 | 32 | ); | |||||||||||
| 58 | |||||||||||||
| 59 | 266 | ▶ | sub _keyword_id { | ||||||||||
|
Subroutine
_keyword_id: called CC=1 SCAR=0.0
| |||||||||||||
| 60 | |||||||||||||
| 61 | # done as part of the initial visit | ||||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 111 | ▶ | sub _keyword_definitions { | ||||||||||
|
Subroutine
_keyword_definitions: called CC=2 SCAR=6.9
| |||||||||||||
| 65 | 111 | my ( $self, $defs ) = @_; | |||||||||||
| 66 | |||||||||||||
| 67 | 111 | $self->sub_schema( $defs->{$_}, "#./definitions/$_" ) for keys %$defs; | |||||||||||
| 68 | |||||||||||||
| 69 | 111 | return; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 28 | ▶ | sub _keyword_pattern { | ||||||||||
|
Subroutine
_keyword_pattern: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | 28 | my ( $self, $pattern ) = @_; | |||||||||||
| 74 | |||||||||||||
| 75 | 28 | Pattern [$pattern]; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 258 | ▶ | sub _keyword_enum { | ||||||||||
|
Subroutine
_keyword_enum: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | 258 | my ( $self, $enum ) = @_; | |||||||||||
| 80 | |||||||||||||
| 81 | 258 | Enum [@$enum]; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 75 | ▶ | sub _keyword_uniqueItems { | ||||||||||
|
Subroutine
_keyword_uniqueItems: called CC=2 SCAR=6.9
| |||||||||||||
| 85 | 75 | my ( $self, $unique ) = @_; | |||||||||||
| 86 | |||||||||||||
| 87 | 75 | ▶ | return unless $unique; # unique false? all is good | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | |||||||||||||
| 89 | 57 | return UniqueItems; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 9 | ▶ | sub _keyword_dependencies { | ||||||||||
|
Subroutine
_keyword_dependencies: called CC=1 SCAR=0.0
| |||||||||||||
| 93 | 9 | my ( $self, $dependencies ) = @_; | |||||||||||
| 94 | |||||||||||||
| 95 | return Dependencies [ | ||||||||||||
| 96 | 16 | ▶ | pairmap { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 97 | 16 | ▶ | $a => ref $b eq 'HASH' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | ? $self->sub_schema( $b, "#./dependencies/$a" ) | ||||||||||||
| 99 | : $b | ||||||||||||
| 100 | 9 | } %$dependencies | |||||||||||
| 101 | ]; | ||||||||||||
| 102 | |||||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | 210 | ▶ | sub _keyword_additionalProperties { | ||||||||||
|
Subroutine
_keyword_additionalProperties: called CC=3 SCAR=11.0
| |||||||||||||
| 106 | 210 | my ( $self, $addi ) = @_; | |||||||||||
| 107 | |||||||||||||
| 108 | 210 | my $add_schema; | |||||||||||
| 109 | 210 | ▶ | $add_schema = $self->sub_schema( $addi, '#./additionalProperties' ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | if ref $addi eq 'HASH'; | ||||||||||||
| 111 | |||||||||||||
| 112 | my @known_keys = ( | ||||||||||||
| 113 | 210 | eval { keys %{ $self->schema->{properties} } }, | |||||||||||
| 114 | 210 | map { qr/$_/ } eval { keys %{ $self->schema->{patternProperties} } } | |||||||||||
| 115 | ); | ||||||||||||
| 116 | |||||||||||||
| 117 | 210 | ▶ | return AdditionalProperties [ \@known_keys, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | $add_schema ? $add_schema->type : $addi ]; | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 129 | ▶ | sub _keyword_patternProperties { | ||||||||||
|
Subroutine
_keyword_patternProperties: called CC=1 SCAR=0.0
| |||||||||||||
| 122 | 129 | my ( $self, $properties ) = @_; | |||||||||||
| 123 | |||||||||||||
| 124 | my %prop_schemas = | ||||||||||||
| 125 | 68 | ▶ | pairmap { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 126 | 68 | $a => $self->sub_schema( $b, "#./patternProperties/$a" )->type } | |||||||||||
| 127 | 129 | %$properties; | |||||||||||
| 128 | |||||||||||||
| 129 | 129 | return PatternProperties [%prop_schemas]; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 493 | ▶ | sub _keyword_properties { | ||||||||||
|
Subroutine
_keyword_properties: called CC=1 SCAR=0.0
| |||||||||||||
| 133 | 493 | my ( $self, $properties ) = @_; | |||||||||||
| 134 | |||||||||||||
| 135 | Properties [ | ||||||||||||
| 136 | 1279 | ▶ | pairmap { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 137 | 1279 | my $schema = $self->sub_schema( $b, "#./properties/$a" ); | |||||||||||
| 138 | 1279 | $a => $schema->type; | |||||||||||
| 139 | } | ||||||||||||
| 140 | 493 | %$properties | |||||||||||
| 141 | ]; | ||||||||||||
| 142 | |||||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 14 | ▶ | sub _keyword_maxProperties { | ||||||||||
|
Subroutine
_keyword_maxProperties: called CC=1 SCAR=0.0
| |||||||||||||
| 146 | 14 | my ( $self, $max ) = @_; | |||||||||||
| 147 | |||||||||||||
| 148 | 14 | MaxProperties [$max]; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 14 | ▶ | sub _keyword_minProperties { | ||||||||||
|
Subroutine
_keyword_minProperties: called CC=1 SCAR=0.0
| |||||||||||||
| 152 | 14 | my ( $self, $min ) = @_; | |||||||||||
| 153 | |||||||||||||
| 154 | 14 | MinProperties [$min]; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | 170 | ▶ | sub _keyword_required { | ||||||||||
|
Subroutine
_keyword_required: called CC=1 SCAR=0.0
| |||||||||||||
| 158 | 170 | my ( $self, $required ) = @_; | |||||||||||
| 159 | |||||||||||||
| 160 | 170 | Required [@$required]; | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | 51 | ▶ | sub _keyword_not { | ||||||||||
|
Subroutine
_keyword_not: called CC=1 SCAR=0.0
| |||||||||||||
| 164 | 51 | my ( $self, $schema ) = @_; | |||||||||||
| 165 | 51 | Not [ $self->sub_schema( $schema, '#./not' )->base_type ]; | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | 67 | ▶ | sub _keyword_oneOf { | ||||||||||
|
Subroutine
_keyword_oneOf: called CC=1 SCAR=0.0
| |||||||||||||
| 169 | 67 | my ( $self, $options ) = @_; | |||||||||||
| 170 | |||||||||||||
| 171 | OneOf [ | ||||||||||||
| 172 | 137 | ▶ | pairmap { $self->sub_schema( $b, "#./oneOf/$a" )->base_type } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 173 | 67 | indexed @$options | |||||||||||
| 174 | ]; | ||||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | 137 | ▶ | sub _keyword_anyOf { | ||||||||||
|
Subroutine
_keyword_anyOf: called CC=1 SCAR=0.0
| |||||||||||||
| 178 | 137 | my ( $self, $options ) = @_; | |||||||||||
| 179 | |||||||||||||
| 180 | 137 | my $i = 0; | |||||||||||
| 181 | 137 | AnyOf [ map { $self->sub_schema( $_, '#./anyOf/' . $i++ )->base_type } | |||||||||||
| 182 | @$options ]; | ||||||||||||
| 183 | } | ||||||||||||
| 184 | |||||||||||||
| 185 | 162 | ▶ | sub _keyword_allOf { | ||||||||||
|
Subroutine
_keyword_allOf: called CC=1 SCAR=0.0
| |||||||||||||
| 186 | 162 | my ( $self, $options ) = @_; | |||||||||||
| 187 | |||||||||||||
| 188 | 162 | my $i = 0; | |||||||||||
| 189 | 162 | AllOf [ map { $self->sub_schema( $_, "#./allOf/" . $i++ )->base_type } | |||||||||||
| 190 | @$options ]; | ||||||||||||
| 191 | } | ||||||||||||
| 192 | |||||||||||||
| 193 | 1586 | ▶ | sub _keyword_type { | ||||||||||
|
Subroutine
_keyword_type: called CC=4 SCAR=13.9
| |||||||||||||
| 194 | 1586 | my ( $self, $struct_type ) = @_; | |||||||||||
| 195 | |||||||||||||
| 196 | 1586 | my %keyword_map = map { lc $_->name => $_ } Integer, Number, String, | |||||||||||
| 197 | Object, Array, Boolean, Null; | ||||||||||||
| 198 | |||||||||||||
| 199 | 1586 | ▶ | unless ( $self->strict_string ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 200 | 9 | $keyword_map{number} = LaxNumber; | |||||||||||
| 201 | 9 | $keyword_map{integer} = LaxInteger; | |||||||||||
| 202 | 9 | $keyword_map{string} = LaxString; | |||||||||||
| 203 | } | ||||||||||||
| 204 | |||||||||||||
| 205 | return $keyword_map{$struct_type} | ||||||||||||
| 206 | 1586 | ▶ | if $keyword_map{$struct_type}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | |||||||||||||
| 208 | 67 | ▶ | if ( ref $struct_type eq 'ARRAY' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | 67 | return AnyOf [ map { $self->_keyword_type($_) } @$struct_type ]; | |||||||||||
| 210 | } | ||||||||||||
| 211 | |||||||||||||
| 212 | 0 | return; | |||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | 54 | ▶ | sub _keyword_multipleOf { | ||||||||||
|
Subroutine
_keyword_multipleOf: called CC=1 SCAR=0.0
| |||||||||||||
| 216 | 54 | my ( $self, $num ) = @_; | |||||||||||
| 217 | |||||||||||||
| 218 | 54 | MultipleOf [$num]; | |||||||||||
| 219 | } | ||||||||||||
| 220 | |||||||||||||
| 221 | 17 | ▶ | sub _keyword_maxItems { | ||||||||||
|
Subroutine
_keyword_maxItems: called CC=1 SCAR=0.0
| |||||||||||||
| 222 | 17 | my ( $self, $max ) = @_; | |||||||||||
| 223 | |||||||||||||
| 224 | 17 | MaxItems [$max]; | |||||||||||
| 225 | } | ||||||||||||
| 226 | |||||||||||||
| 227 | 49 | ▶ | sub _keyword_minItems { | ||||||||||
|
Subroutine
_keyword_minItems: called CC=1 SCAR=0.0
| |||||||||||||
| 228 | 49 | my ( $self, $min ) = @_; | |||||||||||
| 229 | |||||||||||||
| 230 | 49 | MinItems [$min]; | |||||||||||
| 231 | } | ||||||||||||
| 232 | |||||||||||||
| 233 | 31 | ▶ | sub _keyword_maxLength { | ||||||||||
|
Subroutine
_keyword_maxLength: called CC=1 SCAR=0.0
| |||||||||||||
| 234 | 31 | my ( $self, $max ) = @_; | |||||||||||
| 235 | |||||||||||||
| 236 | 31 | MaxLength [$max]; | |||||||||||
| 237 | } | ||||||||||||
| 238 | |||||||||||||
| 239 | 29 | ▶ | sub _keyword_minLength { | ||||||||||
|
Subroutine
_keyword_minLength: called CC=1 SCAR=0.0
| |||||||||||||
| 240 | 29 | my ( $self, $min ) = @_; | |||||||||||
| 241 | |||||||||||||
| 242 | 29 | return MinLength [$min]; | |||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | 36 | ▶ | sub _keyword_maximum { | ||||||||||
|
Subroutine
_keyword_maximum: called CC=2 SCAR=6.9
| |||||||||||||
| 246 | 36 | my ( $self, $maximum ) = @_; | |||||||||||
| 247 | |||||||||||||
| 248 | return $self->schema->{exclusiveMaximum} | ||||||||||||
| 249 | 36 | ▶ | ? ExclusiveMaximum [$maximum] | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 250 | : Maximum [$maximum]; | ||||||||||||
| 251 | |||||||||||||
| 252 | } | ||||||||||||
| 253 | |||||||||||||
| 254 | 72 | ▶ | sub _keyword_minimum { | ||||||||||
|
Subroutine
_keyword_minimum: called CC=2 SCAR=6.9
| |||||||||||||
| 255 | 72 | my ( $self, $minimum ) = @_; | |||||||||||
| 256 | |||||||||||||
| 257 | 72 | ▶ | if ( $self->schema->{exclusiveMinimum} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 9 | return ExclusiveMinimum [$minimum]; | |||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | 63 | return Minimum [$minimum]; | |||||||||||
| 262 | } | ||||||||||||
| 263 | |||||||||||||
| 264 | 71 | ▶ | sub _keyword_additionalItems { | ||||||||||
|
Subroutine
_keyword_additionalItems: called CC=4 SCAR=13.9
| |||||||||||||
| 265 | 71 | my ( $self, $s ) = @_; | |||||||||||
| 266 | |||||||||||||
| 267 | # unless($s) { | ||||||||||||
| 268 | # my $items = $self->schema->{items} or return; | ||||||||||||
| 269 | # return if ref $items eq 'HASH'; # it's a schema, nevermind | ||||||||||||
| 270 | # my $size = @$items; | ||||||||||||
| 271 | |||||||||||||
| 272 | # return AdditionalItems[$size]; | ||||||||||||
| 273 | # } | ||||||||||||
| 274 | |||||||||||||
| 275 | 71 | my $schema = $self->sub_schema( $s, '#./additionalItems' ); | |||||||||||
| 276 | |||||||||||||
| 277 | # items is schema => additionalItems does nothing | ||||||||||||
| 278 | 71 | ▶ | return Any if ref $self->schema->{items} eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 279 | |||||||||||||
| 280 | # no items? it's always valid | ||||||||||||
| 281 | 63 | ▶ | return Any unless defined $self->schema->{items}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 282 | |||||||||||||
| 283 | 51 | ▶ | my $to_skip = ( $self->schema->{items} || [] )->@*; | ||||||||||
|
Condition50%1/2
$self->schema->{'items'} || []
MC/DC0%0/1
$self->schema->{'items'} || []
$self->schema->{'items'}
Truth table50%1/2
A
A= $self->schema->{'items'}
| |||||||||||||
| 284 | |||||||||||||
| 285 | 51 | return AdditionalItems [ $to_skip, $schema ]; | |||||||||||
| 286 | |||||||||||||
| 287 | } | ||||||||||||
| 288 | |||||||||||||
| 289 | 69 | ▶ | sub _keyword_items ( $self, $items, $keyword = 'items' ) { | ||||||||||
|
Subroutine
_keyword_items: called CC=5 SCAR=16.2
| |||||||||||||
| 290 | |||||||||||||
| 291 | 69 | ▶ | if ( Boolean->check($items) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 292 | 0 | return Items [$items]; | |||||||||||
| 293 | } | ||||||||||||
| 294 | |||||||||||||
| 295 | 69 | ▶ | if ( ref $items eq 'HASH' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 296 | 40 | my $type = $self->sub_schema( $items, '#./' . $keyword )->type; | |||||||||||
| 297 | |||||||||||||
| 298 | 40 | return Items [$type]; | |||||||||||
| 299 | } | ||||||||||||
| 300 | |||||||||||||
| 301 | # TODO forward declaration not workie | ||||||||||||
| 302 | 29 | my @types; | |||||||||||
| 303 | 29 | my $i = 0; | |||||||||||
| 304 | 29 | for (@$items) { | |||||||||||
| 305 | 50 | push @types, $self->sub_schema( $_, "#./$keyword/" . $i++ )->type; | |||||||||||
| 306 | } | ||||||||||||
| 307 | |||||||||||||
| 308 | 29 | return Items [ \@types ]; | |||||||||||
| 309 | } | ||||||||||||
| 310 | |||||||||||||
| 311 | 32 | 1; | |||||||||||
| 312 | |||||||||||||
| 313 | __END__ | ||||||||||||