File: 04-profile-usages.t

package info (click to toggle)
libvalidation-class-perl 7.900057-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 355
  • sloc: perl: 21,493; makefile: 2
file content (117 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
BEGIN {
    use FindBin;
    use lib $FindBin::Bin. "/modules/";
}

use Test::More tests => 12;

package MyValAlt;

use Validation::Class;

mixin ID => {
    required   => 1,
    min_length => 1,
    max_length => 11
};

mixin TEXT => {
    required   => 1,
    min_length => 1,
    max_length => 255,
    filters    => [qw/trim strip/]
};

mixin UTEXT => {
    required   => 1,
    min_length => 1,
    max_length => 255,
    filters    => 'uppercase'
};

field id => {
    mixin => 'ID',
    label => 'Object ID',
    error => 'Object ID error'
};

field name => {
    mixin   => 'TEXT',
    label   => 'Object Name',
    error   => 'Object Name error',
    filters => ['uppercase']
};

field handle => {
    mixin   => 'UTEXT',
    label   => 'Object Handle',
    error   => 'Object Handle error',
    filters => [qw/trim strip/]
};

field email => {
    mixin      => 'TEXT',
    label      => 'Object Email',
    error      => 'Object Email error',
    max_length => 500
};

field email_confirm => {
    mixin_field => 'email',
    label       => 'Object Email Confirm',
    error       => 'Object Email confirmation error',
    min_length  => 5
};

profile email_change => sub {

    my ($self, $hash) = @_;

    $self->validate('+email', '+email_confirm');

    return $self->error_count && keys %{$hash} ? 1 : 0;

};

package main;

use MyVal;

my $v1 = MyValAlt->new(params => {});

ok $v1, 'initialization successful';
ok !$v1->validate_profile('email_change'),
  'email_change profile did not validate';
ok $v1->error_count == 2, '2 errors encountered on failure';

$v1->params->{email}         = 'abc';
$v1->params->{email_confirm} = 'abc';

ok !$v1->validate_profile('email_change'),
  'email_change profile did not validate';
ok $v1->validate_profile('email_change', {this => 'that'}),
  'email_change profile validated OK';

my $v2 = MyVal->new(params => {});

ok !$v2->validate_profile('new_ticket'), 'new_ticket profile did not validate';
ok $v2->error_count == 2, '2 errors encountered on failure';

$v2->param(name => 'the dude');

ok !$v2->validate_profile('new_ticket'), 'new_ticket profile did not validate';
ok $v2->error_count == 1, '1 errors encountered on failure';

$v2->param(description => 'the bomb dot com');

ok $v2->validate_profile('new_ticket'), 'new_ticket profile validated OK';
ok $v2->error_count == 0, 'NO errors set';

my $v3 = MyVal->new(
    params => {
        'person.name'        => 'the dude',
        'ticket.description' => 'hot diggidy dog'
    }
);

ok $v3->validate_profile('new_ticket'), 'new_ticket profile validated OK';