« blib/lib/Valiemon/Attributes/Properties.pm
↑ Summary
blib/lib/Valiemon/Attributes/AdditionalItems.pm »
| 1 | package Valiemon; | ||||||||||||||||||||
| 2 | 25 | ▶ | use 5.012; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 25 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 25 | ▶ | use Valiemon::Primitives; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 25 | ▶ | use Valiemon::Context; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 25 | ▶ | use Valiemon::Attributes qw(attr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | use Class::Accessor::Lite ( | ||||||||||||||||||||
| 13 | 25 | ro => [qw(schema options pos schema_cache)], | |||||||||||||||||||
| 14 | 25 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 25 | our $VERSION = "0.04"; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||
| 19 | 581 | ▶ | my ($class, $schema, $options) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # TODO should validate own schema | ||||||||||||||||||||
| 22 | 581 | ▶ | if ($options->{validate_schema}) {} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 581 | ▶ | croak 'schema must be a hashref' unless ref $schema eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 581 | return bless { | |||||||||||||||||||
| 26 | schema => $schema, | ||||||||||||||||||||
| 27 | options => $options, | ||||||||||||||||||||
| 28 | schema_cache => +{}, | ||||||||||||||||||||
| 29 | }, $class; | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | sub validate { | ||||||||||||||||||||
| 33 | 1044 | ▶ | my ($self, $data, $context) = @_; | ||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||
| 34 | 1044 | my $schema = $self->schema; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1044 | ▶ | $context //= Valiemon::Context->new($self, $schema); | ||||||||||||||||||
|
Condition66%2/3
$context //= "Valiemon::Context"->new($self, $schema)
MC/DC0%0/2
$context //= "Valiemon::Context"->new($self, $schema)
$context
"Valiemon::Context"->new($self, $schema)
Truth table66%2/3
A //= B
A= $contextB= "Valiemon::Context"->new($self, $schema)
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1044 | for my $key (keys %{$schema}) { | |||||||||||||||||||
| 39 | 1300 | my $attr = attr($key); | |||||||||||||||||||
| 40 | 1299 | ▶ | if ($attr) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 1173 | my ($is_valid, $error) = $attr->is_valid($context, $schema, $data); | |||||||||||||||||||
| 42 | 1115 | ▶ | unless ($is_valid) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 379 | $error->set_detail( | |||||||||||||||||||
| 44 | expected => $schema, | ||||||||||||||||||||
| 45 | actual => $data, | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | 379 | $context->push_error($error); | |||||||||||||||||||
| 48 | 379 | next; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 985 | my $errors = $context->errors; | |||||||||||||||||||
| 54 | 985 | ▶ | my $is_valid = scalar @$errors ? 0 : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 985 | ▶ | return wantarray ? ($is_valid, $errors->[0]) : $is_valid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | sub prims { | ||||||||||||||||||||
| 59 | 894 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
prims: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 60 | 894 | ▶ | return $self->{prims} //= Valiemon::Primitives->new( | ||||||||||||||||||
|
Condition66%2/3
$self->{"prims"} //= "Valiemon::Primitives"->new($self->options)
MC/DC0%0/2
$self->{"prims"} //= "Valiemon::Primitives"->new($self->options)
$self->{"prims"}
"Valiemon::Primitives"->new($self->options)
Truth table66%2/3
A //= B
A= $self->{"prims"}B= "Valiemon::Primitives"->new($self->options)
| |||||||||||||||||||||
| 61 | $self->options | ||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | sub ref_schema_cache { | ||||||||||||||||||||
| 66 | 85 | ▶ | my ($self, $ref, $schema) = @_; | ||||||||||||||||||
|
Subroutine
ref_schema_cache: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 67 | return defined $schema | ||||||||||||||||||||
| 68 | ? $self->schema_cache->{$ref} = $schema | ||||||||||||||||||||
| 69 | 85 | ▶ | : $self->{schema_cache}->{ref}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | sub resolve_ref { | ||||||||||||||||||||
| 73 | 58 | ▶ | my ($self, $ref) = @_; | ||||||||||||||||||
|
Subroutine
resolve_ref: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # TODO follow the standard referencing | ||||||||||||||||||||
| 76 | 58 | ▶ | unless ($ref =~ qr|^#/|) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 14 | croak 'This package support only single scope and `#/` referencing'; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 44 | ▶ | return $self->ref_schema_cache($ref) || do { | ||||||||||||||||||
|
Condition33%1/3
$self->ref_schema_cache($ref) || do {
my $paths = do {
my @p = split(m[/]u, $ref, 0);
[splice(@p, 1)]
};
my $sub_schema = $self->schema;
{
eval {
do {
$sub_schema = $sub_schema->{$_} foreach (@$paths)
}
};
croak(sprintf('referencing `%s` cause error', $ref)) if $@;
croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema;
};
$self->ref_schema_cache($ref, $sub_schema);
$sub_schema
}
MC/DC0%0/2
$self->ref_schema_cache($ref) || do {
my $paths = do {
my @p = split(m[/]u, $ref, 0);
[splice(@p, 1)]
};
my $sub_schema = $self->schema;
{
eval {
do {
$sub_schema = $sub_schema->{$_} foreach (@$paths)
}
};
croak(sprintf('referencing `%s` cause error', $ref)) if $@;
croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema;
};
$self->ref_schema_cache($ref, $sub_schema);
$sub_schema
}
$self->ref_schema_cache($ref)
do {
my $paths = do {
my @p = split(m[/]u, $ref, 0);
[splice(@p, 1)]
};
my $sub_schema = $self->schema;
{
eval {
do {
$sub_schema = $sub_schema->{$_} foreach (@$paths)
}
};
croak(sprintf('referencing `%s` cause error', $ref)) if $@;
croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema;
};
$self->ref_schema_cache($ref, $sub_schema);
$sub_schema
}
Truth table33%1/3
A || B
A= $self->ref_schema_cache($ref)B= do {
my $paths = do {
my @p = split(m[/]u, $ref, 0);
[splice(@p, 1)]
};
my $sub_schema = $self->schema;
{
eval {
do {
$sub_schema = $sub_schema->{$_} foreach (@$paths)
}
};
croak(sprintf('referencing `%s` cause error', $ref)) if $@;
croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema;
};
$self->ref_schema_cache($ref, $sub_schema);
$sub_schema
}
| |||||||||||||||||||||
| 81 | 44 | my $paths = do { | |||||||||||||||||||
| 82 | 44 | my @p = split '/', $ref; | |||||||||||||||||||
| 83 | 44 | [ splice @p, 1 ]; # remove '#' | |||||||||||||||||||
| 84 | }; | ||||||||||||||||||||
| 85 | 44 | my $sub_schema = $self->schema; | |||||||||||||||||||
| 86 | { | ||||||||||||||||||||
| 87 | 44 | eval { $sub_schema = $sub_schema->{$_} for @$paths }; | |||||||||||||||||||
| 88 | 44 | ▶ | croak sprintf 'referencing `%s` cause error', $ref if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 42 | ▶ | croak sprintf 'schema `%s` not found', $ref unless $sub_schema; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 41 | $self->ref_schema_cache($ref, $sub_schema); # caching | |||||||||||||||||||
| 92 | 41 | $sub_schema; | |||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 25 | 1; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||