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