| 1 | package Poz::Types::array; | ||
| 2 | use 5.032; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp (); | ||
| 6 | use Try::Tiny; | ||
| 7 | use parent 'Poz::Types'; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($class, $validator) = @_; | ||
| 11 | if (!$validator->isa('Poz::Types')) { | ||
| 12 | Carp::croak("Invalid validator: is not a subclass of Poz::Types"); | ||
| 13 | } | ||
| 14 | my $self = bless { | ||
| 15 | __validator__ => $validator, | ||
| 16 | __as__ => undef, | ||
| 17 | __rules__ => [], | ||
| 18 | __optional__ => 0, | ||
| 19 | __default__ => undef, | ||
| 20 | transform => [], | ||
| 21 | }, $class; | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub as { | ||
| 26 | my ($self, $typename) = @_; | ||
| 27 | $self->{__as__} = $typename; | ||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub parse { | ||
| 32 | my ($self, $data) = @_; | ||
| 33 | my ($valid, $errors) = $self->safe_parse($data); | ||
| 34 | if ($errors) { | ||
| 35 | my $error_message = _errors_to_string($errors); | ||
| 36 | Carp::croak($error_message); | ||
| 37 | } | ||
| 38 | return $valid; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub safe_parse { | ||
| 42 | Carp::croak "Must handle error" unless wantarray; | ||
| 43 | |||
| 44 | my ($self, $data) = @_; | ||
| 45 | my @parsed = (); | ||
| 46 | my @errors = (); | ||
| 47 | if (defined $self->{__default__} && !defined $data) { | ||
| 48 | $data = $self->{__default__}; | ||
| 49 | } | ||
| 50 | if (!defined $data) { | ||
| 51 | if ($self->{__optional__}) { | ||
| 52 | return (undef, undef); | ||
| 53 | } else { | ||
| 54 | push @errors, { | ||
| 55 | key => undef, | ||
| 56 | error => "Required" | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | } elsif (ref($data) ne 'ARRAY') { | ||
| 60 | push @errors, { | ||
| 61 | key => undef, | ||
| 62 | error => "Invalid data: is not arrayref" | ||
| 63 | }; | ||
| 64 | } else { | ||
| 65 | for my $rule (@{$self->{__rules__}}) { | ||
| 66 | my $err = $rule->($self, $data); | ||
| 67 | if (defined $err) { | ||
| 68 | push @errors, { | ||
| 69 | key => undef, | ||
| 70 | error => $err, | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | for my $i (0 .. $#{$data}) { | ||
| 75 | my $v = $self->{__validator__}; | ||
| 76 | my $val = $data->[$i]; | ||
| 77 | try { | ||
| 78 | my $_parsed = $v->parse($val); | ||
| 79 | push @parsed, $_parsed; | ||
| 80 | } catch { | ||
| 81 | my $error_message = $_; | ||
| 82 | $error_message =~ s/ at .+ line [0-9]+\.\n//; | ||
| 83 | push @errors, { | ||
| 84 | key => $i, | ||
| 85 | error => $error_message, | ||
| 86 | }; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | if (scalar(@errors) > 0) { | ||
| 91 | return (undef, [@errors]) | ||
| 92 | } | ||
| 93 | my $classname = $self->{__as__}; | ||
| 94 | my $valid = $classname ? bless [@parsed], $classname : [@parsed]; | ||
| 95 | return ($valid, undef); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _errors_to_string { | ||
| 99 | my $errors = shift; | ||
| 100 | my @error_strings = (); | ||
| 101 | for my $error (@$errors) { | ||
| 102 | my $message = $error->{key} ? | ||
| 103 | sprintf("%s on key `%s`", $error->{error}, $error->{key}) : | ||
| 104 | sprintf("%s", $error->{error}); | ||
| 105 | push @error_strings, $message; | ||
| 106 | } | ||
| 107 | return join(", and ", @error_strings); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub min { | ||
| 111 | my ($self, $min) = @_; | ||
| 112 | push @{$self->{__rules__}}, sub { | ||
| 113 | my ($self, $value) = @_; | ||
| 114 | return "Array is too short" if scalar(@$value) < $min; | ||
| 115 | return; | ||
| 116 | }; | ||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub max { | ||
| 121 | my ($self, $max) = @_; | ||
| 122 | push @{$self->{__rules__}}, sub { | ||
| 123 | my ($self, $value) = @_; | ||
| 124 | return "Array is too long" if scalar(@$value) > $max; | ||
| 125 | return; | ||
| 126 | }; | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub length { | ||
| 131 | my ($self, $length) = @_; | ||
| 132 | push @{$self->{__rules__}}, sub { | ||
| 133 | my ($self, $value) = @_; | ||
| 134 | return "Array is not of length $length" if scalar(@$value) != $length; | ||
| 135 | return; | ||
| 136 | }; | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub nonempty { | ||
| 141 | my ($self) = @_; | ||
| 142 | push @{$self->{__rules__}}, sub { | ||
| 143 | my ($self, $value) = @_; | ||
| 144 | return "Array is empty" if scalar(@$value) == 0; | ||
| 145 | return; | ||
| 146 | }; | ||
| 147 | return $self; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub element { | ||
| 151 | my ($self) = @_; | ||
| 152 | return $self->{__validator__}; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub optional { | ||
| 156 | my ($self) = @_; | ||
| 157 | $self->{__optional__} = 1; | ||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub default { | ||
| 162 | my ($self, $default) = @_; | ||
| 163 | $self->{__default__} = $default; | ||
| 164 | return $self; | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 | =head1 NAME | ||
| 170 | |||
| 171 | Poz::Types::array - Array type validation for Poz::Types | ||
| 172 | |||
| 173 | =head1 SYNOPSIS | ||
| 174 | |||
| 175 | use Poz qw/z/; | ||
| 176 | |||
| 177 | my $array_validator = z->array(z->number); | ||
| 178 | |||
| 179 | $array_validator->min(1)->max(5)->nonempty(); | ||
| 180 | |||
| 181 | my $data = [1, 2, 3]; | ||
| 182 | my $validated_data = $array_validator->parse($data); | ||
| 183 | |||
| 184 | =head1 DESCRIPTION | ||
| 185 | |||
| 186 | Poz::Types::array provides a way to validate arrays with various rules. It is designed to work with the Poz. | ||
| 187 | |||
| 188 | =head1 METHODS | ||
| 189 | |||
| 190 | =head2 as | ||
| 191 | |||
| 192 | $array_validator->as('ArrayClass'); | ||
| 193 | |||
| 194 | Sets the class name to bless the validated array into. | ||
| 195 | |||
| 196 | =head2 parse | ||
| 197 | |||
| 198 | my $validated_data = $array_validator->parse($data); | ||
| 199 | |||
| 200 | Parses and validates the data. Throws an exception if validation fails. | ||
| 201 | |||
| 202 | =head2 safe_parse | ||
| 203 | |||
| 204 | my ($validated_data, $errors) = $array_validator->safe_parse($data); | ||
| 205 | |||
| 206 | Parses and validates the data. Returns the validated data and any errors. | ||
| 207 | |||
| 208 | =head2 min | ||
| 209 | |||
| 210 | $array_validator->min($min_length); | ||
| 211 | |||
| 212 | Sets a minimum length for the array. | ||
| 213 | |||
| 214 | =head2 max | ||
| 215 | |||
| 216 | $array_validator->max($max_length); | ||
| 217 | |||
| 218 | Sets a maximum length for the array. | ||
| 219 | |||
| 220 | =head2 length | ||
| 221 | |||
| 222 | $array_validator->length($exact_length); | ||
| 223 | |||
| 224 | Sets an exact length for the array. | ||
| 225 | |||
| 226 | =head2 nonempty | ||
| 227 | |||
| 228 | $array_validator->nonempty(); | ||
| 229 | |||
| 230 | Ensures the array is not empty. | ||
| 231 | |||
| 232 | =head2 element | ||
| 233 | |||
| 234 | my $element_validator = $array_validator->element(); | ||
| 235 | |||
| 236 | Returns the element validator. | ||
| 237 | |||
| 238 | =head1 LICENSE | ||
| 239 | |||
| 240 | Copyright (C) ytnobody. | ||
| 241 | |||
| 242 | This library is free software; you can redistribute it and/or modify | ||
| 243 | it under the same terms as Perl itself. | ||
| 244 | |||
| 245 | =head1 AUTHOR | ||
| 246 | |||
| 247 | ytnobody E<lt>ytnobody@gmail.comE<gt> | ||
| 248 | |||
| 249 | =cut |