16 has 'value' => (isa => 'Num', is => 'ro');
18 around 'validate' => sub {
20 my ($self, $field) = @_;
21 return undef if $c->($self, $self->validation_value($field));
22 return $self->error_message;
25 sub validation_value {
26 my ($self, $field) = @_;
30 sub error_message { confess "Abstract method!" }
32 package Constraint::OnLength;
35 has 'units' => (isa => 'Str', is => 'ro');
37 override 'validation_value' => sub {
38 return length(super());
41 override 'error_message' => sub {
43 return super() . ' ' . $self->units;
51 package Constraint::AtLeast;
57 my ($self, $field) = @_;
58 ($field >= $self->value);
61 sub error_message { 'must be at least ' . (shift)->value; }
63 package Constraint::NoMoreThan;
69 my ($self, $field) = @_;
70 ($field <= $self->value);
73 sub error_message { 'must be no more than ' . (shift)->value; }
75 package Constraint::LengthNoMoreThan;
78 extends 'Constraint::NoMoreThan';
79 with 'Constraint::OnLength';
81 package Constraint::LengthAtLeast;
84 extends 'Constraint::AtLeast';
85 with 'Constraint::OnLength';
88 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
89 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
91 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
93 ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
94 is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
96 my $at_least_10 = Constraint::AtLeast->new(value => 10);
97 isa_ok($at_least_10, 'Constraint::AtLeast');
99 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
101 ok(!defined($at_least_10->validate(11)), '... validated correctly');
102 is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
106 my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
107 isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
108 isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
110 ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
111 ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
113 ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
114 is($no_more_than_10_chars->validate('foooooooooo'),
115 'must be no more than 10 chars',
116 '... validation failed correctly');
118 my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
119 isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
120 isa_ok($at_least_10_chars, 'Constraint::AtLeast');
122 ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
123 ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
125 ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
126 is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');