| 1 | package Valiant::Validator::Unique; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | has is_not_unique_msg => (is=>'ro', required=>1, default=>sub {_t 'is_not_unique'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | has unique_method => (is=>'ro', required=>1, default=>sub {'is_unique'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | has skip_if_undef => (is=>'ro', required=>1, default=>0); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 2 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||
| 15 | 2 | ▶ | if($arg eq '1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 2 | return +{}; | |||||||||||||||||||||||||||
| 17 | } else { | ||||||||||||||||||||||||||||
| 18 | 0 | return +{ unique_method => $arg }; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub validate_each { | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=7 SCAR=21.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 3 | my ($self, $record, $attribute, $value, $opts) = @_; | |||||||||||||||||||||||||||
| 24 | 3 | my $is_unique = $self->unique_method; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | return if !defined($value) and $self->skip_if_undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value and $self->skip_if_undef
MC/DC0%0/2
not defined $value and $self->skip_if_undef
not defined $value
$self->skip_if_undef
Truth table33%1/3
not(A) and B
A= not defined $valueB= $self->skip_if_undef
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | if($record->can("${attribute}_${is_unique}")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | 1 | my $attribute_is_unique = "${attribute}_${is_unique}"; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | $record->errors->add($attribute, $self->is_not_unique_msg, $opts) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | unless $record->$attribute_is_unique($attribute, $value, $opts); | ||||||||||||||||||||||||||||
| 32 | } elsif($record->can($is_unique)) { | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | $record->errors->add($attribute, $self->is_not_unique_msg, $opts) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | unless $record->$is_unique($attribute, $value, $opts); | ||||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | 0 | throw_exception MissingMethod => (object=>$record, method=>["${attribute}_${is_unique}", $is_unique]); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | 1; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Valiant::Validator::Unique - Verify that a value is unique to the record domain | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | package Local::Test::Unique; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | use Moo; | ||||||||||||||||||||||||||||
| 51 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has username => (is=>'ro'); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | validates username => (unique => 1); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub is_unique { | ||||||||||||||||||||||||||||
| 58 | my ($self, $attribute_name, $value, $opts) = @_; | ||||||||||||||||||||||||||||
| 59 | # Some custom logic that determines if the $attribute_name (in this case the value | ||||||||||||||||||||||||||||
| 60 | # will be 'username') has a unique value (determined by $value) in a given logical | ||||||||||||||||||||||||||||
| 61 | # domain (for example a Users table in a database). | ||||||||||||||||||||||||||||
| 62 | return 0; # Return true for 'is unique' and false otherwise. In this test case we always fail. | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my $object = Local::Test::Unique->new(username=>'John'); | ||||||||||||||||||||||||||||
| 66 | $object->validate; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | $VAR1 = { | ||||||||||||||||||||||||||||
| 71 | 'username' => [ | ||||||||||||||||||||||||||||
| 72 | 'Username choosen is not unique', | ||||||||||||||||||||||||||||
| 73 | ] | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Checks a value to see if it is unique by some custom logic that your class | ||||||||||||||||||||||||||||
| 79 | must provide. By default this must be a method on your class called "${attribute}_is_unique" | ||||||||||||||||||||||||||||
| 80 | or 'is_unique' (we check the attribute specific method first and you can change | ||||||||||||||||||||||||||||
| 81 | the method name via the 'unique_method' parameter) which will accept | ||||||||||||||||||||||||||||
| 82 | the name of the attribute and its current value. You must then provide some custom | ||||||||||||||||||||||||||||
| 83 | logic which determines if that pair is unique in a given domain (such as usernames in | ||||||||||||||||||||||||||||
| 84 | a User table in a database). Method must return true for unique and false otherwise. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | This validator will raise an error if you fail to provide the methods required. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | This validator supports the following attributes: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 unique_method | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Name of the method on your validating class which resolves the question of uniqueness. | ||||||||||||||||||||||||||||
| 95 | Default is to check first for "${attribute}_is_unique" and then "is_unique". Example: | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub is_unique { | ||||||||||||||||||||||||||||
| 98 | my ($self, $attribute_name, $value) = @_; | ||||||||||||||||||||||||||||
| 99 | my $found = $self->result_source->resultset->find({$attribute_name=>$value}); | ||||||||||||||||||||||||||||
| 100 | # If $found in table then the $value is not unique. | ||||||||||||||||||||||||||||
| 101 | return $found ? 0:1; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | If your class does not provide this method an exception will be thrown at runtime. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | B<NOTE> You shouldn't add an error to the errors list directly in this method, the error | ||||||||||||||||||||||||||||
| 107 | message will be added automatically for you. This is to make the unique test method | ||||||||||||||||||||||||||||
| 108 | uncoupled from L<Valiant> (For example you can use this method as a general uniqueness | ||||||||||||||||||||||||||||
| 109 | test in your business logic). | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 is_not_unique_msg | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | The error message / tag used when the value is not unique. Default is _t('is_not_unique') | ||||||||||||||||||||||||||||
| 114 | which resolves in English to 'chosen is not unique'. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 skip_if_undef | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Don't perform the uniqueness test if the value is undefined. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | validates attribute => ( unique => 1, ... ); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Which is the same as: | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | validates attribute => ( | ||||||||||||||||||||||||||||
| 129 | unique => +{ } | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Not a lot of typing saved but it makes this semantically consistent with other similar | ||||||||||||||||||||||||||||
| 133 | constraints. You can also customize the unique method using a string value: | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | validates attribute => ( unique => 'check_uniqueness', ... ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Which is the same as: | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | validates attribute => ( | ||||||||||||||||||||||||||||
| 140 | unique => +{ | ||||||||||||||||||||||||||||
| 141 | unique_method => 'check_uniqueness', | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 148 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | See L<Valiant> | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | See L<Valiant> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||