| 1 | package JSON::Schema::AsType; | ||||||||||||||
| 2 | 44 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||
| 3 | 44 | $JSON::Schema::AsType::VERSION = '1.0.0'; | |||||||||||||
| 4 | # ABSTRACT: generates Type::Tiny types out of JSON schemas | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 44 | ▶ | use 5.14.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 44 | ▶ | use feature 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 44 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 44 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 44 | ▶ | use PerlX::Maybe; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 44 | ▶ | use Type::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 44 | ▶ | use Type::Tiny::Class; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 44 | ▶ | use Scalar::Util qw/ looks_like_number /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 44 | ▶ | use List::Util qw/ reduce pairmap pairs /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 44 | ▶ | use List::MoreUtils qw/ any all none uniq zip /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | use Types::Standard | ||||||||||||||
| 20 | 44 | ▶ | qw/InstanceOf HashRef StrictNum Any Str ArrayRef Int Object slurpy Dict Optional slurpy /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 44 | ▶ | use Type::Utils; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 44 | ▶ | use Clone 'clone'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 44 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 44 | ▶ | use Module::Runtime qw/ use_module /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 44 | ▶ | use Moose::Util qw/ apply_all_roles ensure_all_roles /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 44 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 44 | ▶ | use Type::Utils qw( class_type ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | 44 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 44 | ▶ | use MooseX::MungeHas 'is_ro'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 44 | with 'JSON::Schema::AsType::Registry'; | |||||||||||||
| 35 | 44 | with 'JSON::Schema::AsType::Type'; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 44 | ▶ | no warnings 'uninitialized'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 44 | our $strict_string = 1; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 44 | our @DRAFT_VERSIONS = ( 3, 4, 6, 7, '2019-09', '2020-12' ); | |||||||||||||
| 42 | |||||||||||||||
| 43 | has draft => ( | ||||||||||||||
| 44 | is => 'ro', | ||||||||||||||
| 45 | lazy => 1, | ||||||||||||||
| 46 | 5 | ▶ | default => sub($self) { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||
| 47 | 5 | ▶ | return $self->parent_schema->draft if $self->parent_schema; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 5 | return $DRAFT_VERSIONS[-1]; | |||||||||||||
| 49 | }, | ||||||||||||||
| 50 | 44 | isa => enum( \@DRAFT_VERSIONS ), | |||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | has metaschema => ( | ||||||||||||||
| 54 | is => 'ro', | ||||||||||||||
| 55 | lazy => 1, | ||||||||||||||
| 56 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 57 | 0 | $_[0]->fetch( sprintf "https://json-schema.org/draft-%02d/schema", | |||||||||||||
| 58 | $_[0]->draft ); | ||||||||||||||
| 59 | }, | ||||||||||||||
| 60 | 44 | ); | |||||||||||||
| 61 | |||||||||||||||
| 62 | has schema => ( | ||||||||||||||
| 63 | predicate => 'has_schema', | ||||||||||||||
| 64 | is => 'ro', | ||||||||||||||
| 65 | lazy => 1, | ||||||||||||||
| 66 | 7 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=14.6
| |||||||||||||||
| 67 | 7 | return +{}; | |||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | ▶ | my $uri = $self->uri or die "schema or uri required"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | return $self->fetch($uri)->schema; | |||||||||||||
| 73 | }, | ||||||||||||||
| 74 | 44 | ); | |||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | sub _schema_trigger { } | ||||||||||||
|
Subroutine
_schema_trigger: not called CC=1 SCAR=0.0
| |||||||||||||||
| 77 | |||||||||||||||
| 78 | 44 | has parent_schema => ( clearer => 1, ); | |||||||||||||
| 79 | |||||||||||||||
| 80 | has strict_string => ( | ||||||||||||||
| 81 | is => 'ro', | ||||||||||||||
| 82 | lazy => 1, | ||||||||||||||
| 83 | 1044 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||
| 84 | 1044 | my $self = shift; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 1044 | ▶ | return $self->parent_schema->strict_string if $self->parent_schema; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | 1044 | return $JSON::Schema::AsType::strict_string; | |||||||||||||
| 89 | }, | ||||||||||||||
| 90 | 44 | ); | |||||||||||||
| 91 | |||||||||||||||
| 92 | 44 | our %VOCABULARY; | |||||||||||||
| 93 | |||||||||||||||
| 94 | has vocabularies => ( | ||||||||||||||
| 95 | is => 'ro', | ||||||||||||||
| 96 | lazy => 1, | ||||||||||||||
| 97 | 4118 | ▶ | default => sub($self) { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | 4118 | ▶ | return [] unless $self->metaschema; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 4089 | ▶ | my $v = $self->metaschema->schema->{'$vocabulary'} or return []; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 817 | ▶ | return [ pairmap { ($a) x !!$b } %$v ]; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | } | ||||||||||||||
| 105 | 44 | ); | |||||||||||||
| 106 | |||||||||||||||
| 107 | 1 | ▶ | sub add_vocabulary( $self, $vocab ) { | ||||||||||||
|
Subroutine
add_vocabulary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 108 | 1 | push $self->vocabularies->@*, $vocab; | |||||||||||||
| 109 | 1 | ensure_all_roles( $self, $vocab ); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 14426 | ▶ | sub vocabulary_role( $self, $url ) { | ||||||||||||
|
Subroutine
vocabulary_role: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 113 | 14426 | $VOCABULARY{$url}; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | has uri => ( | ||||||||||||||
| 117 | is => 'ro', | ||||||||||||||
| 118 | isa => class_type( { class => 'URI' } )->plus_constructors( Str, "new", ), | ||||||||||||||
| 119 | coerce => 1, | ||||||||||||||
| 120 | 4419 | ▶ | trigger => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 121 | 4419 | my ( $self, $uri ) = @_; | |||||||||||||
| 122 | 4419 | ▶ | return if $uri->fragment; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | } | ||||||||||||||
| 124 | 44 | ); | |||||||||||||
| 125 | |||||||||||||||
| 126 | 4365 | ▶ | sub sub_schema( $self, $subschema, $uri ) { | ||||||||||||
|
Subroutine
sub_schema: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 127 | |||||||||||||||
| 128 | 4365 | ▶ | $uri = $self->resolve_uri($uri) if $uri; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | |||||||||||||||
| 130 | 4365 | JSON::Schema::AsType->new( | |||||||||||||
| 131 | draft => $self->draft, | ||||||||||||||
| 132 | schema => $subschema, | ||||||||||||||
| 133 | parent_schema => $self, | ||||||||||||||
| 134 | registry => $self->registry, | ||||||||||||||
| 135 | strict_string => $self->strict_string, | ||||||||||||||
| 136 | fetch_remote => $self->fetch_remote, | ||||||||||||||
| 137 | maybe uri => $uri | ||||||||||||||
| 138 | ); | ||||||||||||||
| 139 | |||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 4600 | ▶ | sub all_active_keywords($self) { | ||||||||||||
|
Subroutine
all_active_keywords: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 143 | 4600 | return grep { exists $self->schema->{$_} } $self->all_keywords; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 2077 | ▶ | sub all_keywords { | ||||||||||||
|
Subroutine
all_keywords: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 147 | 2077 | my $self = shift; | |||||||||||||
| 148 | |||||||||||||||
| 149 | # 'id' has to be first | ||||||||||||||
| 150 | 248751 | ▶ | return sort { $a eq 'id' ? -1 : $b eq 'id' ? 1 : $a cmp $b } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 151 | 2077 | map { /^_keyword_(.*)/ } $self->meta->get_method_list; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | 1 | ▶ | sub has_keyword( $self, $keyword ) { | ||||||||||||
|
Subroutine
has_keyword: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 155 | 1 | my $method = "_keyword_$keyword"; | |||||||||||||
| 156 | 1 | return $self->can($method); | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 5822 | ▶ | sub _process_keyword { | ||||||||||||
|
Subroutine
_process_keyword: called CC=1 SCAR=0.0
| |||||||||||||||
| 160 | 5822 | my ( $self, $keyword ) = @_; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 5822 | my $value = $self->schema->{$keyword}; | |||||||||||||
| 163 | |||||||||||||||
| 164 | 5822 | my $method = "_keyword_$keyword"; | |||||||||||||
| 165 | |||||||||||||||
| 166 | 5822 | $self->$method($value); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 457 | ▶ | sub resolve_reference { | ||||||||||||
|
Subroutine
resolve_reference: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 170 | 457 | my ( $self, $ref ) = @_; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 457 | my $uri = $self->resolve_uri($ref); | |||||||||||||
| 173 | |||||||||||||||
| 174 | 457 | ▶ | my $schema = $self->fetch($uri) or die "couldn't retrieve schema $uri\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | |||||||||||||||
| 176 | 453 | return $schema; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 289 | ▶ | sub _unescape_ref { | ||||||||||||
|
Subroutine
_unescape_ref: called CC=1 SCAR=0.0
| |||||||||||||||
| 180 | 289 | my ( $self, $ref ) = @_; | |||||||||||||
| 181 | |||||||||||||||
| 182 | 289 | $ref =~ s/~0/~/g; | |||||||||||||
| 183 | 289 | $ref =~ s!~1!/!g; | |||||||||||||
| 184 | 289 | $ref =~ s!%25!%!g; | |||||||||||||
| 185 | 289 | $ref =~ s!%22!"!g; | |||||||||||||
| 186 | |||||||||||||||
| 187 | 289 | $ref; | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 285 | ▶ | sub _escape_ref { | ||||||||||||
|
Subroutine
_escape_ref: called CC=1 SCAR=0.0
| |||||||||||||||
| 191 | 285 | my ( $self, $ref ) = @_; | |||||||||||||
| 192 | |||||||||||||||
| 193 | 285 | $ref =~ s/~/~0/g; | |||||||||||||
| 194 | 285 | $ref =~ s!/!~1!g; | |||||||||||||
| 195 | 285 | $ref =~ s!%!%25!g; | |||||||||||||
| 196 | 285 | $ref =~ s!"!%22!g; | |||||||||||||
| 197 | |||||||||||||||
| 198 | 285 | $ref; | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | |||||||||||||||
| 201 | 5878 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||
| 202 | 5878 | my $self = shift; | |||||||||||||
| 203 | |||||||||||||||
| 204 | 5878 | use_module( 'JSON::Schema::AsType::Draft' . $self->draft =~ s/-/_/r ) | |||||||||||||
| 205 | ->meta->rebless_instance($self); | ||||||||||||||
| 206 | |||||||||||||||
| 207 | # make it available early for the potential $refs | ||||||||||||||
| 208 | 5878 | ▶ | $self->register_schema( $self->uri, $self ) if $self->uri; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 209 | |||||||||||||||
| 210 | # TODO move the role into a trait, which should take care of this | ||||||||||||||
| 211 | 5878 | ▶ | $self->_schema_trigger( $self->schema ) if $self->has_schema; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | |||||||||||||||
| 213 | my @roles = | ||||||||||||||
| 214 | 5878 | map { $self->vocabulary_role($_) } $self->vocabularies->@*; | |||||||||||||
| 215 | |||||||||||||||
| 216 | 5878 | ▶ | ensure_all_roles( $self, @roles ) if @roles; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 217 | |||||||||||||||
| 218 | 5878 | ▶ | $self->_after_build if $self->can('_after_build'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | |||||||||||||||
| 220 | } | ||||||||||||||
| 221 | |||||||||||||||
| 222 | 44 | 1; | |||||||||||||
| 223 | |||||||||||||||
| 224 | __END__ | ||||||||||||||