| 1 | package QBit::Validator::Type::array; | ||
| 2 | $QBit::Validator::Type::array::VERSION = '0.009'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Validator::Type); | ||
| 6 | |||
| 7 | #order is important | ||
| 8 | my $OPTIONS = [ | ||
| 9 | {name => 'optional', required => TRUE}, | ||
| 10 | {name => 'size_min'}, | ||
| 11 | {name => 'size'}, | ||
| 12 | {name => 'size_max'}, | ||
| 13 | {name => 'all'}, | ||
| 14 | {name => 'contents'}, | ||
| 15 | ]; | ||
| 16 | |||
| 17 | sub _get_options { | ||
| 18 | my ($self) = @_; | ||
| 19 | |||
| 20 | return clone($OPTIONS); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _get_options_name { | ||
| 24 | return map {$_->{'name'}} @$OPTIONS; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub optional { | ||
| 28 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 29 | |||
| 30 | if ($template->{$option}) { | ||
| 31 | if (defined($data)) { | ||
| 32 | unless (ref($data) eq 'ARRAY') { | ||
| 33 | $qv->_add_error($template, gettext('Data must be ARRAY'), \@path_field); | ||
| 34 | |||
| 35 | return FALSE; | ||
| 36 | } | ||
| 37 | } else { | ||
| 38 | $qv->_add_ok(\@path_field); | ||
| 39 | |||
| 40 | return FALSE; | ||
| 41 | } | ||
| 42 | } else { | ||
| 43 | if (!defined($data)) { | ||
| 44 | $qv->_add_error($template, gettext('Data must be defined'), \@path_field); | ||
| 45 | |||
| 46 | return FALSE; | ||
| 47 | } else { | ||
| 48 | unless (ref($data) eq 'ARRAY') { | ||
| 49 | $qv->_add_error($template, gettext('Data must be ARRAY'), \@path_field); | ||
| 50 | |||
| 51 | return FALSE; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return TRUE; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub size_min { | ||
| 60 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 61 | |||
| 62 | throw Exception::Validator gettext('Option "%s" must be positive number', $option) | ||
| 63 | if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/; | ||
| 64 | |||
| 65 | if (@$data < $template->{$option}) { | ||
| 66 | $qv->_add_error($template, gettext('Data size "%s" less then "%s"', scalar(@$data), $template->{$option}), | ||
| 67 | \@path_field); | ||
| 68 | |||
| 69 | return FALSE; | ||
| 70 | } | ||
| 71 | |||
| 72 | return TRUE; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub size { | ||
| 76 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 77 | |||
| 78 | throw Exception::Validator gettext('Key "%s" must be positive number', $option) | ||
| 79 | if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/; | ||
| 80 | |||
| 81 | unless (@$data == $template->{$option}) { | ||
| 82 | $qv->_add_error($template, gettext('Data size "%s" not equal "%s"', scalar(@$data), $template->{$option}), | ||
| 83 | \@path_field); | ||
| 84 | |||
| 85 | return FALSE; | ||
| 86 | } | ||
| 87 | |||
| 88 | return TRUE; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub size_max { | ||
| 92 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 93 | |||
| 94 | throw Exception::Validator gettext('Key "%s" must be positive number', $option) | ||
| 95 | if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/; | ||
| 96 | |||
| 97 | if (@$data > $template->{$option}) { | ||
| 98 | $qv->_add_error($template, gettext('Data size "%s" more than "%s"', scalar(@$data), $template->{$option}), | ||
| 99 | \@path_field); | ||
| 100 | |||
| 101 | return FALSE; | ||
| 102 | } | ||
| 103 | |||
| 104 | return TRUE; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub all { | ||
| 108 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 109 | |||
| 110 | throw Exception::Validator gettext('Options "all" and "contents" can not be used together') | ||
| 111 | if exists($template->{$option}) && exists($template->{'contents'}); | ||
| 112 | |||
| 113 | throw Exception::Validator gettext('Option "%s" must be HASH') | ||
| 114 | if !defined($template->{$option}) || ref($template->{$option}) ne 'HASH'; | ||
| 115 | |||
| 116 | my $num = 0; | ||
| 117 | foreach (@$data) { | ||
| 118 | my @path = (@path_field, $num); | ||
| 119 | |||
| 120 | $qv->_validation($_, $template->{'all'}, undef, @path); | ||
| 121 | |||
| 122 | return FALSE if $qv->has_error(\@path); | ||
| 123 | |||
| 124 | $num++; | ||
| 125 | } | ||
| 126 | |||
| 127 | return TRUE; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub contents { | ||
| 131 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 132 | |||
| 133 | throw Exception::Validator gettext('Option "%s" must be ARRAY', $option) | ||
| 134 | if !defined($template->{$option}) || ref($template->{$option}) ne 'ARRAY'; | ||
| 135 | |||
| 136 | if (@$data != @{$template->{$option}}) { | ||
| 137 | $qv->_add_error($template, | ||
| 138 | gettext('Data size "%s" no equal "%s"', scalar(@$data), scalar(@{$template->{$option}})), \@path_field); | ||
| 139 | |||
| 140 | return FALSE; | ||
| 141 | } | ||
| 142 | |||
| 143 | my $num = 0; | ||
| 144 | foreach (@$data) { | ||
| 145 | my @path = (@path_field, $num); | ||
| 146 | |||
| 147 | $qv->_validation($_, $template->{$option}[$num], undef, @path); | ||
| 148 | |||
| 149 | return FALSE if $qv->has_error(\@path); | ||
| 150 | |||
| 151 | $num++; | ||
| 152 | } | ||
| 153 | |||
| 154 | return TRUE; | ||
| 155 | } | ||
| 156 | |||
| 157 | TRUE; |