Mojolicious-Plugin-ValidateTiny

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin('ValidateTiny');
    
    # Mojolicious::Lite
    plugin 'ValidateTiny';
    
    sub action { 
        my $self = shift;
        my $validate_rules = [
             # All of these are required
             [qw/name email pass pass2/] => is_required(),

             # pass2 must be equal to pass
             pass2 => is_equal('pass'),

             # custom sub validates an email address
             email => sub {
                my ( $value, $params ) = @_;
                Email::Valid->address($value) ? undef : 'Invalid email';
             }
        ];
        return unless $self->do_validation($validate_rules);
        
        ... Do something ...
    }
        
        
    sub action2 {
        my $self = shift;

        my $validate_rules = { 
            checks  => [...],
            fields  => [...],
            filters => [...]
        };
        if ( my $filtered_params =  $self->do_validation($validate_rules) ) {
            # all $params are validated and filters are applyed
            ... do your action ...

         
        } else {
            my $errors     = $self->validator_error;             # hash with errors
            my $pass_error = $self->validator_error('password'); # password error text
            my $any_error  = $self->validator_any_error;         # any error text
            
            $self->render( status => '403', text => $any_error );  
        }

lib/Mojolicious/Plugin/ValidateTiny.pm  view on Meta::CPAN

        exclude    => [],
        %{ $conf || {} } };

    # Helper do_validation
    $app->helper(
        do_validation => sub {
            my ( $c, $rules, $params ) = @_;
            croak "ValidateTiny: Wrong validatation rules"
                unless ref($rules) ~~ [ 'ARRAY', 'HASH' ];

            $c->stash('validate_tiny.was_called', 1);

            $rules = { checks => $rules } if ref $rules eq 'ARRAY';
            $rules->{fields} ||= [];

            # Validate GET+POST parameters by default
            $params ||= $c->req->params->to_hash();

            # Validate Uploaded files by default
            $params->{ $_->name } ||= $_ for (@{ $c->req->uploads });

lib/Mojolicious/Plugin/ValidateTiny.pm  view on Meta::CPAN

                }

                if (@fields_wo_rules) {
                    my $err_msg = 'ValidateTiny: No validation rules for '
                        . join( ', ', map { qq'"$_"' } @fields_wo_rules );

                    my $errors = {};
                    foreach my $f (@fields_wo_rules) {
                        $errors->{$f} = "No validation rules for field \"$f\"";
                    }
                    $c->stash( 'validate_tiny.errors' => $errors);
                    $log->debug($err_msg);
                    return 0;
                }
            }

            # Do validation, Validate::Tiny made a breaking change and we need to support old and new users
            my $result; 
            if(Validate::Tiny->can('check')) {
                $result = Validate::Tiny->check( $params, $rules );
            }
            else { # Fall back for old Validate::Tiny version
                $result = Validate::Tiny->new( $params, $rules );
            }
            
            $c->stash( 'validate_tiny.result' => $result );

            if ( $result->success ) {
                $log->debug('ValidateTiny: Successful');
                return $result->data;
            } else {
                $log->debug( 'ValidateTiny: Failed: ' . join( ', ', keys %{ $result->error } ) );
                $c->stash( 'validate_tiny.errors' => $result->error );
                return;
            }
        } );

    # Helper validator_has_errors
    $app->helper(
        validator_has_errors => sub {
            my $c      = shift;
            my $errors = $c->stash('validate_tiny.errors');

            return 0 if !$errors || !keys %$errors;
            return 1;
        } );

    # Helper validator_error
    $app->helper(
        validator_error => sub {
            my ( $c, $name ) = @_;
            my $errors = $c->stash('validate_tiny.errors');

            return $errors unless defined $name;

            if ( $errors && defined $errors->{$name} ) {
                return $errors->{$name};
            }
        } );

    # Helper validator_error_string
    $app->helper(
        validator_error_string => sub {
            my ( $c, $params ) = @_;
            return '' unless $c->validator_has_errors();
            $params //= {};

		    return $c->stash('validate_tiny.result')->error_string(%$params);
        } );

    # Helper validator_any_error
    $app->helper(
        validator_any_error => sub {
            my ( $c ) = @_;
            my $errors = $c->stash('validate_tiny.errors');

            if ( $errors ) {
                return ( ( values %$errors )[0] );
            }

            return;
        } );


    # Print info about actions without validation
    $app->hook(
        after_dispatch => sub {
            my ($c) = @_;
            my $stash = $c->stash;
            return 1 if $stash->{'validate_tiny.was_called'};

            if ( $stash->{controller} && $stash->{action} ) {
                $log->debug("ValidateTiny: No validation in [$stash->{controller}#$stash->{action}]");
                return 0;
            }

            return 1;
    } );

}

lib/Mojolicious/Plugin/ValidateTiny.pm  view on Meta::CPAN

    use Validate::Tiny ':all';

    # Mojolicious
    $self->plugin('ValidateTiny');

    # Mojolicious::Lite
    plugin 'ValidateTiny';

    sub action {
        my $self = shift;
        my $validate_rules = [
             # All of these are required
             [qw/name email pass pass2/] => is_required(),

             # pass2 must be equal to pass
             pass2 => is_equal('pass'),

             # custom sub validates an email address
             email => sub {
                my ( $value, $params ) = @_;
                Email::Valid->address($value) ? undef : 'Invalid email';
             }
        ];
        return unless $self->do_validation($validate_rules);

        ... Do something ...
    }


    sub action2 {
        my $self = shift;

        my $validate_rules = {
            checks  => [...],
            fields  => [...],
            filters => [...]
        };
        if ( my $filtered_params =  $self->do_validation($validate_rules) ) {
            # all $params are validated and filters are applyed
            ... do your action ...


        } else {
            my $errors     = $self->validator_error;             # hash with errors
            my $pass_error = $self->validator_error('password'); # password error text
            my $any_error  = $self->validator_any_error;         # any error text

            $self->render( status => '403', text => $any_error );
        }



( run in 3.099 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )