Skip to content

interchange/Dancer-Plugin-DataTransposeValidator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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), <peter@sysnix.com>

CONTRIBUTORS

Slaven Rezić (SREZIC), <slaven@rezic.de>

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.

About

Perl Dancer/Dancer2 plugin for Data::Transpose::Validator

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages