| 1 | package QBit::Validator::Type::hash; | ||
| 2 | $QBit::Validator::Type::hash::VERSION = '0.011'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Validator::Type); | ||
| 6 | |||
| 7 | use Exception::Validator; | ||
| 8 | |||
| 9 | #order is important | ||
| 10 | my $OPTIONS = [ | ||
| 11 | {name => 'optional', required => TRUE}, | ||
| 12 | {name => 'deps'}, | ||
| 13 | {name => 'fields'}, | ||
| 14 | {name => 'one_of'}, | ||
| 15 | {name => 'any_of'}, | ||
| 16 | {name => 'extra', required => TRUE}, | ||
| 17 | ]; | ||
| 18 | |||
| 19 | sub _get_options { | ||
| 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 'HASH') { | ||
| 33 | $qv->_add_error($template, gettext('Data must be HASH'), \@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 'HASH') { | ||
| 49 | $qv->_add_error($template, gettext('Data must be HASH'), \@path_field); | ||
| 50 | |||
| 51 | return FALSE; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return TRUE; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub deps { | ||
| 60 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 61 | |||
| 62 | throw Exception::Validator gettext('Option "%s" must be HASH', $option) unless ref($template->{$option}) eq 'HASH'; | ||
| 63 | |||
| 64 | my $no_error = TRUE; | ||
| 65 | |||
| 66 | foreach my $field (keys(%{$template->{$option}})) { | ||
| 67 | my @path = (@path_field, $field); | ||
| 68 | |||
| 69 | if (exists($data->{$field})) { | ||
| 70 | my $deps = $template->{$option}{$field}; | ||
| 71 | |||
| 72 | my ($dep_fields, $cases, $set_template); | ||
| 73 | if (ref($deps) eq 'HASH') { | ||
| 74 | throw Exception::Validator gettext('You must specify option "fields"') | ||
| 75 | unless exists($deps->{'fields'}); | ||
| 76 | |||
| 77 | $dep_fields = $deps->{'fields'}; | ||
| 78 | |||
| 79 | my @exists_options = grep {exists($deps->{$_})} qw(cases set_template); | ||
| 80 | throw Exception::Validator gettext('You must specify option "cases" or "set_template"') | ||
| 81 | if @exists_options != 1; | ||
| 82 | |||
| 83 | $cases = $deps->{'cases'}; | ||
| 84 | $set_template = $deps->{'set_template'}; | ||
| 85 | } else { | ||
| 86 | $dep_fields = $deps; | ||
| 87 | } | ||
| 88 | |||
| 89 | throw Exception::Validator gettext('You must specify the fields on which the field "%s"', $field) | ||
| 90 | unless defined($dep_fields); | ||
| 91 | |||
| 92 | $dep_fields = [$dep_fields] unless ref($dep_fields) eq 'ARRAY'; | ||
| 93 | |||
| 94 | my @dep_fields_with_errors = (); | ||
| 95 | my $has_errors = FALSE; | ||
| 96 | foreach my $dep_field (@$dep_fields) { | ||
| 97 | unless (exists($data->{$dep_field})) { | ||
| 98 | push(@dep_fields_with_errors, $dep_field); | ||
| 99 | |||
| 100 | next; | ||
| 101 | } | ||
| 102 | |||
| 103 | my @dep_path = (@path_field, $dep_field); | ||
| 104 | |||
| 105 | $qv->_validation($data->{$dep_field}, $template->{'fields'}{$dep_field}, undef, @dep_path) | ||
| 106 | unless $qv->checked(\@dep_path); | ||
| 107 | |||
| 108 | $has_errors = TRUE if $qv->has_error(\@dep_path); | ||
| 109 | } | ||
| 110 | |||
| 111 | if (@dep_fields_with_errors) { | ||
| 112 | $qv->_add_error($template, | ||
| 113 | gettext('Key "%s" depends from: %s', $field, join(',', map {"\"$_\""} @dep_fields_with_errors)), | ||
| 114 | \@path); | ||
| 115 | |||
| 116 | return FALSE; | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($has_errors) { | ||
| 120 | $no_error = FALSE; | ||
| 121 | next; | ||
| 122 | } | ||
| 123 | |||
| 124 | if (defined($cases)) { | ||
| 125 | throw Exception::Validator gettext('Option "%s" must be ARRAY', 'cases') if ref($cases) ne 'ARRAY'; | ||
| 126 | |||
| 127 | foreach my $case (@$cases) { | ||
| 128 | my $case_template = | ||
| 129 | {%{$case->[0]}, map {$_ => {skip => TRUE}} grep {!exists($case->[0]{$_})} @$dep_fields}; | ||
| 130 | |||
| 131 | my $case_qv = $qv->new( | ||
| 132 | data => {map {$_ => $data->{$_}} @$dep_fields}, | ||
| 133 | template => {type => 'hash', fields => $case_template} | ||
| 134 | ); | ||
| 135 | |||
| 136 | unless ($case_qv->has_errors) { | ||
| 137 | $template->{'fields'}{$field} = $case->[1]; | ||
| 138 | |||
| 139 | last; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | if (defined($set_template)) { | ||
| 145 | throw Exception::Validator gettext('Option "%s" must be code', 'set_template') | ||
| 146 | if ref($set_template) ne 'CODE'; | ||
| 147 | |||
| 148 | try { | ||
| 149 | my $new_template = $set_template->($qv, $data); | ||
| 150 | |||
| 151 | if (defined($new_template) && ref($new_template) eq 'HASH') { | ||
| 152 | $template->{'fields'}{$field} = $new_template; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | catch { | ||
| 156 | throw Exception::Validator gettext('Internal error'); | ||
| 157 | }; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | return $no_error; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub fields { | ||
| 166 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 167 | |||
| 168 | my $no_error = TRUE; | ||
| 169 | |||
| 170 | foreach my $field (keys(%{$template->{$option}})) { | ||
| 171 | my @path = (@path_field, $field); | ||
| 172 | |||
| 173 | if (!$template->{$option}{$field}{'optional'} && !exists($data->{$field})) { | ||
| 174 | $qv->_add_error($template, gettext('Key "%s" required', $field), \@path); | ||
| 175 | } | ||
| 176 | |||
| 177 | $qv->_validation($data->{$field}, $template->{$option}{$field}, undef, @path) | ||
| 178 | unless $qv->checked(\@path); | ||
| 179 | |||
| 180 | $no_error = FALSE if $qv->has_error(\@path); | ||
| 181 | } | ||
| 182 | |||
| 183 | return $no_error; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub extra { | ||
| 187 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 188 | |||
| 189 | my @extra_fields = grep {!$template->{'fields'}{$_}} keys(%$data); | ||
| 190 | |||
| 191 | if (@extra_fields && !$template->{$option}) { | ||
| 192 | $qv->_add_error($template, gettext('Extra fields: %s', join(', ', @extra_fields)), \@path_field); | ||
| 193 | |||
| 194 | return FALSE; | ||
| 195 | } | ||
| 196 | |||
| 197 | return TRUE; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub one_of { | ||
| 201 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 202 | |||
| 203 | throw Exception::Validator gettext('Option "%s" must be ARRAY', $option) | ||
| 204 | if ref($template->{$option}) ne 'ARRAY'; | ||
| 205 | |||
| 206 | my $min_size = 2; | ||
| 207 | |||
| 208 | throw Exception::Validator gettext('Option "%s" have size "%s", but expected size equal or more than "%s"', | ||
| 209 | $option, scalar(@{$template->{$option}}), $min_size) | ||
| 210 | if @{$template->{$option}} < $min_size; | ||
| 211 | |||
| 212 | my @received_fields = (); | ||
| 213 | foreach my $field (@{$template->{$option}}) { | ||
| 214 | throw Exception::Validator gettext('Key "%s" do not use in option "fields"', $field) | ||
| 215 | unless exists($template->{'fields'}{$field}); | ||
| 216 | |||
| 217 | push(@received_fields, $field) if exists($data->{$field}); | ||
| 218 | } | ||
| 219 | |||
| 220 | unless (@received_fields == 1) { | ||
| 221 | $qv->_add_error($template, gettext('Expected one key from: %s', join(', ', @{$template->{$option}})), | ||
| 222 | \@path_field); | ||
| 223 | |||
| 224 | return FALSE; | ||
| 225 | } | ||
| 226 | |||
| 227 | return TRUE; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub any_of { | ||
| 231 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 232 | |||
| 233 | throw Exception::Validator gettext('Option "%s" must be ARRAY', $option) | ||
| 234 | if ref($template->{$option}) ne 'ARRAY'; | ||
| 235 | |||
| 236 | my $min_size = 2; | ||
| 237 | |||
| 238 | throw Exception::Validator gettext('Option "%s" have size "%s", but expected size equal or more than "%s"', | ||
| 239 | $option, scalar(@{$template->{$option}}), $min_size) | ||
| 240 | if @{$template->{$option}} < $min_size; | ||
| 241 | |||
| 242 | my @received_fields = (); | ||
| 243 | foreach my $field (@{$template->{$option}}) { | ||
| 244 | throw Exception::Validator gettext('Key "%s" do not use in option "fields"', $field) | ||
| 245 | unless exists($template->{'fields'}{$field}); | ||
| 246 | |||
| 247 | push(@received_fields, $field) if exists($data->{$field}); | ||
| 248 | } | ||
| 249 | |||
| 250 | unless (@received_fields) { | ||
| 251 | $qv->_add_error($template, gettext('Expected any keys from: %s', join(', ', @{$template->{$option}})), | ||
| 252 | \@path_field); | ||
| 253 | |||
| 254 | return FALSE; | ||
| 255 | } | ||
| 256 | |||
| 257 | return TRUE; | ||
| 258 | } | ||
| 259 | |||
| 260 | TRUE; |