Method-ParamValidator

 view release on metacpan or  search on metacpan

lib/Method/ParamValidator/Key/Field.pm  view on Meta::CPAN

package Method::ParamValidator::Key::Field;

$Method::ParamValidator::Key::Field::VERSION   = '0.16';
$Method::ParamValidator::Key::Field::AUTHORITY = 'cpan:MANWAR';

=head1 NAME

Method::ParamValidator::Key::Field - Represents 'parameter key field' for Method::ParamValidator.

=head1 VERSION

Version 0.16

=cut

use 5.006;
use Data::Dumper;

use Moo;
use namespace::autoclean;

use Types::Standard qw(:all);
use Method::ParamValidator::Key::Field::DataType qw(:all);

has 'name'    => (is => 'ro', isa => Str,          required  => 1);
has 'format'  => (is => 'ro', isa => Str,          default   => sub { 's' });
has 'check'   => (is => 'rw', isa => CodeRef,      predicate => 1);
has 'source'  => (is => 'ro', isa => HashRef[Str], predicate => 1);
has 'message' => (is => 'ro', isa => Str);
has 'multi'   => (is => 'ro');

sub str { !(defined $_[0] && $_[0] =~ /^\d+$/) };
sub int {  (defined $_[0] && $_[0] =~ /^\d+$/) };

=head1 DESCRIPTION

B<FOR INTERNAL USE ONLY>.

=cut

sub valid {
    my ($self, $value) = @_;

    if ($self->has_check) {
        return $self->check->($value);
    }
    else {
        if ($self->has_source && (keys %{$self->source})) {
            my $separator = $self->multi;
            if (defined $separator) {
                # Check each against the source.
                foreach (split /\Q$separator\E/, $value) {
                    return 0 unless exists $self->source->{uc($_)};
                }
                return 1;
            }
            return exists $self->source->{uc($value)};
        }
        elsif ($self->format eq 's') {
            &str($value);
        }
        elsif ($self->format eq 'd') {
            &int($value);
        }
    }
}

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Method-ParamValidator>

=head1 BUGS

Please report any  bugs or feature requests to C<bug-method-paramvalidator at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-ParamValidator>.
I will  be notified and then you'll automatically be notified of progress on your
bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Method::ParamValidator::Key::Field



( run in 0.706 second using v1.01-cache-2.11-cpan-39bf76dae61 )