CatalystX-Eta

 view release on metacpan or  search on metacpan

t/lib/Data/Verifier.pm  view on Meta::CPAN

package Data::Verifier;
{
    $Data::Verifier::VERSION = '0.56';
}
use Moose;

# ABSTRACT: Profile based data verification with Moose type constraints.

use Data::Verifier::Field;
use Data::Verifier::Filters;
use Data::Verifier::Results;
use Moose::Util::TypeConstraints;
use Scalar::Util qw(blessed);

#use Try::Tiny;

has 'derived' => (
    is        => 'ro',
    isa       => 'HashRef[HashRef]',
    predicate => 'has_derived'
);

has 'filters' => (
    is      => 'ro',
    isa     => 'ArrayRef[Str|CodeRef]',
    default => sub { [] }
);

has 'profile' => (
    is       => 'ro',
    isa      => 'HashRef[HashRef]',
    required => 1
);

sub coercion {
    my %params = @_;
    Moose::Meta::TypeCoercion->new(
        type_coercion_map => [
            $params{'from'} => $params{'via'}
        ]
    );
}

sub verify {
    my ( $self, $params, $members ) = @_;

    my $results = Data::Verifier::Results->new;
    my $profile = $self->profile;

    my $blessed_params = blessed($params);

    my @post_checks = ();
    foreach my $key ( keys( %{$profile} ) ) {
        my $skip_string_checks = 0;

        # Get the profile part that is pertinent to this field
        my $fprof = $profile->{$key};

        # Deal with the fact that what we're given may be an object.
        my $val = do {
            if ($blessed_params) {
                $params->can($key) ? $params->$key() : undef;
            }
            else {
                $params->{$key};
            }
        };

        # Creat the "field" that we'll put into the result.
        my $field = Data::Verifier::Field->new;

        # Save the original value.
        if ( ref($val) eq 'ARRAY' ) {
            my @values = @{$val};    # Make a copy of the array
            $field->original_value( \@values );



( run in 1.127 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )