/usr/share/perl5/Validation/Class/Directive.pm is in libvalidation-class-perl 7.900057-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | # ABSTRACT: Base Class for Validation Class Directives
package Validation::Class::Directive;
use strict;
use warnings;
use Validation::Class::Util;
use Carp 'confess';
our $VERSION = '7.900057'; # VERSION
# defaults
has 'mixin' => 0;
has 'field' => 0;
has 'multi' => 0;
has 'message' => '%s could not be validated';
has 'validator' => sub { sub{1} };
has 'dependencies' => sub {{ normalization => [], validation => [] }};
has 'name' => sub {
my ($self) = @_;
my $name = ref $self || $self;
my $regexp = qr/Validation::Class::Directive::(.*)$/;
$name = $1 if $name =~ $regexp;
$name =~ s/([a-z])([A-Z])/$1_$2/g;
$name =~ s/\W/_/g;
$name = lc $name;
return $name;
};
sub new {
my $class = shift;
my $arguments = $class->build_args(@_);
confess
"Error creating directive without a name, specifying a name is " .
"required to instatiate a new non-subclass directive"
if 'Validation::Class::Directive' eq $class && ! $arguments->{name}
;
my $self = bless {}, $class;
while (my($key, $value) = each %{$arguments}) {
$self->$key($value);
}
return $self;
}
sub error {
my ($self, $proto, $field, $param, @tokens) = @_;
my $name = $field->label || $field->name;
unshift @tokens, $name;
# use custom field-level error message
if ($field->error) {
$field->errors->add($field->error);
}
# use field-level error message override
elsif (defined $field->{messages} && $field->{messages}->{$self->name}) {
my $message = $field->{messages}->{$self->name};
$field->errors->add(sprintf($message, @tokens));
}
# use class-level error message override
elsif ($proto->messages->has($self->name)) {
my $message = $proto->messages->get($self->name);
$field->errors->add(sprintf($message, @tokens));
}
# use directive error message
else {
$field->errors->add(sprintf($self->message, @tokens));
}
return $self;
}
sub validate {
my $self = shift;
my ($proto, $field, $param) = @_;
my $context = $proto->stash->{'validation.context'};
# nasty hack, we need a better way !!!
$self->validator->($context, $field, $proto->params);
return $self;
}
1;
__END__
=pod
=head1 NAME
Validation::Class::Directive - Base Class for Validation Class Directives
=head1 VERSION
version 7.900057
=head1 SYNOPSIS
package Validation::Class::Directive::CheckBlacklist;
use base 'Validation::Class::Directive';
use strict;
use warnings;
use Validation::Class::Util;
use File::Slurp;
has 'mixin' => 0;
has 'field' => 1;
has 'multi' => 0;
has 'message' => '%s has been blacklisted';
sub validate {
my $self = shift;
my ($proto, $field, $param) = @_;
if (defined $field->{check_blacklist} && $param) {
# is the parameter value blacklisted?
my @blacklist = read_file('/blacklist.txt');
$self->error if grep { $param =~ /^$_$/ } @blacklist;
}
}
1;
... in your validation class:
package MyApp::Person;
use Validation::Class;
field ip_address => {
required => 1,
check_blacklist => 1
};
1;
... in your application:
package main;
use MyApp::Person;
my $person = MyApp::Person->new(ip_address => '0.0.0.0');
unless ($person->validates('ip_address')) {
# handle validation error
}
=head1 DESCRIPTION
You can extend Validation::Class by creating your own validation rules
(directives). Validation::Class::Directive provides a base-class for you to use
when creating new directive classes. Please see L<Validation::Class::Directives>
for a complete list of core directives.
=head1 AUTHOR
Al Newkirk <anewkirk@ana.io>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|