| 1 | package QBit::Validator::Type::hash; | ||
| 2 | $QBit::Validator::Type::hash::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 => 'deps'}, | ||
| 11 | {name => 'fields'}, | ||
| 12 | {name => 'one_of'}, | ||
| 13 | {name => 'any_of'}, | ||
| 14 | {name => 'extra', required => TRUE}, | ||
| 15 | ]; | ||
| 16 | |||
| 17 | sub _get_options { | ||
| 18 | return clone($OPTIONS); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub _get_options_name { | ||
| 22 | return map {$_->{'name'}} @$OPTIONS; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub optional { | ||
| 26 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 27 | |||
| 28 | if ($template->{$option}) { | ||
| 29 | if (defined($data)) { | ||
| 30 | unless (ref($data) eq 'HASH') { | ||
| 31 | $qv->_add_error($template, gettext('Data must be HASH'), \@path_field); | ||
| 32 | |||
| 33 | return FALSE; | ||
| 34 | } | ||
| 35 | } else { | ||
| 36 | $qv->_add_ok(\@path_field); | ||
| 37 | |||
| 38 | return FALSE; | ||
| 39 | } | ||
| 40 | } else { | ||
| 41 | if (!defined($data)) { | ||
| 42 | $qv->_add_error($template, gettext('Data must be defined'), \@path_field); | ||
| 43 | |||
| 44 | return FALSE; | ||
| 45 | } else { | ||
| 46 | unless (ref($data) eq 'HASH') { | ||
| 47 | $qv->_add_error($template, gettext('Data must be HASH'), \@path_field); | ||
| 48 | |||
| 49 | return FALSE; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | return TRUE; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub deps { | ||
| 58 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 59 | |||
| 60 | throw Exception::Validator gettext('Option "%s" must be HASH', $option) unless ref($template->{$option}) eq 'HASH'; | ||
| 61 | |||
| 62 | my $no_error = TRUE; | ||
| 63 | |||
| 64 | foreach my $field (keys(%{$template->{$option}})) { | ||
| 65 | my @path = (@path_field, $field); | ||
| 66 | |||
| 67 | if (exists($data->{$field})) { | ||
| 68 | my $deps = $template->{$option}{$field}; | ||
| 69 | |||
| 70 | throw Exception::Validator gettext('You must specify the fields on which the field "%s"', $field) | ||
| 71 | unless defined($deps); | ||
| 72 | |||
| 73 | $deps = [$deps] unless ref($deps) eq 'ARRAY'; | ||
| 74 | |||
| 75 | foreach my $dep_field (@$deps) { | ||
| 76 | unless (exists($data->{$dep_field})) { | ||
| 77 | $qv->_add_error($template, gettext('Key "%s" depends from "%s"', $field, $dep_field), \@path); | ||
| 78 | |||
| 79 | $no_error = FALSE; | ||
| 80 | |||
| 81 | next; | ||
| 82 | } | ||
| 83 | |||
| 84 | my @dep_path = (@path_field, $dep_field); | ||
| 85 | |||
| 86 | $qv->_validation($data->{$dep_field}, $template->{'fields'}{$dep_field}, undef, @dep_path) | ||
| 87 | unless $qv->checked(\@dep_path); | ||
| 88 | |||
| 89 | $no_error = FALSE if $qv->has_error(\@dep_path); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | return $no_error; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub fields { | ||
| 98 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 99 | |||
| 100 | my $no_error = TRUE; | ||
| 101 | |||
| 102 | foreach my $field (keys(%{$template->{$option}})) { | ||
| 103 | my @path = (@path_field, $field); | ||
| 104 | |||
| 105 | if (!$template->{$option}{$field}{'optional'} && !exists($data->{$field})) { | ||
| 106 | $qv->_add_error($template, gettext('Key "%s" required', $field), \@path); | ||
| 107 | } | ||
| 108 | |||
| 109 | $qv->_validation($data->{$field}, $template->{$option}{$field}, undef, @path) | ||
| 110 | unless $qv->checked(\@path); | ||
| 111 | |||
| 112 | $no_error = FALSE if $qv->has_error(\@path); | ||
| 113 | } | ||
| 114 | |||
| 115 | return $no_error; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub extra { | ||
| 119 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 120 | |||
| 121 | my @extra_fields = grep {!$template->{'fields'}{$_}} keys(%$data); | ||
| 122 | |||
| 123 | if (@extra_fields && !$template->{$option}) { | ||
| 124 | $qv->_add_error($template, gettext('Extra fields: %s', join(', ', @extra_fields)), \@path_field); | ||
| 125 | |||
| 126 | return FALSE; | ||
| 127 | } | ||
| 128 | |||
| 129 | return TRUE; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub one_of { | ||
| 133 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 134 | |||
| 135 | throw Exception::Validator gettext('Option "%s" must be ARRAY', $option) | ||
| 136 | if ref($template->{$option}) ne 'ARRAY'; | ||
| 137 | |||
| 138 | my $min_size = 2; | ||
| 139 | |||
| 140 | throw Exception::Validator gettext('Option "%s" have size "%s", but expected size equal or more than "%s"', | ||
| 141 | $option, scalar(@{$template->{$option}}), $min_size) | ||
| 142 | if @{$template->{$option}} < $min_size; | ||
| 143 | |||
| 144 | my @received_fields = (); | ||
| 145 | foreach my $field (@{$template->{$option}}) { | ||
| 146 | throw Exception::Validator gettext('Key "%s" do not use in option "fields"', $field) | ||
| 147 | unless exists($template->{'fields'}{$field}); | ||
| 148 | |||
| 149 | push(@received_fields, $field) if exists($data->{$field}); | ||
| 150 | } | ||
| 151 | |||
| 152 | unless (@received_fields == 1) { | ||
| 153 | $qv->_add_error($template, gettext('Expected one key from: %s', join(', ', @{$template->{$option}})), | ||
| 154 | \@path_field); | ||
| 155 | |||
| 156 | return FALSE; | ||
| 157 | } | ||
| 158 | |||
| 159 | return TRUE; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub any_of { | ||
| 163 | my ($self, $qv, $data, $template, $option, @path_field) = @_; | ||
| 164 | |||
| 165 | throw Exception::Validator gettext('Option "%s" must be ARRAY', $option) | ||
| 166 | if ref($template->{$option}) ne 'ARRAY'; | ||
| 167 | |||
| 168 | my $min_size = 2; | ||
| 169 | |||
| 170 | throw Exception::Validator gettext('Option "%s" have size "%s", but expected size equal or more than "%s"', | ||
| 171 | $option, scalar(@{$template->{$option}}), $min_size) | ||
| 172 | if @{$template->{$option}} < $min_size; | ||
| 173 | |||
| 174 | my @received_fields = (); | ||
| 175 | foreach my $field (@{$template->{$option}}) { | ||
| 176 | throw Exception::Validator gettext('Key "%s" do not use in option "fields"', $field) | ||
| 177 | unless exists($template->{'fields'}{$field}); | ||
| 178 | |||
| 179 | push(@received_fields, $field) if exists($data->{$field}); | ||
| 180 | } | ||
| 181 | |||
| 182 | unless (@received_fields) { | ||
| 183 | $qv->_add_error($template, gettext('Expected any keys from: %s', join(', ', @{$template->{$option}})), | ||
| 184 | \@path_field); | ||
| 185 | |||
| 186 | return FALSE; | ||
| 187 | } | ||
| 188 | |||
| 189 | return TRUE; | ||
| 190 | } | ||
| 191 | |||
| 192 | TRUE; |