Sah/Schema/str_or_re.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 10 / 10 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« Sah/Schema/str_or_aos1/arrayified.pm ↑ Summary Sah/Schema/uppercase_str.pm »
1 package Sah::Schema::str_or_re;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 1 our $DATE = '2024-02-06'; # DATE
7 1 our $DIST = 'Sah-Schemas-Str'; # DIST
8 1 our $VERSION = '0.018'; # VERSION
9  
10 1 our $schema = [any => {
11 summary => 'String or regex (if string is of the form `/.../`)',
12 description => <<'MARKDOWN',
13  
14 Either string or Regexp object is accepted.
15  
16 If string is of the form of `/.../` or `qr(...)`, then it will be compiled into
17 a Regexp object. If the regex pattern inside `/.../` or `qr(...)` is invalid,
18 value will be rejected.
19  
20 Currently, unlike in normal Perl, for the `qr(...)` form, only parentheses `(`
21 and `)` are allowed as the delimiter.
22  
23 Currently modifiers `i`, `m`, and `s` after the second `/` are allowed.
24  
25 What's the difference between this schema and `re_from_str` (from
26 <pm:Sah::Schemas::Re>)? Both this schema and `re_from_str` accept string, but
27 this schema will leave strings not in the form of `/.../` or `qr(...)` as-is,
28 while `re_from_str` will still convert the string to Regexp object (after
29 escaping the special characters). In other words, this schema can produce
30 strings while `str_or_re` always produces Regexp object.
31  
32 MARKDOWN
33 of => [
34 ['str'],
35 ['re'],
36 ],
37  
38 prefilters => ['Str::maybe_convert_to_re'],
39  
40 examples => [
41 {value=>'', valid=>1},
42 {value=>'a', valid=>1},
43 {value=>{}, valid=>0, summary=>'Not a string'},
44  
45 {value=>'//', valid=>1, validated_value=>qr//},
46 {value=>'/foo', valid=>1, summary=>'Becomes a string'},
47 {value=>'qr(foo', valid=>1, summary=>'Becomes a string'},
48 {value=>'qr(foo(', valid=>1, summary=>'Becomes a string'},
49 {value=>'qr/foo/', valid=>1, summary=>'Becomes a string'},
50  
51 {value=>'/foo.*/', valid=>1, validated_value=>qr/foo.*/},
52 {value=>'qr(foo.*)', valid=>1, validated_value=>qr/foo.*/},
53 {value=>'/foo/is', valid=>1, validated_value=>qr/foo/is},
54 {value=>'qr(foo)is', valid=>1, validated_value=>qr/foo/is},
55  
56 {value=>'/foo[/', valid=>0, summary=>'Invalid regex'},
57 ],
58  
59 }];
60  
61 1 1;
62 # ABSTRACT:
63  
64 __END__
« Sah/Schema/str_or_aos1/arrayified.pm ↑ Summary Sah/Schema/uppercase_str.pm »