« blib/lib/Plack/Middleware/APISchema/RequestValidator.pm
↑ Summary
blib/lib/Plack/Middleware/APISchema/ResponseValidator.pm »
| 1 | package APISchema::Validator::Result; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # core | ||
| 6 | use List::MoreUtils qw(all); | ||
| 7 | |||
| 8 | # cpan | ||
| 9 | use Hash::Merge::Simple (); | ||
| 10 | use Class::Accessor::Lite ( | ||
| 11 | new => 1, | ||
| 12 | ); | ||
| 13 | |||
| 14 | sub new_valid { | ||
| 15 | my ($class, @targets) = @_; | ||
| 16 | return $class->new(values => { map { ($_ => [1]) } @targets }); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub new_error { | ||
| 20 | my ($class, $target, $err) = @_; | ||
| 21 | return $class->new(values => { ( $target // '' ) => [ undef, $err] }); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _values { shift->{values} // {} } | ||
| 25 | |||
| 26 | sub merge { | ||
| 27 | my ($self, $other) = @_; | ||
| 28 | $self->{values} = Hash::Merge::Simple::merge( | ||
| 29 | $self->_values, | ||
| 30 | $other->_values, | ||
| 31 | ); | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub errors { | ||
| 36 | my $self = shift; | ||
| 37 | return +{ map { | ||
| 38 | my $err = $self->_values->{$_}->[1]; | ||
| 39 | $err ? ( $_ => $err ) : (); | ||
| 40 | } keys %{$self->_values} }; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub is_valid { | ||
| 44 | my $self = shift; | ||
| 45 | return all { $self->_values->{$_}->[0] } keys %{$self->_values}; | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; |