Data-MuForm
view release on metacpan or search on metacpan
t/validation/types.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Test::Exception;
use Data::MuForm::Types (':assert', ':all');
isa_ok( PositiveNum, 'Type::Tiny', 'PositiveNum' );
ok( assert_PositiveNum("5"), 'PositiveNum works (pass)' );
like( exception { assert_PositiveNum(-1) }, qr/positive number/, 'PositiveNum fails' );
{
package Test::Form;
use Moo;
use Data::MuForm::Meta;
extends 'Data::MuForm';
use Data::MuForm::Types (':all');
use Types::Standard -types;
use Type::Utils;
my $GreaterThan10 = declare 'GreaterThan10',
as Int,
where { $_ > 10 },
message { "This number ($_) is not greater than 10" };
has 'posint' => ( is => 'rw', isa => PositiveInt);
has_field 'test' => ( apply => [ PositiveInt ] );
has_field 'text_gt' => ( apply=> [ $GreaterThan10 ] );
has_field 'text_both' => ( apply => [ PositiveInt, $GreaterThan10 ] );
has_field 'field_wtype' => ( apply => [
{ type => PositiveInt, message => 'Not a positive number' } ] );
has_field 'state' => ( apply => [ State ] );
}
my $form = Test::Form->new;
ok( $form, 'get form');
$form->posint(100);
my $params = {
test => '-100',
text_gt => 5,
text_both => 6,
state => 'GG',
field_wtype => '-10',
};
$form->process($params);
ok( !$form->validated, 'form did not validate' );
ok( $form->field('test')->has_errors, 'errors on MooseX type');
ok( $form->field('text_gt')->has_errors, 'errors on subtype');
ok( $form->field('text_both')->has_errors, 'errors on both');
ok( $form->field('field_wtype')->has_errors, 'errors on type with message');
ok( $form->field('state')->has_errors, 'errors on state' );
$params = {
test => 100,
text_gt => 21,
text_both => 15,
( run in 1.199 second using v1.01-cache-2.11-cpan-54e63673c56 )