| 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__ | ||