| 1 | package XAS::Service::Profiles::Search; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | 0 | ▶ | use XAS::Utils ':validation'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use XAS::Constants 'ARRAYREF'; | ||
| 7 | use Data::FormValidator::Constraints::MethodsFactory ':set'; | ||
| 8 | |||
| 9 | #use Data::Dumper; | ||
| 10 | |||
| 11 | # ----------------------------------------------------------------- | ||
| 12 | # Public Methods | ||
| 13 | # ----------------------------------------------------------------- | ||
| 14 | |||
| 15 | # ----------------------------------------------------------------- | ||
| 16 | # Private Methods | ||
| 17 | # ----------------------------------------------------------------- | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | my ($fields) = validate_params(\@_, [ | ||
| 22 | { type => ARRAYREF }, | ||
| 23 | ]); | ||
| 24 | |||
| 25 | my $pager = { | ||
| 26 | filters => ['trim'], | ||
| 27 | required => [qw( start limit )], | ||
| 28 | constraint_methods => { | ||
| 29 | start => qr/^\d+$/, | ||
| 30 | limit => qr/^\d+$/, | ||
| 31 | }, | ||
| 32 | msgs => { | ||
| 33 | constraints => { | ||
| 34 | start => 'must be a number', | ||
| 35 | limit => 'must be a number', | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | my $filter = { | ||
| 41 | filters => ['trim'], | ||
| 42 | required => ['type', 'field', 'value'], | ||
| 43 | optional => ['comparison'], | ||
| 44 | constraint_methods => { | ||
| 45 | type => FV_set(1, qw( string date list boolean deleted )), | ||
| 46 | comparison => FV_set(1, qw( lt le gt ge eq lk be)), | ||
| 47 | field => FV_set(1, @$fields), | ||
| 48 | }, | ||
| 49 | msgs => { | ||
| 50 | constraints => { | ||
| 51 | type => 'must be either "boolean", "string", "date" or "list"', | ||
| 52 | comparison => 'must be either "lt", "le", "gt", "ge", "eq", "lk" or "be"', | ||
| 53 | field => 'must be a single word', | ||
| 54 | } | ||
| 55 | } | ||
| 56 | }; | ||
| 57 | |||
| 58 | my $sort = { | ||
| 59 | filters => ['trim'], | ||
| 60 | required => ['field', 'direction'], | ||
| 61 | constraint_methods => { | ||
| 62 | field => FV_set(1, @$fields ), | ||
| 63 | direction => FV_set(1, qw( ASC DESC )), | ||
| 64 | }, | ||
| 65 | msgs => { | ||
| 66 | constraints => { | ||
| 67 | field => 'sort fields are invalid', | ||
| 68 | direction => 'sort directions must be either ASC or DESC' | ||
| 69 | } | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | my $group = { | ||
| 74 | filters => ['trim'], | ||
| 75 | required => ['field'], | ||
| 76 | optional => ['direction'], | ||
| 77 | constraint_methods => { | ||
| 78 | field => FV_set(1, @$fields ), | ||
| 79 | direction => FV_set(1, qw( ASC DESC )), | ||
| 80 | }, | ||
| 81 | msgs => { | ||
| 82 | constraints => { | ||
| 83 | field => 'group fields are invalid', | ||
| 84 | direction => 'group directions must be either ASC or DESC' | ||
| 85 | } | ||
| 86 | } | ||
| 87 | }; | ||
| 88 | |||
| 89 | my $profiles = { | ||
| 90 | sort => $sort, | ||
| 91 | group => $group, | ||
| 92 | pager => $pager, | ||
| 93 | filter => $filter, | ||
| 94 | }; | ||
| 95 | |||
| 96 | return $profiles; | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | =head1 NAME | ||
| 103 | |||
| 104 | XAS::Service::Profiles::Search - A class for creating standard validation profiles. | ||
| 105 | |||
| 106 | =head1 SYNOPSIS | ||
| 107 | |||
| 108 | my $params = { | ||
| 109 | start => 0, | ||
| 110 | limit => 25, | ||
| 111 | }; | ||
| 112 | |||
| 113 | my $fields = qw(id server queue requestor typeofrequest status startdatetime); | ||
| 114 | my $search = XAS::Service::Profiles::Search->new($fields); | ||
| 115 | my $validate = XAS::Service::Validate->new($search); | ||
| 116 | my $results = $validate->check($params, 'pager'); | ||
| 117 | |||
| 118 | if ($results->has_invalid) { | ||
| 119 | |||
| 120 | my @invalids = $results->invalid; | ||
| 121 | |||
| 122 | foreach $invalid (@invalids) { | ||
| 123 | |||
| 124 | printf("%s: %s\n", $invalid, $results->msgs->{$invalide}); | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | =head1 DESCRIPTION | ||
| 131 | |||
| 132 | This module creates a standardized | ||
| 133 | L<Data::FormValidator|https://metacpan.org/pod/Data::FormValidator> validation | ||
| 134 | profile for searches. | ||
| 135 | |||
| 136 | =head1 METHODS | ||
| 137 | |||
| 138 | =head2 new($fields) | ||
| 139 | |||
| 140 | Initializes the vaildation profile. | ||
| 141 | |||
| 142 | =over 4 | ||
| 143 | |||
| 144 | =item B<$field> | ||
| 145 | |||
| 146 | An array ref of field names that may appear in search requests. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =head1 SEE ALSO | ||
| 151 | |||
| 152 | =over 4 | ||
| 153 | |||
| 154 | =item L<XAS::Service|XAS::Service> | ||
| 155 | |||
| 156 | =item L<XAS|XAS> | ||
| 157 | |||
| 158 | =back | ||
| 159 | |||
| 160 | =head1 AUTHOR | ||
| 161 | |||
| 162 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 163 | |||
| 164 | =head1 COPYRIGHT AND LICENSE | ||
| 165 | |||
| 166 | Copyright (c) 2012-2016 Kevin L. Esteb | ||
| 167 | |||
| 168 | This is free software; you can redistribute it and/or modify it under | ||
| 169 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 170 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 171 | |||
| 172 | =cut | ||