Poz/Types/string.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Poz/Builder.pm ↑ Summary
1 package Poz::Types::string;
2 11 ▶use 5.032;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use parent 'Poz::Types::scalar';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Email::Address ();
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use URI::URL ();
9 use Net::IPv6Addr ();
10 use Time::Piece ();
11 use DateTime::Format::Strptime ();
12 use DateTime::Format::ISO8601 ();
13 use DateTime::Format::Duration::ISO8601 ();
14  
15 sub new {
16 my ($class, $opts) = @_;
17 $opts = $opts || {};
18 $opts->{required_error} //= "required";
19 $opts->{invalid_type_error} //= "Not a string";
20 my $self = $class->SUPER::new($opts);
21 return $self;
22 }
23  
24 sub rule {
25 my ($self, $value) = @_;
26 return $self->{required_error} unless defined $value;
27 return $self->{invalid_type_error} unless !ref $value;
28 return;
29 }
30  
31 sub coerce {
32 my ($self, $value) = @_;
33 return "$value";
34 }
35  
36 sub max {
37 my ($self, $max, $opts) = @_;
38 $opts = $opts || {};
39 $opts->{message} //= "Too long";
40 push @{$self->{rules}}, sub {
41 my ($self, $value) = @_;
42 return $opts->{message} if CORE::length($value) > $max;
43 return;
44 };
45 return $self;
46 }
47  
48 sub min {
49 my ($self, $min, $opts) = @_;
50 $opts = $opts || {};
51 $opts->{message} //= "Too short";
52 push @{$self->{rules}}, sub {
53 my ($self, $value) = @_;
54 return $opts->{message} if CORE::length($value) < $min;
55 return;
56 };
57 return $self;
58 }
59  
60 sub length {
61 my ($self, $length, $opts) = @_;
62 $opts = $opts || {};
63 $opts->{message} //= "Not the right length";
64 push @{$self->{rules}}, sub {
65 my ($self, $value) = @_;
66 return $opts->{message} if CORE::length($value) != $length;
67 return;
68 };
69 return $self;
70 }
71  
72 sub email {
73 my ($self, $opts) = @_;
74 $opts = $opts || {};
75 $opts->{message} //= "Not an email";
76 push @{$self->{rules}}, sub {
77 my ($self, $value) = @_;
78 my ($addr) = Email::Address->parse($value);
79 return $opts->{message} unless defined $addr;
80 return;
81 };
82 return $self;
83 }
84  
85 sub url {
86 my ($self, $opts) = @_;
87 $opts = $opts || {};
88 $opts->{message} //= "Not an URL";
89 push @{$self->{rules}}, sub {
90 my ($self, $value) = @_;
91 my $url = URI::URL->new($value);
92 return $opts->{message} if !defined $url || !defined $url->scheme;
93 return;
94 };
95 return $self;
96 }
97  
98 sub emoji {
99 my ($self, $opts) = @_;
100 $opts = $opts || {};
101 $opts->{message} //= "Not an emoji";
102 push @{$self->{rules}}, sub {
103 my ($self, $value) = @_;
104 return $opts->{message} unless $value =~ /\p{Emoji}/;
105 return;
106 };
107 return $self;
108 }
109  
110 sub uuid {
111 my ($self, $opts) = @_;
112 $opts = $opts || {};
113 $opts->{message} //= "Not an UUID";
114 push @{$self->{rules}}, sub {
115 my ($self, $value) = @_;
116 return $opts->{message} unless lc($value) =~ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
117 return;
118 };
119 return $self;
120 }
121  
122 sub nanoid {
123 my ($self, $opts) = @_;
124 $opts = $opts || {};
125 $opts->{message} //= "Not a nanoid";
126 push @{$self->{rules}}, sub {
127 my ($self, $value) = @_;
128 return $opts->{message} unless $value =~ /^[0-9a-zA-Z_-]{21}$/;
129 return;
130 };
131 return $self;
132 }
133  
134 sub cuid {
135 my ($self, $opts) = @_;
136 $opts = $opts || {};
137 $opts->{message} //= "Not a cuid";
138 push @{$self->{rules}}, sub {
139 my ($self, $value) = @_;
140 return $opts->{message} unless $value =~ /^c[a-z0-9]{24}$/;
141 return;
142 };
143 return $self;
144 }
145  
146 sub cuid2 {
147 my ($self, $opts) = @_;
148 $opts = $opts || {};
149 $opts->{message} //= "Not a cuid2";
150 push @{$self->{rules}}, sub {
151 my ($self, $value) = @_;
152 return $opts->{message} unless $value =~ /^[a-z0-9]{24,32}$/;
153 return;
154 };
155 return $self;
156 }
157  
158 sub ulid {
159 my ($self, $opts) = @_;
160 $opts = $opts || {};
161 $opts->{message} //= "Not an ulid";
162 push @{$self->{rules}}, sub {
163 my ($self, $value) = @_;
164 return $opts->{message} unless $value =~ /^[0-9A-HJKMNP-TV-Z]{26}$/;
165 return;
166 };
167 return $self;
168 }
169  
170 sub regex {
171 my ($self, $regex, $opts) = @_;
172 $opts = $opts || {};
173 $opts->{message} //= "Not match regex";
174 push @{$self->{rules}}, sub {
175 my ($self, $value) = @_;
176 return $opts->{message} unless $value =~ $regex;
177 return;
178 };
179 return $self;
180 }
181  
182 sub includes {
183 my ($self, $includes, $opts) = @_;
184 $opts = $opts || {};
185 $opts->{message} //= "Not includes $includes";
186 push @{$self->{rules}}, sub {
187 my ($self, $value) = @_;
188 return $opts->{message} unless index($value, $includes) != -1;
189 return;
190 };
191 return $self;
192 }
193  
194 sub startsWith {
195 my ($self, $startWith, $opts) = @_;
196 $opts = $opts || {};
197 $opts->{message} //= "Not starts with $startWith";
198 push @{$self->{rules}}, sub {
199 my ($self, $value) = @_;
200 return $opts->{message} unless index($value, $startWith) == 0;
201 return;
202 };
203 return $self;
204 }
205  
206 sub endsWith {
207 my ($self, $endsWith, $opts) = @_;
208 $opts = $opts || {};
209 $opts->{message} //= "Not ends with $endsWith";
210 push @{$self->{rules}}, sub {
211 my ($self, $value) = @_;
212 return $opts->{message} unless substr($value, -1 * CORE::length($endsWith)) eq $endsWith;
213 return;
214 };
215 return $self;
216 }
217  
218 # supports ipv4 / ipv6
219 sub ip {
220 my ($self, $opts) = @_;
221 $opts = $opts || {};
222 $opts->{message} //= "Not an IP address";
223 my $version = $opts->{version} || "any";
224 push @{$self->{rules}}, sub {
225 my ($self, $value) = @_;
226 my $pass = 0;
227 if ($version eq "v4" || $version eq "any") {
228 my @octets = split(/\./, $value);
229 if (scalar(@octets) == 4) {
230 foreach my $octet (@octets) {
231 return $opts->{message} unless $octet =~ /^\d+$/ && $octet >= 0 && $octet <= 255;
232 }
233 $pass = 1;
234 }
235 if ($version eq "v4" && !$pass) {
236 return $opts->{message};
237 }
238 }
239 if (!$pass && ($version eq "v6" || $version eq "any")) {
240 return $opts->{message} unless Net::IPv6Addr::is_ipv6($value);
241 }
242 return;
243 };
244 return $self;
245 }
246  
247 sub trim {
248 my ($self) = @_;
249 push @{$self->{transform}}, sub {
250 my ($self, $value) = @_;
251 $value =~ s/^\s+|\s+$//g;
252 return $value;
253 };
254 return $self;
255 }
256  
257 sub toLowerCase {
258 my ($self) = @_;
259 push @{$self->{transform}}, sub {
260 my ($self, $value) = @_;
261 return lc($value);
262 };
263 return $self;
264 }
265  
266 sub toUpperCase {
267 my ($self) = @_;
268 push @{$self->{transform}}, sub {
269 my ($self, $value) = @_;
270 return uc($value);
271 };
272 return $self;
273 }
274  
275 sub date {
276 my ($self, $opts) = @_;
277 $opts = $opts || {};
278 $opts->{message} //= "Not a date";
279 push @{$self->{rules}}, sub {
280 my ($self, $value) = @_;
281 return $opts->{message} unless $value =~ /^\d{4}-\d{2}-\d{2}$/;
282 return $opts->{message} unless eval { Time::Piece->strptime($value, "%Y-%m-%d") };
283 return;
284 };
285 return $self;
286 }
287  
288 sub time {
289 my ($self, $opts) = @_;
290 $opts = $opts || {};
291 $opts->{message} //= "Not a time";
292 my $precision = $opts->{precision} || 6;
293 my $precision_regex = _build_precision_regex($precision);
294 my $format_check = qr/^\d{2}:\d{2}:\d{2}$precision_regex$/;
295 push @{$self->{rules}}, sub {
296 my ($self, $value) = @_;
297 my $format = "%H:%M:%S";
298 return $opts->{message} unless $value =~ $format_check;
299 if ($value =~ /\.[0-9]+/) {
300 $format = "%H:%M:%S.%N";
301 }
302 my $formatter = DateTime::Format::Strptime->new(pattern => $format);
303 return $opts->{message} unless eval { $formatter->parse_datetime($value) };
304 return;
305 };
306 return $self;
307 }
308  
309 # iso8601 format
310 sub datetime {
311 my ($self, $opts) = @_;
312 $opts = $opts || {};
313 $opts->{message} //= "Not a datetime";
314 my $precision = $opts->{precision} || 6;
315 my $precision_regex = _build_precision_regex($precision);
316 my $offset = $opts->{offset} || 0;
317 my $offset_regex = "(Z)?";
318 if ($offset) {
319 $offset_regex = "(Z|[+-][0-9]{4}|[+-][0-9]{2}(:[0-9]{2})?)?";
320 }
321  
322 my $format_check = qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$precision_regex$offset_regex$/;
323 push @{$self->{rules}}, sub {
324 my ($self, $value) = @_;
325 return $opts->{message} unless $value =~ $format_check;
326 return $opts->{message} unless eval { DateTime::Format::ISO8601->parse_datetime($value) };
327 return;
328 };
329 return $self;
330 }
331  
332 sub _build_precision_regex {
333 my ($precision) = @_;
334 my $min_precision = 1;
335 if ($precision < $min_precision) {
336 $min_precision = $precision;
337 }
338 return "(\\.[0-9]{$min_precision,$precision})?";
339 }
340  
341 # iso8601 duration
342 sub duration {
343 my ($self, $opts) = @_;
344 $opts = $opts || {};
345 $opts->{message} //= "Not a duration";
346 push @{$self->{rules}}, sub {
347 my ($self, $value) = @_;
348 my $format = DateTime::Format::Duration::ISO8601->new;
349 return $opts->{message} unless eval { $format->parse_duration($value) };
350 return;
351 };
352 return $self;
353 }
354  
355 sub base64 {
356 my ($self, $opts) = @_;
357 $opts = $opts || {};
358 $opts->{message} //= "Not a base64";
359 push @{$self->{rules}}, sub {
360 my ($self, $value) = @_;
361 return $opts->{message} unless $value =~ /^[A-Za-z0-9+\/]+={0,2}$/;
362 return;
363 };
364 return $self;
365 }
366 1;
367  
368 =head1 NAME
369  
370 Poz::Types::string - A module for validating and transforming strings.
371  
372 =head1 SYNOPSIS
373  
374 use Poz qw/z/;
375  
376 my $string = z->string;
377  
378 # Validate a string
379 $string->rule($value);
380  
381 # Coerce a value to a string
382 my $coerced_value = $string->coerce($value);
383  
384 # Add validation rules
385 $string->max($max_length, \%opts);
386 $string->min($min_length, \%opts);
387 $string->email(\%opts);
388 $string->url(\%opts);
389 $string->emoji(\%opts);
390 $string->uuid(\%opts);
391 $string->nanoid(\%opts);
392 $string->cuid(\%opts);
393 $string->cuid2(\%opts);
394 $string->ulid(\%opts);
395 $string->regex($regex, \%opts);
396 $string->includes($substring, \%opts);
397 $string->startsWith($prefix, \%opts);
398 $string->endsWith($suffix, \%opts);
399 $string->ip(\%opts);
400 $string->date(\%opts);
401 $string->time(\%opts);
402 $string->datetime(\%opts);
403 $string->duration(\%opts);
404 $string->base64(\%opts);
405  
406 # Add transformations
407 $string->trim;
408 $string->toLowerCase;
409 $string->toUpperCase;
410  
411 =head1 DESCRIPTION
412  
413 This module provides a set of methods for validating and transforming strings. It includes rules for checking string length, format, and content, as well as transformations for modifying the string.
414  
415 =head2 METHODS
416  
417 =over 4
418  
419 =item rule
420  
421 $string->rule($value);
422 Validates that the value is a defined, non-reference string.
423  
424 =item coerce
425  
426 my $coerced_value = $string->coerce($value);
427 Coerces the value to a string.
428  
429 =item max
430  
431 $string->max($max_length, \%opts);
432 Adds a rule to ensure the string does not exceed the specified maximum length.
433  
434 =item min
435  
436 $string->min($min_length, \%opts);
437 Adds a rule to ensure the string is at least the specified minimum length.
438  
439 =item email
440  
441 $string->email(\%opts);
442 Adds a rule to validate that the string is a valid email address.
443  
444 =item url
445  
446 $string->url(\%opts);
447 Adds a rule to validate that the string is a valid URL.
448  
449 =item emoji
450  
451 $string->emoji(\%opts);
452 Adds a rule to validate that the string contains an emoji.
453  
454 =item uuid
455  
456 $string->uuid(\%opts);
457 Adds a rule to validate that the string is a valid UUID.
458  
459 =item nanoid
460  
461 $string->nanoid(\%opts);
462 Adds a rule to validate that the string is a valid NanoID.
463  
464 =item cuid
465  
466 $string->cuid(\%opts);
467 Adds a rule to validate that the string is a valid CUID.
468  
469 =item cuid2
470  
471 $string->cuid2(\%opts);
472 Adds a rule to validate that the string is a valid CUID2.
473  
474 =item ulid
475  
476 $string->ulid(\%opts);
477 Adds a rule to validate that the string is a valid ULID.
478  
479 =item regex
480  
481 $string->regex($regex, \%opts);
482 Adds a rule to validate that the string matches the specified regular expression.
483  
484 =item includes
485  
486 $string->includes($substring, \%opts);
487 Adds a rule to validate that the string includes the specified substring.
488  
489 =item startsWith
490  
491 $string->startsWith($prefix, \%opts);
492 Adds a rule to validate that the string starts with the specified prefix.
493  
494 =item endsWith
495  
496 $string->endsWith($suffix, \%opts);
497 Adds a rule to validate that the string ends with the specified suffix.
498  
499 =item ip
500  
501 $string->ip(\%opts);
502 Adds a rule to validate that the string is a valid IP address (IPv4 or IPv6).
503  
504 =item trim
505  
506 $string->trim;
507 Adds a transformation to trim whitespace from the string.
508  
509 =item toLowerCase
510  
511 $string->toLowerCase;
512 Adds a transformation to convert the string to lowercase.
513  
514 =item toUpperCase
515  
516 $string->toUpperCase;
517 Adds a transformation to convert the string to uppercase.
518  
519 =item date
520  
521 $string->date(\%opts);
522 Adds a rule to validate that the string is a valid date (YYYY-MM-DD).
523  
524 =item time
525  
526 $string->time(\%opts);
527 Adds a rule to validate that the string is a valid time (HH:MM:SS).
528  
529 =item datetime
530  
531 $string->datetime(\%opts);
532 Adds a rule to validate that the string is a valid ISO8601 datetime.
533  
534 =item duration
535  
536 $string->duration(\%opts);
537 Adds a rule to validate that the string is a valid ISO8601 duration.
538  
539 =item base64
540  
541 $string->base64(\%opts);
542 Adds a rule to validate that the string is a valid base64 encoded string.
543  
544 =back
545  
546 =head1 LICENSE
547  
548 Copyright (C) ytnobody.
549  
550 This library is free software; you can redistribute it and/or modify
551 it under the same terms as Perl itself.
552  
553 =head1 AUTHOR
554  
555 ytnobody E<lt>ytnobody@gmail.comE<gt>
556  
557 =cut
« Poz/Builder.pm ↑ Summary