| 1 | package JSON::Schema::AsType::Draft4; | ||||||||||||
| 2 | 9 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||
| 3 | 9 | $JSON::Schema::AsType::Draft4::VERSION = '1.0.0'; | |||||||||||
| 4 | # ABSTRACT: A draft 4 JSON Schema | ||||||||||||
| 5 | |||||||||||||
| 6 | |||||||||||||
| 7 | 9 | ▶ | use 5.42.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 9 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 9 | ▶ | use feature ':5.42'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 9 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | # use Data::Visitor::Tiny; | ||||||||||||
| 15 | 9 | ▶ | use JSON::Schema::AsType::Visit; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 9 | ▶ | use List::Util qw/ uniq /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 9 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 9 | extends qw/ JSON::Schema::AsType /; | |||||||||||
| 22 | |||||||||||||
| 23 | 9 | with 'JSON::Schema::AsType::Draft4::Keywords'; | |||||||||||
| 24 | |||||||||||||
| 25 | 9 | ▶ | use feature qw/ signatures /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | |||||||||||||
| 27 | 9 | has '+draft' => default => 4; | |||||||||||
| 28 | |||||||||||||
| 29 | has '+metaschema' => ( | ||||||||||||
| 30 | 776 | ▶ | default => sub($self) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 776 | my $schema = _metaschema(); | |||||||||||
| 32 | 776 | return $schema; | |||||||||||
| 33 | } | ||||||||||||
| 34 | 9 | ); | |||||||||||
| 35 | |||||||||||||
| 36 | 9 | my $_uri_port = 1; | |||||||||||
| 37 | 185 | ▶ | has '+uri' => default => sub($self) { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 38 | 185 | ▶ | my $id = $self->_has_id( $self->schema ) | ||||||||||
|
Condition100%2/2
$self->_has_id($self->schema) // 'http://254.0.0.1:' . $_uri_port++
MC/DC100%1/1
$self->_has_id($self->schema) // 'http://254.0.0.1:' . $_uri_port++
$self->_has_id($self->schema)
Truth table100%2/2
A
A= $self->_has_id($self->schema)
| |||||||||||||
| 39 | // 'http://254.0.0.1:' . $_uri_port++; | ||||||||||||
| 40 | 185 | $self->clear_parent_schema; | |||||||||||
| 41 | 185 | return $id; | |||||||||||
| 42 | 9 | }; | |||||||||||
| 43 | |||||||||||||
| 44 | 646 | ▶ | override all_keywords => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 45 | 646 | my $self = shift; | |||||||||||
| 46 | |||||||||||||
| 47 | # $ref trumps all | ||||||||||||
| 48 | 646 | ▶ | return '$ref' if $self->schema->{'$ref'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 499 | return uniq 'id', super(); | |||||||||||
| 51 | 9 | }; | |||||||||||
| 52 | |||||||||||||
| 53 | 776 | ▶ | sub _schema_trigger( $self, $schema, @ ) { | ||||||||||
|
Subroutine
_schema_trigger: called CC=2 SCAR=6.9
| |||||||||||||
| 54 | 776 | ▶ | return unless $schema; # TODO | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | JSON::Schema::AsType::Visit::visit( | ||||||||||||
| 56 | $schema, | ||||||||||||
| 57 | 3861 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 58 | 3861 | ▶ | return unless ref $_ eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | |||||||||||||
| 60 | 1396 | ▶ | my $id = $self->_has_id($_) or return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | |||||||||||||
| 62 | # that doesn't look like a 'id' for the schema | ||||||||||||
| 63 | 32 | ▶ | return if ref $id; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | |||||||||||||
| 65 | 21 | $self->sub_schema( $_, $id ); | |||||||||||
| 66 | } | ||||||||||||
| 67 | 766 | ); | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 2225 | ▶ | sub _has_id ( $self, $schema = {} ) { | ||||||||||
|
Subroutine
_has_id: called CC=3 SCAR=11.0
| |||||||||||||
| 71 | 2225 | ▶ | return unless ref $schema eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 2215 | return $schema->{id}; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 583 | ▶ | around sub_schema => sub ( $orig, $self, $subschema, $uri ) { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 76 | |||||||||||||
| 77 | # ah AH, resolve the subschema id | ||||||||||||
| 78 | 583 | ▶ | if ( my $id = $self->_has_id($subschema) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 50 | ▶ | $uri = $self->resolve_uri($id) unless $subschema->{'$ref'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | } | ||||||||||||
| 81 | 583 | $orig->( $self, $subschema, $uri ); | |||||||||||
| 82 | 9 | }; | |||||||||||
| 83 | |||||||||||||
| 84 | 778 | ▶ | sub _metaschema { | ||||||||||
|
Subroutine
_metaschema: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | 778 | state $METASCHEMA = __PACKAGE__->new( | |||||||||||
| 86 | uri => "https://json-schema.org/draft-04/schema", | ||||||||||||
| 87 | schema => from_json join '', | ||||||||||||
| 88 | <DATA>, | ||||||||||||
| 89 | ); | ||||||||||||
| 90 | |||||||||||||
| 91 | 778 | return $METASCHEMA; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | =pod | ||||||||||||
| 95 | |||||||||||||
| 96 | =encoding UTF-8 | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 NAME | ||||||||||||
| 99 | |||||||||||||
| 100 | JSON::Schema::AsType::Draft4 - A draft 4 JSON Schema | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 VERSION | ||||||||||||
| 103 | |||||||||||||
| 104 | version 1.0.0 | ||||||||||||
| 105 | |||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||
| 107 | |||||||||||||
| 108 | Internal module for L<JSON::Schema:::AsType>. | ||||||||||||
| 109 | |||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||
| 111 | |||||||||||||
| 112 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||||
| 113 | |||||||||||||
| 114 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 115 | |||||||||||||
| 116 | This software is copyright (c) 2026 by Yanick Champoux. | ||||||||||||
| 117 | |||||||||||||
| 118 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 119 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 120 | |||||||||||||
| 121 | =cut | ||||||||||||
| 122 | |||||||||||||
| 123 | __DATA__ | ||||||||||||