| 1 | package Exception::Validator; | ||
| 2 | $Exception::Validator::VERSION = '0.009'; | ||
| 3 | use base qw(Exception); | ||
| 4 | |||
| 5 | package FF; | ||
| 6 | $FF::VERSION = '0.009'; | ||
| 7 | use base qw(Exception::Validator); | ||
| 8 | |||
| 9 | package QBit::Validator; | ||
| 10 | $QBit::Validator::VERSION = '0.009'; | ||
| 11 | use qbit; | ||
| 12 | |||
| 13 | use base qw(QBit::Class); | ||
| 14 | |||
| 15 | __PACKAGE__->mk_ro_accessors(qw(data app)); | ||
| 16 | |||
| 17 | __PACKAGE__->mk_accessors(qw(template)); | ||
| 18 | |||
| 19 | my %available_fields = map {$_ => TRUE} qw(data template app throw pre_run); | ||
| 20 | |||
| 21 | sub init { | ||
| 22 | my ($self) = @_; | ||
| 23 | |||
| 24 | foreach (qw(data template)) { | ||
| 25 | throw Exception::Validator gettext('Expected "%s"', $_) unless exists($self->{$_}); | ||
| 26 | } | ||
| 27 | |||
| 28 | my @bad_fields = grep {!$available_fields{$_}} keys(%{$self}); | ||
| 29 | throw Exception::Validator gettext('Unknown options: %s', join(', ', @bad_fields)) | ||
| 30 | if @bad_fields; | ||
| 31 | |||
| 32 | if (exists($self->{'pre_run'})) { | ||
| 33 | throw Exception::Validator gettext('Option "pre_run" must be code') | ||
| 34 | if !defined($self->{'pre_run'}) || ref($self->{'pre_run'}) ne 'CODE'; | ||
| 35 | |||
| 36 | $self->{'pre_run'}($self); | ||
| 37 | } | ||
| 38 | |||
| 39 | $self->{'__CHECK_FIELDS__'} = {}; | ||
| 40 | |||
| 41 | my $data = $self->data; | ||
| 42 | my $template = $self->template; | ||
| 43 | |||
| 44 | $self->_validation($data, $template); | ||
| 45 | |||
| 46 | $self->throw_exception() if $self->has_errors && $self->{'throw'}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _validation { | ||
| 50 | my ($self, $data, $template, $no_check_options, @path_field) = @_; | ||
| 51 | |||
| 52 | throw Exception::Validator gettext('Key "template" must be HASH') | ||
| 53 | if !defined($template) || ref($template) ne 'HASH'; | ||
| 54 | |||
| 55 | $template->{'type'} //= ['scalar']; | ||
| 56 | |||
| 57 | $template->{'type'} = [$template->{'type'}] unless ref($template->{'type'}) eq 'ARRAY'; | ||
| 58 | |||
| 59 | my $already_check; | ||
| 60 | foreach my $type_name (@{$template->{'type'}}) { | ||
| 61 | my $type = $self->_get_type_by_name($type_name); | ||
| 62 | |||
| 63 | if ($type->can('get_template')) { | ||
| 64 | my $new_template = $type->merge_templates($template, $type->get_template()); | ||
| 65 | |||
| 66 | $self->_validation($data, $new_template, TRUE, @path_field); | ||
| 67 | } | ||
| 68 | |||
| 69 | last unless $type->check_options($self, $data, $template, $already_check, @path_field); | ||
| 70 | } | ||
| 71 | |||
| 72 | unless ($no_check_options) { | ||
| 73 | my $all_options = $self->_get_all_options_by_types($template->{'type'}); | ||
| 74 | |||
| 75 | my $diff = arrays_difference([keys(%$template)], $all_options); | ||
| 76 | |||
| 77 | throw Exception::Validator gettext('Unknown options: %s', join(', ', @$diff)) if @$diff; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _get_type_by_name { | ||
| 82 | my ($self, $type_name) = @_; | ||
| 83 | |||
| 84 | unless (exists($self->{'__TYPES__'}{$type_name})) { | ||
| 85 | my $type_class = 'QBit::Validator::Type::' . $type_name; | ||
| 86 | my $type_fn = "$type_class.pm"; | ||
| 87 | $type_fn =~ s/::/\//g; | ||
| 88 | |||
| 89 | try { | ||
| 90 | require $type_fn; | ||
| 91 | } | ||
| 92 | catch { | ||
| 93 | throw Exception::Validator gettext('Unknown type "%s"', $type_name); | ||
| 94 | }; | ||
| 95 | |||
| 96 | $self->{'__TYPES__'}{$type_name} = $type_class->new(); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $self->{'__TYPES__'}{$type_name}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub super_check { | ||
| 103 | my ($self, $type_name, @params) = @_; | ||
| 104 | |||
| 105 | my %types = map {$_ => TRUE} $self->_get_all_types($params[2]->{'type'}); | ||
| 106 | |||
| 107 | throw Exception::Validator gettext('You can not use sub "check" of type "%s" for this template', $type_name) | ||
| 108 | unless $types{$type_name}; | ||
| 109 | |||
| 110 | my $type = $self->_get_type_by_name($type_name); | ||
| 111 | |||
| 112 | throw Exception::Validator gettext('Do not exists sub "check" for type "%s"', $type_name) | ||
| 113 | unless $type->can('get_template'); | ||
| 114 | |||
| 115 | my $type_template = $type->get_template(); | ||
| 116 | |||
| 117 | throw Exception::Validator gettext('Do not exists sub "check" for type "%s"', $type_name) | ||
| 118 | unless exists($type_template->{'check'}); | ||
| 119 | |||
| 120 | throw Exception::Validator gettext('Option "check" must be code') | ||
| 121 | if !defined($type_template->{'check'}) || ref($type_template->{'check'}) ne 'CODE'; | ||
| 122 | |||
| 123 | $type_template->{'check'}(@params); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _get_all_types { | ||
| 127 | my ($self, $types) = @_; | ||
| 128 | |||
| 129 | $types //= ['scalar']; | ||
| 130 | $types = [$types] unless ref($types) eq 'ARRAY'; | ||
| 131 | |||
| 132 | my %uniq_types = map {$_ => TRUE} @$types; | ||
| 133 | |||
| 134 | foreach my $type_name (@$types) { | ||
| 135 | my $type = $self->_get_type_by_name($type_name); | ||
| 136 | |||
| 137 | if ($type->can('get_template')) { | ||
| 138 | my $type_template = $type->get_template(); | ||
| 139 | |||
| 140 | $uniq_types{$_} = TRUE foreach $self->_get_all_types($type_template->{'type'}); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return sort(keys(%uniq_types)); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _get_all_options_by_types { | ||
| 148 | my ($self, $types) = @_; | ||
| 149 | |||
| 150 | my @types_name = $self->_get_all_types($types); | ||
| 151 | |||
| 152 | my %uniq_options = (); | ||
| 153 | |||
| 154 | foreach my $type_name (@types_name) { | ||
| 155 | my $type = $self->_get_type_by_name($type_name); | ||
| 156 | |||
| 157 | $uniq_options{$_} = TRUE foreach $type->get_all_options_name(); | ||
| 158 | } | ||
| 159 | |||
| 160 | return [keys(%uniq_options)]; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub throw_exception { | ||
| 164 | my ($self) = @_; | ||
| 165 | |||
| 166 | throw Exception::Validator $self->get_all_errors; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _add_error { | ||
| 170 | my ($self, $template, $error, $field, %opts) = @_; | ||
| 171 | |||
| 172 | my $key = $self->_get_key($field); | ||
| 173 | |||
| 174 | if ($opts{'check_error'}) { | ||
| 175 | $self->{'__CHECK_FIELDS__'}{$key}{'error'} = { | ||
| 176 | msgs => [$error], | ||
| 177 | path => $field // [] | ||
| 178 | }; | ||
| 179 | } elsif ($self->has_error($field)) { | ||
| 180 | push(@{$self->{'__CHECK_FIELDS__'}{$key}{'error'}{'msgs'}}, $error) | ||
| 181 | unless exists($template->{'msg'}); | ||
| 182 | } else { | ||
| 183 | $self->{'__CHECK_FIELDS__'}{$key}{'error'} = { | ||
| 184 | msgs => [exists($template->{'msg'}) ? $template->{'msg'} : $error], | ||
| 185 | path => $field // [] | ||
| 186 | }; | ||
| 187 | } | ||
| 188 | |||
| 189 | delete($self->{'__CHECK_FIELDS__'}{$key}{'ok'}) if exists($self->{'__CHECK_FIELDS__'}{$key}{'ok'}); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub get_all_errors { | ||
| 193 | my ($self) = @_; | ||
| 194 | |||
| 195 | my $error = ''; | ||
| 196 | |||
| 197 | $error .= join("\n", map {@{$_->{'msgs'}}} $self->get_fields_with_error()); | ||
| 198 | |||
| 199 | return $error; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub get_error { | ||
| 203 | my ($self, $field) = @_; | ||
| 204 | |||
| 205 | my $key = $self->_get_key($field); | ||
| 206 | |||
| 207 | my $error = ''; | ||
| 208 | foreach ($self->get_fields_with_error()) { | ||
| 209 | $error = join("\n", @{$_->{'msgs'}}) if $key eq $self->_get_key($_->{'path'}); | ||
| 210 | } | ||
| 211 | |||
| 212 | return $error; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub get_fields_with_error { | ||
| 216 | my ($self) = @_; | ||
| 217 | |||
| 218 | return map {$self->{'__CHECK_FIELDS__'}{$_}{'error'}} | ||
| 219 | grep {$self->{'__CHECK_FIELDS__'}{$_}{'error'}} sort keys(%{$self->{'__CHECK_FIELDS__'}}); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _add_ok { | ||
| 223 | my ($self, $field) = @_; | ||
| 224 | |||
| 225 | return if $self->checked($field) && $self->has_error($field); | ||
| 226 | |||
| 227 | $self->{'__CHECK_FIELDS__'}{$self->_get_key($field)}{'ok'} = TRUE; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub checked { | ||
| 231 | my ($self, $field) = @_; | ||
| 232 | |||
| 233 | return exists($self->{'__CHECK_FIELDS__'}{$self->_get_key($field)}); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub has_error { | ||
| 237 | my ($self, $field) = @_; | ||
| 238 | |||
| 239 | return exists($self->{'__CHECK_FIELDS__'}{$self->_get_key($field)}{'error'}); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub has_errors { | ||
| 243 | my ($self) = @_; | ||
| 244 | |||
| 245 | return !!$self->get_fields_with_error(); | ||
| 246 | } | ||
| 247 | |||
| 248 | sub _get_key { | ||
| 249 | my ($self, $path_field) = @_; | ||
| 250 | |||
| 251 | $path_field //= []; | ||
| 252 | |||
| 253 | $path_field = [$path_field] unless ref($path_field) eq 'ARRAY'; | ||
| 254 | |||
| 255 | return join(' => ', @$path_field); | ||
| 256 | } | ||
| 257 | |||
| 258 | TRUE; | ||
| 259 | |||
| 260 | __END__ |