| 1 | package Valiant::Validations; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Class::Method::Modifiers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Valiant::Util 'debug'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Moo::_Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | require Moo::Role; | |||||||||||||||||||||||||||
| 12 | 40 | require Sub::Util; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | our @DEFAULT_ROLES = (qw(Valiant::Validates)); | |||||||||||||||||||||||||||
| 15 | 40 | our @DEFAULT_EXPORTS = (qw(validates validates_with push_to_i18n_lookup)); | |||||||||||||||||||||||||||
| 16 | 40 | our %Meta_Data = (); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 58 | ▶ | sub default_roles { @DEFAULT_ROLES } | ||||||||||||||||||||||||||
|
Subroutine
default_roles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 116 | ▶ | sub default_exports { @DEFAULT_EXPORTS } | ||||||||||||||||||||||||||
|
Subroutine
default_exports: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 58 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||
| 22 | 58 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 58 | my $target = caller; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 58 | ▶ | unless (Moo::Role->is_role($target)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 57 | my $orig = $target->can('with'); | |||||||||||||||||||||||||||
| 27 | 2 | ▶ | Moo::_Utils::_install_tracked($target, 'with', sub { | ||||||||||||||||||||||||||
|
Subroutine
with: called CC=3 SCAR=11.7
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | unless ($target->can('validations_metadata')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | 1 | $Meta_Data{$target}{'validations'} = \my @data; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | my $method = Sub::Util::set_subname "${target}::validations_metadata" => sub { @data }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 40 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 0 | *{"${target}::validations_metadata"} = $method; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless ($target->can('i18n_metadata')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | $Meta_Data{$target}{'i18n'} = \my @data; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $method = Sub::Util::set_subname "${target}::i18n_metadata" => sub { @data }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 37 | 40 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 0 | *{"${target}::i18n_metadata"} = $method; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | &$orig; | |||||||||||||||||||||||||||
| 41 | 57 | }); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 58 | foreach my $default_role ($class->default_roles) { | |||||||||||||||||||||||||||
| 45 | 58 | ▶ | next if Role::Tiny::does_role($target, $default_role); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 58 | debug 1, "Applying role '$default_role' to '$target'"; | |||||||||||||||||||||||||||
| 47 | 58 | Moo::Role->apply_roles_to_package($target, $default_role); | |||||||||||||||||||||||||||
| 48 | 58 | foreach my $export ($class->default_exports) { | |||||||||||||||||||||||||||
| 49 | 174 | debug 2, "Adding method '__${export}_for_exporter' as alias for '$export'"; | |||||||||||||||||||||||||||
| 50 | 174 | Moo::_Utils::_install_tracked($target, "__${export}_for_exporter", \&{"${target}::${export}"}); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my %cb = map { | ||||||||||||||||||||||||||||
| 55 | 58 | $_ => $target->can("__${_}_for_exporter"); | |||||||||||||||||||||||||||
| 56 | } $class->default_exports; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 58 | foreach my $exported_method (keys %cb) { | |||||||||||||||||||||||||||
| 59 | 54 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
push_to_i18n_lookup: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 60 | 54 | ▶ | if(Scalar::Util::blessed($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | return $cb{$exported_method}->(@_); | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 54 | return $cb{$exported_method}->($target, @_); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 174 | }; | |||||||||||||||||||||||||||
| 66 | 174 | Moo::_Utils::_install_tracked($target, $exported_method, $sub); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 101 | ▶ | Class::Method::Modifiers::install_modifier $target, 'around', 'has', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||
| 70 | 101 | my $orig = shift; | |||||||||||||||||||||||||||
| 71 | 101 | my ($attr, %opts) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 101 | my $method = \&{"${target}::validates"}; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 101 | ▶ | if(my $validates = delete $opts{validates}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | debug 1, "Found validation in attribute '$attr'"; | |||||||||||||||||||||||||||
| 77 | 0 | $method->($attr, @$validates); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 101 | return $orig->($attr, %opts); | |||||||||||||||||||||||||||
| 81 | 58 | ▶ | } if $target->can('has'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 104 | ▶ | sub _add_metadata { | ||||||||||||||||||||||||||
|
Subroutine
_add_metadata: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 85 | 104 | my ($target, $type, @add) = @_; | |||||||||||||||||||||||||||
| 86 | 104 | ▶ | my $store = $Meta_Data{$target}{$type} ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$Valiant::Validations::Meta_Data{$target}{$type} ||= do {
my @data;
if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) {
$target->can("around")->("${type}_metadata", sub {
my($orig, $self) = (shift(), shift());
$self->$orig(@_), @data;
}
);
}
else {
require Sub::Util;
my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub {
@data;
}
);
no strict 'refs';
*{"${target}::${type}_metadata";} = $method;
};
\@data
}
MC/DC0%0/2
$Valiant::Validations::Meta_Data{$target}{$type} ||= do {
my @data;
if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) {
$target->can("around")->("${type}_metadata", sub {
my($orig, $self) = (shift(), shift());
$self->$orig(@_), @data;
}
);
}
else {
require Sub::Util;
my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub {
@data;
}
);
no strict 'refs';
*{"${target}::${type}_metadata";} = $method;
};
\@data
}
$Valiant::Validations::Meta_Data{$target}{$type}
do {
my @data;
if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) {
$target->can("around")->("${type}_metadata", sub {
my($orig, $self) = (shift(), shift());
$self->$orig(@_), @data;
}
);
}
else {
require Sub::Util;
my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub {
@data;
}
);
no strict 'refs';
*{"${target}::${type}_metadata";} = $method;
};
\@data
}
Truth table66%2/3
A ||= B
A= $Valiant::Validations::Meta_Data{$target}{$type}B= do {
my @data;
if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) {
$target->can("around")->("${type}_metadata", sub {
my($orig, $self) = (shift(), shift());
$self->$orig(@_), @data;
}
);
}
else {
require Sub::Util;
my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub {
@data;
}
);
no strict 'refs';
*{"${target}::${type}_metadata";} = $method;
};
\@data
}
| |||||||||||||||||||||||||||||
| 87 | 50 | my @data; | |||||||||||||||||||||||||||
| 88 | 50 | ▶ | if (Moo::Role->is_role($target) or $target->can("${type}_metadata")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
"Moo::Role"->is_role($target) or $target->can("${type}_metadata")
MC/DC100%2/2
"Moo::Role"->is_role($target) or $target->can("${type}_metadata")
"Moo::Role"->is_role($target)
$target->can("${type}_metadata")
Truth table100%3/3
A or B
A= "Moo::Role"->is_role($target)B= $target->can("${type}_metadata")
| |||||||||||||||||||||||||||||
| 89 | 47 | ▶ | $target->can('around')->("${type}_metadata", sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 47 | my ($orig, $self) = (shift, shift); | |||||||||||||||||||||||||||
| 91 | 47 | ($self->$orig(@_), @data); | |||||||||||||||||||||||||||
| 92 | 3 | }); | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 47 | require Sub::Util; | |||||||||||||||||||||||||||
| 95 | 47 | ▶ | my $method = Sub::Util::set_subname "${target}::${type}_metadata" => sub { @data }; | ||||||||||||||||||||||||||
|
Subroutine
validations_metadata: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 40 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 47 | *{"${target}::${type}_metadata"} = $method; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 50 | \@data; | |||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||
| 101 | 104 | push @$store, @add; | |||||||||||||||||||||||||||
| 102 | 104 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 40 | 1; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 NAME | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Valiant::Validations - Adds a validation DSL and API to your Moo/se classes | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | package Local::Person; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | use Moo; | ||||||||||||||||||||||||||||
| 117 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | has name => (is=>'ro'); | ||||||||||||||||||||||||||||
| 120 | has age => (is=>'ro'); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | validates name => ( | ||||||||||||||||||||||||||||
| 123 | length => { | ||||||||||||||||||||||||||||
| 124 | maximum => 10, | ||||||||||||||||||||||||||||
| 125 | minimum => 3, | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | validates age => ( | ||||||||||||||||||||||||||||
| 130 | numericality => { | ||||||||||||||||||||||||||||
| 131 | is_integer => 1, | ||||||||||||||||||||||||||||
| 132 | less_than => 200, | ||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Validators on specific attributes can be added to the C<has> clause if you prefer: | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | package Local::Person; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | use Moo; | ||||||||||||||||||||||||||||
| 141 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | has name => ( | ||||||||||||||||||||||||||||
| 144 | is => 'ro', | ||||||||||||||||||||||||||||
| 145 | validates => [ | ||||||||||||||||||||||||||||
| 146 | length => { | ||||||||||||||||||||||||||||
| 147 | maximum => 10, | ||||||||||||||||||||||||||||
| 148 | minimum => 3, | ||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | ], | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | has age => ( | ||||||||||||||||||||||||||||
| 154 | is => 'ro', | ||||||||||||||||||||||||||||
| 155 | validates => [ | ||||||||||||||||||||||||||||
| 156 | numericality => { | ||||||||||||||||||||||||||||
| 157 | is_integer => 1, | ||||||||||||||||||||||||||||
| 158 | less_than => 200, | ||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||
| 160 | ], | ||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Using validations on objects: | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | my $person = Local::Person->new( | ||||||||||||||||||||||||||||
| 166 | name => 'Ja', | ||||||||||||||||||||||||||||
| 167 | age => 300, | ||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | $person->validate; | ||||||||||||||||||||||||||||
| 171 | $person->valid; # FALSE | ||||||||||||||||||||||||||||
| 172 | $person->invalid; # TRUE | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my %errors = $person->errors->to_hash(full_messages=>1); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # \%errors = +{ | ||||||||||||||||||||||||||||
| 177 | # age => [ | ||||||||||||||||||||||||||||
| 178 | # "Age must be less than 200", | ||||||||||||||||||||||||||||
| 179 | # ], | ||||||||||||||||||||||||||||
| 180 | # name => [ | ||||||||||||||||||||||||||||
| 181 | # "Name is too short (minimum is 3 characters)', | ||||||||||||||||||||||||||||
| 182 | # ], | ||||||||||||||||||||||||||||
| 183 | # }; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | See L<Valiant> for overall overview and L<Valiant::Validates> for additional API | ||||||||||||||||||||||||||||
| 186 | level documentation. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Using this package will apply the L<Valiant::Validates> role to your current class | ||||||||||||||||||||||||||||
| 191 | as well as import several class methods from that role. It also wraps the C<has> | ||||||||||||||||||||||||||||
| 192 | imported method so that you can add attribute validations as arguments to C<has> if | ||||||||||||||||||||||||||||
| 193 | you find that approach to be neater than calling C<validates>. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | You can override several class methods of this package if you need to create your | ||||||||||||||||||||||||||||
| 196 | own custom subclass. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 IMPORTS | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | The following subroutines are imported from L<Valiant::Validates> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head2 validates_with | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Accepts the name of a custom validator or a reference to a function, followed by a list | ||||||||||||||||||||||||||||
| 205 | of arguments. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | validates_with sub { | ||||||||||||||||||||||||||||
| 208 | my ($self, $opts) = @_; | ||||||||||||||||||||||||||||
| 209 | }; | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | validates_with 'SpecialValidator', arg1=>'foo', arg2=>'bar'; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | See C<validates_with> in either L<Valiant> or L<Valiant::Validates> for more. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 validates | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Create validations on an objects attributes. Accepts the name of an attributes (or an | ||||||||||||||||||||||||||||
| 218 | arrayref of names) followed by a list of validators and global options. Validators can | ||||||||||||||||||||||||||||
| 219 | be a subroutine reference, a type constraint or the name of a Validator class. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | validates name => sub { | ||||||||||||||||||||||||||||
| 222 | my ($self, $attribute, $value, $opts) = @_; | ||||||||||||||||||||||||||||
| 223 | $self->errors->add($attribute, "Invalid", $opts) if ... | ||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | validates name => ( | ||||||||||||||||||||||||||||
| 227 | length => { | ||||||||||||||||||||||||||||
| 228 | maximum => 10, | ||||||||||||||||||||||||||||
| 229 | minimum => 3, | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | See C<validates> in either L<Valiant> or L<Valiant::Validates> for more. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 METHODS | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | The following class methods are available for subclasses | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 default_roles | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Roles that are applied when using this class. Default is L<Valiant::Validates>. If | ||||||||||||||||||||||||||||
| 242 | you are subclassing and wish to apply more roles, or if you've made your own version | ||||||||||||||||||||||||||||
| 243 | of L<Valiant::Validates> you can override this method. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 default_exports | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Methods that are automatically exported into the calling package. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 ADDING VALIDATIONS TO OBJECTS | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Generally for best performance you will want to add validations to your classes, that way | ||||||||||||||||||||||||||||
| 252 | we can searching and precompile all the validations for optimized runtime. However you | ||||||||||||||||||||||||||||
| 253 | can add validations to objects after they are initialized and they will DTRT (add those | ||||||||||||||||||||||||||||
| 254 | validations only to the instance and not to the class). | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my $object = Local::Test::User->new(age=>5); | ||||||||||||||||||||||||||||
| 257 | $object->validates(age => (numericality => {greater_than => 10})); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Please note that you should expect some performance hit here since we need to search for | ||||||||||||||||||||||||||||
| 260 | and prepare the validation. So don't use this in hot parts of your code. Ideally you won't | ||||||||||||||||||||||||||||
| 261 | really need this feature and can work around using validation contexts but I saw now reason | ||||||||||||||||||||||||||||
| 262 | to prevent this from working for those unusual cases where it might be worth the price. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | L<Valiant>, L<Valiant::Validates> | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | See L<Valiant> | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | See L<Valiant> | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =cut | ||||||||||||||||||||||||||||