<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Validator::Custom::Result;
use Object::Simple -base;

use Carp 'croak';

# Attrbutes
has data =&gt; sub { {} },
  missing_params =&gt; sub { [] },
  raw_data  =&gt; sub { {} };

# Default message
our $DEFAULT_MESSAGE = 'Error message not specified';

sub error_reason {
  my ($self, $name) = @_;
  
  # Parameter name not specifed
  croak 'Parameter name must be specified'
    unless $name;
  
  # Error reason
  return $self-&gt;{_error_infos}-&gt;{$name}{reason};
}

sub has_invalid {
  my $self = shift;
  
  # Has invalid parameter?
  return keys %{$self-&gt;{_error_infos}} ? 1 : 0;
}

sub has_missing { @{shift-&gt;missing_params} ? 1 : 0 }

sub invalid_params {
  my $self = shift;
  
  # Invalid parameter names
  my @invalid_params;
  foreach my $name (@{$self-&gt;invalid_rule_keys}) {
      my $param = $self-&gt;{_error_infos}-&gt;{$name}{original_key};
      $param = [$param] unless ref $param eq 'ARRAY';
      push @invalid_params, @$param;
  }
  
  return \@invalid_params;
}

sub invalid_rule_keys {
  my $self = shift;
  
  # Invalid rule keys
  my $error_infos = $self-&gt;{_error_infos};
  my @invalid_rule_keys = sort { $error_infos-&gt;{$a}{position} &lt;=&gt;
    $error_infos-&gt;{$b}{position} } keys %$error_infos;
  
  return \@invalid_rule_keys;
}

sub is_valid {
  my ($self, $name) = @_;
  
  # Deprecated
  unless (defined $name) {
    warn "Validator::Custom::Result is_valid() method without argument" .
      " is deprecated. Use is_ok()";
    return $self-&gt;is_ok;
  }
  
  return exists $self-&gt;{_error_infos}-&gt;{$name} ? 0 : 1;
}

sub is_ok {
  my $self = shift;
  
  # Is ok?
  return !$self-&gt;has_invalid &amp;&amp; !$self-&gt;has_missing ? 1 : 0;
}

sub loose_data {
  my $self = shift;
  return {%{$self-&gt;raw_data}, %{$self-&gt;data}};
}

sub message {
  my ($self, $name) = @_;
  
  # Parameter name not specifed
  croak 'Parameter name must be specified'
    unless $name;
  
  return $self-&gt;{_error_infos}-&gt;{$name}{message}
    || $self-&gt;{_default_messages}{$name}
    || $DEFAULT_MESSAGE;
}

sub messages {
  my $self = shift;

  # Error messages
  my @messages;
  my $error_infos = $self-&gt;{_error_infos};
  my @keys = sort { $error_infos-&gt;{$a}{position} &lt;=&gt;
    $error_infos-&gt;{$b}{position} } keys %$error_infos;
  foreach my $name (@keys) {
    my $message = $self-&gt;message($name);
    push @messages, $message;
  }
  
  return \@messages;
}

sub messages_to_hash {
  my $self = shift;

  # Error messages
  my $messages = {};
  foreach my $name (keys %{$self-&gt;{_error_infos}}) {
    $messages-&gt;{$name} = $self-&gt;message($name);
  }
  
  return $messages;
}

sub to_hash {
  my $self = shift;
  
  # Result
  my $result = {};
  $result-&gt;{ok}      = $self-&gt;is_ok;
  $result-&gt;{invalid} = $self-&gt;has_invalid;
  $result-&gt;{missing} = $self-&gt;has_missing;
  $result-&gt;{missing_params} = $self-&gt;missing_params;
  $result-&gt;{messages} = $self-&gt;messages_to_hash;
  
  return $result;
}

# DEPRECATED!
has error_infos =&gt; sub { {} };
# DEPRECATED!
sub add_error_info {
  my $self = shift;
  warn "add_error_info method is DEPRECATED!";
  # Merge
  my $error_infos = ref $_[0] eq 'HASH' ? $_[0] : {@_};
  $self-&gt;error_infos({%{$self-&gt;error_infos}, %$error_infos});
  return $self;
}
# DEPRECATED!
sub error {
  warn "error_info method is DEPRECATED!";
  shift-&gt;message(@_)
}
# DEPRECATED!
sub errors { 
  warn "errors method is DEPRECATED!";
  return wantarray
       ? @{shift-&gt;messages(@_)}
       : shift-&gt;messages(@_);
}
# DEPRECATED!
sub errors_to_hash {
  warn "errors_to_hash method is DEPRECATED!";
  shift-&gt;messages_to_hash(@_)
}
# DEPRECATED!
sub invalid_keys {
  warn "invalid_keys method is DEPRECATED!";
  return wantarray
     ? @{shift-&gt;invalid_rule_keys(@_)}
     : shift-&gt;invalid_rule_keys(@_);
}
# DEPRECATED!
sub remove_error_info {
  my ($self, $key) = @_;
  warn "remove_error_info method is DEPRECATED!";
  # Remove
  delete $self-&gt;error_infos-&gt;{$key};
  return $self;
}

