Mojolicious-Plugin-ValidateMoose

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Mojolicious-Plugin-ValidateMoose

0.02     Tue Apr 24 09:50:47 2012
       * Made unittest more sloppy

0.01     Mon Apr 23 21:50:21 2012
       * Add validate_moose() helper
       * Started project

MANIFEST  view on Meta::CPAN

inc/Module/Install/WriteAll.pm
lib/Mojolicious/Plugin/ValidateMoose.pm
Makefile.PL
MANIFEST			This list of files
MANIFEST.skip
META.yml
README
t/00-load.t
t/00-pod-coverage.t
t/00-pod.t
t/10-validate-moose.t

META.yml  view on Meta::CPAN

---
abstract: 'Can validate using Moose objects'
author:
  - 'Jan Henning Thorsen - jhthorsen at cpan.org'
build_requires:
  ExtUtils::MakeMaker: 6.36
configure_requires:
  ExtUtils::MakeMaker: 6.36
distribution_type: module
dynamic_config: 1
generated_by: 'Module::Install version 1.06'
license: perl

META.yml  view on Meta::CPAN

  directory:
    - inc
    - t
requires:
  Mojolicious: 2.8
  Moose: 2
resources:
  bugtracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-ValidateMoose
  homepage: https://metacpan.org/release/Mojolicious-Plugin-ValidateMoose
  license: http://dev.perl.org/licenses/
  repository: git://github.com/mojolicious-plugin-validatemoose
version: 0.02

Makefile.PL  view on Meta::CPAN

use inc::Module::Install;

name q(Mojolicious-Plugin-ValidateMoose);
all_from q(lib/Mojolicious/Plugin/ValidateMoose.pm);

requires q(Mojolicious) => 2.80;
requires q(Moose) => 2.00;

bugtracker q(http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-ValidateMoose);
homepage q(https://metacpan.org/release/Mojolicious-Plugin-ValidateMoose);
repository q(git://github.com/mojolicious-plugin-validatemoose);

auto_install;
WriteAll;

README  view on Meta::CPAN

NAME
    Mojolicious::Plugin::ValidateMoose - Can validate using Moose objects

VERSION
    0.02

DESCRIPTION
    This module is handy if you want to validate POST/GET parameters using
    Moose classes.

SYNOPSIS
        package MyApp;
        use Mojo::Base 'Mojolicious';
        sub startup {
            my $self = shift;

            $self->plugin('Mojolicious::Plugin::ValidateMoose');

            # ...
        }

        package MyApp::Root;
        use Mojo::Base 'Mojolicious::Controller';
        sub foo {
            my $self = shift;

            if($self->req->method eq 'POST') {
                if(my $obj = $self->validate_moose('My::Moose::Class')) {
                    # input validate, and $obj created from My::Moose::Class
                    # with the params set as attributes
                }
            }
        }

HELPERS
  validate_moose
        $obj = $controller->validate_moose($moose_class, \%args);
        $obj = $controller->validate_moose($moose_obj, \%args);

    Will either update an existing or create a new Moose object, if all the
    attributes gets validated. If any of the attributes is not updated with
    the right value from "param()", this method will set
    "invalid_form_elements" in the stash to a datastructure like this:

        {
            $param_name_a => 'required', # fixed
            $param_name_b => 'moose exception message', # custom
        }

    Example moose exception message:

        Validation failed for 'Int' with value "asd"

    The method will return empty list if it fail to validate the input.

METHODS
  register
    Will register the methods undef "HELPERS" as Mojolicious helpers.

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

AUTHOR

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

package Mojolicious::Plugin::ValidateMoose;

=head1 NAME

Mojolicious::Plugin::ValidateMoose - Can validate using Moose objects

=head1 VERSION

0.02

=head1 DESCRIPTION

This module is handy if you want to validate POST/GET parameters
using L<Moose> classes.

=head1 SYNOPSIS

    package MyApp;
    use Mojo::Base 'Mojolicious';
    sub startup {
        my $self = shift;

        $self->plugin('Mojolicious::Plugin::ValidateMoose');

        # ...
    }

    package MyApp::Root;
    use Mojo::Base 'Mojolicious::Controller';
    sub foo {
        my $self = shift;

        if($self->req->method eq 'POST') {
            if(my $obj = $self->validate_moose('My::Moose::Class')) {
                # input validate, and $obj created from My::Moose::Class
                # with the params set as attributes
            }
        }
    }

=cut

use Mojo::Base 'Mojolicious::Plugin';

our $VERSION = eval '0.02';

=head1 HELPERS

=head2 validate_moose

    $obj = $controller->validate_moose($moose_class, \%args);
    $obj = $controller->validate_moose($moose_obj, \%args);

Will either update an existing or create a new L<Moose> object, if all
the attributes gets validated. If any of the attributes is not updated
with the right value from C<param()>, this method will set
C<invalid_form_elements> in the stash to a datastructure like this:

    {
        $param_name_a => 'required', # fixed
        $param_name_b => 'moose exception message', # custom
    }

Example moose exception message:

    Validation failed for 'Int' with value "asd"

The method will return empty list if it fail to validate the input.

=cut

sub validate_moose {
    my($c, $class, $args) = @_;
    my $obj = ref $class ? $class : undef;
    my $meta = $class->meta;
    my(%constructor_args, %invalid);

    ATTRIBUTE:
    for my $attr (__get_attributes($meta, $args)) {
        my $name = $attr->name;
        my $type = $attr->type_constraint;
        my $value = $c->param($name);

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


=head2 register

Will register the methods undef L</HELPERS> as L<Mojolicious> helpers.

=cut

sub register {
    my($self, $app, $config) = @_;

    $app->helper(validate_moose => \&validate_moose);
}

=head1 COPYRIGHT & LICENSE

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

=head1 AUTHOR

Jan Henning Thorsen - jhthorsen at cpan.org

t/10-validate-moose.t  view on Meta::CPAN

use warnings;
use strict;
use lib qw(lib);
use Test::More;
use Mojolicious::Plugin::ValidateMoose;

plan tests => 15;

{
    my $plugin = Mojolicious::Plugin::ValidateMoose->new;
    my $validator = \&Mojolicious::Plugin::ValidateMoose::validate_moose;
    my $app = mock_app();
    my $obj;

    $plugin->register($app, {});
    is_deeply($app->{'helper'}, [validate_moose => $validator], 'validate_moose() was registered');

    eval { $validator->($app, 'TestClass') };
    like($@, qr{forgot to load}, 'class need to be loaded');

    mock_class();
    $validator->($app, 'TestClass');
    is_deeply($app->{'invalid_form_elements'}, { req => 'required' }, 'required attribute missing');

    $app->{'req'} = '';
    $validator->($app, 'TestClass');



( run in 1.232 second using v1.01-cache-2.11-cpan-437f7b0c052 )