Giter Site home page Giter Site logo

dbix-class-validationtoo's Introduction

NAME

DBIx::CLass::ValidationToo

DESCRIPTION

Database validation plugin for DBIx::Class Result classess

CONFIGURATION

Include this package as a component in your result class. For each column definition needing validation, define a validation attribute.

__PACKAGE__->load_components('ValidationToo');

__PACKAGE__->add_column(
  column_name => {
    data_type => 'varchar',
    size => 100,
    validation => {
      is_required => 1,
      type => [qw(email unique)],
      validate_sub => sub {
        my ( $row, $value, $col ) = @_;
        return "John is not allowed" if $value =~ /john/i;
      }
    }
  }
);

VALIDATION

$row = $schema->resultset('Thing')->create( \%thing );

# Validate a single column against a given value
$err_msg = $row->validate( email => '[email protected]' );

# Validate multiple columns against a href of values
$err_href = $row->validate({
  foo => 'bar',
  bar => 'foo',
});

# Validate the values stored in all columns of the $row object
$row->foo('bar');
$row->bar('foo');
$err_href = $row->validate;

say "Column $_ - Error $err_href{$_}" for keys %$err_href;

METHODS

validate

Validates every column value currently stored in the $row object. Returns a hashref of error messages keyed on column name.

# Validate all data for all columns in object
$err_href = $row->validate()

# Validate a single column with a given value
$err_msg = $row->validate('column' => $value);

# Validate a hash of values
$err_href = $row->validate(\%values);

validate_object

Validate all data for all columns in object

Upon validation error, returns a hashref of column error messages

Otherwise, returns undef

validate_href \%columns

Given a hashref of column values, validate the hashref. Data contained in the validation hashref will not be stored onto the row object.

Upon validation error, returns a hashref of column error messages

Otherwise, returns undef

validate_column $column, [$value]

Validate a value for a column

If $value is not set, validates the value stored in the row object. If $value is set, $value will not be stored onto the row object.

Returns an error message string, or undef

Columns with no validation attribute hashes always pass validation.

Validation process:

  • Check for is_required
  • Check each given validation type in the given order
  • Run validation_sub
  • for varchar/char, check length <= size attribute

get_validation_hash $column

Return the validation attribute hashref for $column

test_password_strength $password \%params

Check password strength with Data::Password

Since passwords should be stored hashed and salted, this is not used as a standard validation type. After including this component, you can call the test_password method on the row object.

Defaults for Data::Password can be overwritten with a parameter href. The default values are shown here:

Usage:

my %params = (
  FOLLOWING  => 4, # forbid abcd, or 2345
  GROUPS     => 3, # forbid aaaa, or bbbb
  MINLEN     => 8,
  MAXLEN     => 64,
  DICTIONARY => 1, # enable dictionary checking
)
my $error_msg = $row->test_password( $password, \%params );

VALIDATION TYPES

  • int

  • integer

  • float

  • money

  • bool

  • shortname

    For the URL identifier fields. Allows - numbers, letters, spaces, dashes, underscores

  • email - Check E-Mail is valid

  • percentage

    Check value is an int between 0-100

  • template - Check Template fragment for errors

  • text - Allow any text input

  • time - SQL time column

    Value is in 24h time, with or without a seconds field.

    e.g: 12:45 or 19:05:00 or 12:3 or 11:30:25

  • unique - value does not exist on any other record in the table.

  • date - Check for validity of a date useing DateTime::Format::DateParse

  • datetime - Column must contain a DateTime object

    For use with DBIx::Class::InflateColumn::DateTime

  • as_phone

    Check string is a 10-digit number, after all non-numeric characters are discarded -- Assuming InflateColumn will be formatting the number as it's retreived, and strippping formatting as it's saved

TODO

Create a way a column title string can be defined, and then used in error messages. "First Name is a required field" looks better than "first_name is a required field"

Make the size check for varchar/char columns optional

AUTHOR

Mitch Jackson [email protected]

LICENSE

This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.

dbix-class-validationtoo's People

Contributors

mitchjacksontech avatar

Watchers

 avatar  avatar

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.