Giter Club home page Giter Club logo

dancer-plugin-datatransposevalidator's Introduction

NAME

Dancer::Plugin::DataTransposeValidator - Data::Transpose::Validator plugin for Dancer

VERSION

Version 0.009

SYNOPSIS

use Dancer::Plugin::DataTransposeValidator;

post '/' => sub {
    my $params = params;
    my $data = validator($params, 'rules-file');
    if ( $data->{valid} ) { ... }
}

DESCRIPTION

Dancer plugin for for Data::Transpose::Validator

FUNCTIONS

This module exports the single function validator.

validator( $params, $rules_file, @additional_args )

Arguments should be a hash reference of parameters to be validated and the name of the rules file to use. Any @additional_args are passed as arguments to the rules file only if it is a code reference. See "RULES FILE".

A hash reference with the following keys is returned:

  • valid

    A boolean 1/0 showing whether the parameters validated correctly or not.

  • values

    The transposed values as a hash reference.

  • errors

    A hash reference containing one key for each parameter which failed validation. See "errors_hash" in "CONFIGURATION" for an explanation of what the value of each parameter key will be.

  • css

    A hash reference containing one key for each parameter which failed validation. The value for each parameter is a css class. See "css_error_class" in "CONFIGURATION".

RULES FILE

The rules file format allows the "validator" to be configured using all options available in Data::Transpose::Validator. The rules file must contain a valid hash reference, e.g.:

{
    options => {
        stripwhite => 1,
        collapse_whitespace => 1,
        requireall => 0,
        unknown => "fail",
        missing => "undefine",
    },
    prepare => {
        email => {
            validator => "EmailValid",
            required => 1,
        },
        email2 => {
            validator => {
                class => "MyValidator::EmailValid",
                absolute => 1,
            }
        },
        field4 => {
            validator => {
                sub {
                    my $field = shift;
                    if ( $field =~ /^\d+/ && $field > 0 ) {
                        return 1;
                    }
                    else {
                        return ( undef, "Not a positive integer" );
                    }
                }
            }
        }
    }
}

Note that the value of the prepare key must be a hash reference since the array reference form of "prepare" in Data::Transpose::Validator is not supported.

As an alternative the rules file can contain a code reference, e.g.:

sub {
    my $username = shift;
    return {
        options => {
            stripwhite => 1,
        },
        prepare => {
            password => {
                validator => {
                    class => 'PasswordPolicy',
                    options => {
                        username  => $username,
                        minlength => 8,
                    }
                }
            }
        }
    };
}

The code reference receives the @additional_args passed to "validator". The code reference must return a valid hash reference.

CONFIGURATION

The following configuration settings are available (defaults are shown here):

plugins:
  DataTransposeValidator:
    css_error_class: has-error
    errors_hash: 0
    rules_dir: validation

css_error_class

The class returned as a value for parameters in the css key of the hash reference returned by "validator".

errors_hash

This can has a number of different values:

  • A false value (the default) means that only a single scalar error string will be returned for each parameter error. This will be the first error returned for the parameter by "errors_hash" in Data::Transpose::Validator.

  • joined

    All errors for a parameter will be returned joined by a full stop and a space.

  • arrayref

    All errors for a parameter will be returned as an array reference.

rules_dir

Subdirectory of "appdir" in Dancer::Config in which rules files are stored.

ACKNOWLEDGEMENTS

Alexey Kolganov for Dancer::Plugin::ValidateTiny which inspired a number of aspects of this plugin.

SEE ALSO

Dancer::Plugin::ValidateTiny Dancer::Plugin::FormValidator Dancer::Plugin::DataFu

AUTHOR

Peter Mottram (SysPete), <[email protected]>

CONTRIBUTORS

Slaven Rezić (SREZIC), <[email protected]>

COPYRIGHT AND LICENSE

Copyright 2015-2016 Peter Mottram (SysPete).

This program is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

dancer-plugin-datatransposevalidator's People

Contributors

syspete avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

perldancer

dancer-plugin-datatransposevalidator's Issues

Test failure on install

t/d1plugin.t ............. Can't locate object method "create" via package "Plack::Test" at t/d1plugin.t line 124, <DATA> line 1003.

This is Plack version 1.0028x.

Installation failure

Checking dependencies from MYMETA.json ...
Checking if you have Test::Exception 0 ... Yes (0.31)
Checking if you have Email::Valid 0 ... Yes (0.190)
Checking if you have Plack 1.0035 ... Yes (1.0042)
Checking if you have Class::Load 0 ... Yes (0.20)
Checking if you have File::Spec 0 ... Yes (3.47)
Checking if you have Moo 0 ... Yes (2.000002)
Checking if you have HTTP::Request::Common 0 ... Yes (6.04)
Checking if you have namespace::clean 0 ... Yes (0.24)
Checking if you have Test::More 0 ... Yes (1.001009)
Checking if you have JSON 0 ... Yes (2.53)
Checking if you have Test::Deep 0 ... Yes (1.123)
Checking if you have Data::Transpose 0.0020 ... Yes (0.0021)
Building and testing Dancer-Plugin-DataTransposeValidator-0.009 ... cp lib/Dancer2/Plugin/DataTransposeValidator.pm blib/lib/Dancer2/Plugin/DataTransposeValidator.pm
cp lib/Dancer/Plugin/DataTransposeValidator/Validator.pm blib/lib/Dancer/Plugin/DataTransposeValidator/Validator.pm
cp lib/Dancer/Plugin/DataTransposeValidator.pm blib/lib/Dancer/Plugin/DataTransposeValidator.pm
Manifying 3 pod documents
PERL_DL_NONLAZY=1 "/disk2/home/interch/perl5/perlbrew/perls/perl-5.16.2/bin/perl5.16.2" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/d1plugin.t t/d2plugin.t t/author/manifest.t t/author/pod-coverage.t t/author/pod-spelling.t t/author/pod.t
t/author/manifest.t ...... skipped: Author tests not required for installation
t/author/pod-coverage.t .. skipped: Author tests not required for installation
t/author/pod-spelling.t .. skipped: Author tests not required for installation
t/author/pod.t ........... skipped: Author tests not required for installation

#   Failed test 'post good foo'
#   at t/d1plugin.t line 136.
malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "<!DOCTYPE html PUBLI...") at (eval 31) line 161.
# Tests were run but no plan was declared and done_testing() was not seen.
# Looks like your test exited with 2 just after 3.
Recursive inheritance detected in package 'Types::Serialiser::BooleanBase' during global destruction.
t/d1plugin.t ............. 
Dubious, test returned 22 (wstat 5632, 0x1600)
Failed 1/3 subtests 
t/d2plugin.t ............. ok

Test Summary Report
-------------------
t/d1plugin.t           (Wstat: 5632 Tests: 3 Failed: 1)
  Failed test:  3
  Non-zero exit status: 22
  Parse errors: No plan found in TAP output
Files=6, Tests=14,  1 wallclock secs ( 0.03 usr  0.00 sys +  0.59 cusr  0.07 csys =  0.69 CPU)
Result: FAIL
Failed 1/6 test programs. 1/14 subtests failed.
make: *** [test_dynamic] Error 255
FAIL

t/d2plugin.t fails if Dancer2 is not installed

If Dancer2 is not installed:

Can't locate Dancer2.pm in @INC (you may need to install the Dancer2 module) (@INC contains: /home/cpansand/.cpan/build/2015091118/Dancer-Plugin-DataTransposeValidator-0.003-K2jsKq/blib/lib /home/cpansand/.cpan/build/2015091118/Dancer-Plugin-DataTransposeValidator-0.003-K2jsKq/blib/arch /usr/perl5.20.3-RC2p/lib/site_perl/5.20.3/amd64-freebsd /usr/perl5.20.3-RC2p/lib/site_perl/5.20.3 /usr/perl5.20.3-RC2p/lib/5.20.3/amd64-freebsd /usr/perl5.20.3-RC2p/lib/5.20.3 .) at t/d2plugin.t line 26.
BEGIN failed--compilation aborted at t/d2plugin.t line 26.
t/d2plugin.t ............. 
Dubious, test returned 2 (wstat 512, 0x200)
No subtests run 

It seems that the try_load_class(...) block should be in a BEGIN block to force the check at compile time, otherwise "use Dancer2" is executed earlier.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.