File Coverage

blib/lib/WebService/MinFraud/Validator.pm
Criterion Covered Total %
statement 53 53 100.0
branch 4 4 100.0
condition n/a
subroutine 25 25 100.0
pod 1 1 100.0
total 83 83 100.0


line stmt bran cond sub pod time code
1             package WebService::MinFraud::Validator;
2              
3 2     2   112063 use Moo;
  2         10614  
  2         10  
4 2     2   2215 use namespace::autoclean;
  2         11529  
  2         13  
5              
6             our $VERSION = '1.010000';
7              
8 2     2   953 use Data::Delete 0.05;
  2         41032  
  2         117  
9 2     2   17 use Try::Tiny;
  2         4  
  2         139  
10 2     2   591 use Types::Standard qw( InstanceOf Object HashRef );
  2         80120  
  2         20  
11              
12 2     2   2722 use WebService::MinFraud::Validator::Chargeback;
  2         7  
  2         75  
13 2     2   859 use WebService::MinFraud::Validator::Score;
  2         8  
  2         87  
14 2     2   1070 use WebService::MinFraud::Validator::Insights;
  2         7  
  2         61  
15 2     2   743 use WebService::MinFraud::Validator::Factors;
  2         5  
  2         59  
16 2     2   13 use WebService::MinFraud::Validator::FraudService;
  2         4  
  2         1070  
17              
18             has _deleter => (
19             is => 'lazy',
20             isa => InstanceOf ['Data::Delete'],
21 56     56   10645 builder => sub { Data::Delete->new },
22             handles => { _delete => 'delete' },
23             );
24              
25             has _validator_chargeback => (
26             is => 'lazy',
27             isa => InstanceOf ['WebService::MinFraud::Validator::Chargeback'],
28 15     15   393 builder => sub { WebService::MinFraud::Validator::Chargeback->new },
29             );
30              
31             has _validator_score => (
32             is => 'lazy',
33             isa => InstanceOf ['WebService::MinFraud::Validator::Score'],
34 15     15   429 builder => sub { WebService::MinFraud::Validator::Score->new },
35             );
36              
37             has _validator_insights => (
38             is => 'lazy',
39             isa => InstanceOf ['WebService::MinFraud::Validator::Insights'],
40 13     13   366 builder => sub { WebService::MinFraud::Validator::Insights->new },
41             );
42              
43             has _validator_factors => (
44             is => 'lazy',
45             isa => InstanceOf ['WebService::MinFraud::Validator::Factors'],
46 13     13   355 builder => sub { WebService::MinFraud::Validator::Factors->new },
47             );
48              
49             has _validator_fraud_service => (
50             is => 'lazy',
51             isa => InstanceOf ['WebService::MinFraud::Validator::FraudService'],
52 1     1   41 builder => sub { WebService::MinFraud::Validator::FraudService->new },
53             );
54              
55             has _dispatch_validator => (
56             is => 'lazy',
57             isa => HashRef,
58             builder => sub {
59 56     56   618 my $self = shift;
60             return {
61 50     50   1218 fraud_service => sub { $self->_validator_fraud_service },
62 15     15   575 score => sub { $self->_validator_score },
63 20     20   691 chargeback => sub { $self->_validator_chargeback },
64 13     13   502 factors => sub { $self->_validator_factors },
65 13     13   467 insights => sub { $self->_validator_insights },
66 56         1486 };
67             },
68             );
69              
70             sub validate_request {
71 111     111 1 120428 my ( $self, $request, $path ) = @_;
72              
73 111 100       372 if ( !defined $path ) {
74 50         94 $path = 'fraud_service';
75             }
76              
77             try {
78 111     111   6825 $self->_dispatch_validator->{$path}()->assert_valid($request);
79             }
80             catch {
81             my @error_strings = map {
82 31 100       161 'VALUE: '
83             . ( defined $_->value ? $_->value : 'undef' )
84             . ' caused ERROR: '
85             . $_->stringify
86 31     31   3308 } @{ $_->failures };
  31         79  
87 31         10814 my $all_error_strings = join "\n", @error_strings;
88 31         596 die $all_error_strings;
89 111         875 };
90             }
91              
92             1;
93              
94             # ABSTRACT: Validation for the minFraud requests
95              
96             __END__
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             WebService::MinFraud::Validator - Validation for the minFraud requests
105              
106             =head1 VERSION
107              
108             version 1.010000
109              
110             =head1 SYNOPSIS
111              
112             use 5.010;
113              
114             use WebService::MinFraud::Validator;
115              
116             my $validator = WebService::MinFraud::Validator->new;
117             my $request = { device => { ip_address => '24.24.24.24' } };
118             $validator->validate_request($request, 'score'); # takes an optional 'path'
119              
120             =head1 DESCRIPTION
121              
122             This module defines the request schema for the minFraud API. In addition, it
123             provides a C<validate_request> method that is used to validate any request
124             passed to the C<score>, C<insights>, C<factors>, or C<chargeback> methods.
125              
126             =head1 METHODS
127              
128             =head2 validate_request
129              
130             my $validator = WebService::MinFraud::Validator->new;
131             my $request = { ip => '24.24.24.24' };
132             $validator->validate_request($request, 'chargeback');
133              
134             $request = { device => { ip_address => '24.24.24.24' } };
135             $validator->validate_request($request); # by default will use WebService::MinFraud::Validator::FraudService
136              
137             This method takes a minFraud request as a HashRef and validates it against the
138             minFraud request schema for the specified API endpoint. A second optional argument can be used
139             to specify the schema to use, C<socre>, C<insights>, C<factors>, C<chargeback>,
140             or C<fraud_service>. If the request HashRef fails validation, an exception
141             is thrown, which is a string containing all of the validation errors.
142              
143             =head1 SUPPORT
144              
145             Bugs may be submitted through L<https://github.com/maxmind/minfraud-api-perl/issues>.
146              
147             =head1 AUTHOR
148              
149             Mateu Hunter <mhunter@maxmind.com>
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is copyright (c) 2015 - 2020 by MaxMind, Inc.
154              
155             This is free software; you can redistribute it and/or modify it under
156             the same terms as the Perl 5 programming language system itself.
157              
158             =cut