1;

=head1 NAME

Validator::Custom::Result - Result of validation

=head1 SYNOPSYS
    
  # Result
  my $result = $vc-&gt;validate($data, $rule);

  # Safety data
  my $safe_data = $result-&gt;data;

  # Chacke if the result is valid.
  # (this means result have neither missing nor invalid parameter)
  my $is_ok = $result-&gt;is_ok;

  # Check the existence of missing parameter
  my $has_missing_param = $result-&gt;has_missing;
  
  # Check if one parameter is valid
  my $title_is_valid = $result-&gt;is_valid('title');

  # Missing parameters(this is original keys)
  my $missing_params = $result-&gt;missing_params;
  
  # Invalid parameter names(this is original keys)
  my $invalid_params = $result-&gt;invalid_params;
  
  # Invalid rule keys
  my $invalid_rule_keys = $result-&gt;invalid_rule_keys;

  # A error message
  my $message = $result-&gt;message('title');

  # Error messages
  my $messages = $result-&gt;messages;

  # Error messages to hash ref
  my $messages_hash = $result-&gt;message_to_hash;
  
  # Result to hash
  my $rhash = $result-&gt;to_hash;
  
  # Raw data
  my $raw_data = $result-&gt;raw_data;
  

=head1 ATTRIBUTES

=head2 data

  my $data = $result-&gt;data;
  $result  = $result-&gt;data($data);

Get the data in the end state. L&lt;Validator::Custom&gt; has filtering ability
if you need.
The data passed to C&lt;validate()&gt; is converted to other data by filter.
You can get filtered data using C&lt;data()&gt;.

=head2 missing_params

  my $missing_params = $result-&gt;missing_params;
  $result            = $result-&gt;missing_params($missing_params);

You can get missing paramter names using C&lt;missing_params()&gt;.
In this example, return value is the following one.

=head2 raw_data

  my $data  = $result-&gt;raw_data;
  $result   = $result-&gt;raw_data($data);

Raw data soon after data_filter is excuted.

=head1 METHODS

L&lt;Validator::Custom::Result&gt; inherits all methods from L&lt;Object::Simple&gt;
and implements the following new ones.

=head2 error_reason

  $error_reason = $result-&gt;error_reason('title');

Error reason. This is constraint name.

=head2 has_invalid

  my $has_invalid = $result-&gt;has_invalid;

If at least one of parameter value is invalid,
C&lt;has_invalid()&gt; return true value.

=head2 has_missing

  my $has_missing_param = $result-&gt;has_missing;

If at least one of parameter names specified in the rule
is not found in the data,
C&lt;has_missing()&gt; return true value.

=head2 invalid_params

  my $invalid_params = $result-&gt;invalid_params;

Invalid raw data parameter names.

=head2 invalid_rule_keys

  my $invalid_rule_keys = $result-&gt;invalid_rule_keys;

Invalid rule keys

=head2 is_ok

  my $is_ok = $result-&gt;is_ok;

If you check the data is completely valid, use C&lt;is_ok()&gt;.
C&lt;is_ok()&gt; return true value
if invalid parameter values is not found and all parameter
names specified in the rule is found in the data.

=head2 is_valid

  my $title_is_valid = $result-&gt;is_valid('title');

Check if one paramter is valid.

=head2 loose_data

  my $loose_data = $result-&gt;loose_data;

Loose data, which is data merged C&lt;raw_data&gt; and C&lt;data&gt;

  # Loose data
  {%{$self-&gt;raw_data}, %{$self-&gt;data}}

=head2 message

  my $message = $result-&gt;message('title');

Get a message corresponding to the parameter name which value is invalid.

=head2 messages

  my $messages = $result-&gt;messages;

Get messages corresponding to the parameter names which value is invalid.
Messages keep the order of parameter names of the rule.

=head2 messages_to_hash

  my $messages = $result-&gt;messages_to_hash;

You can get the pairs of invalid parameter name and message
using C&lt;messages_to_hash()&gt;.

=head2 to_hash

  my $rhash = $result-&gt;to_hash;

Convert result information to hash reference.
The following keys is set.

  {
    ok =&gt;      $result-&gt;is_ok,
    missing =&gt; $result-&gt;has_missing,
    invalid =&gt; $result-&gt;has_invalid,
    missing_params =&gt; $result-&gt;missing_params,
    messages =&gt; $result-&gt;messages_to_hash
  }

=cut
</pre></body></html>