aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311/Endpoint/Schema/Comma.pm
blob: f6ac1bcc782bfe1ed2a9a0ee7970156fbed49b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use strict; use warnings;
package Open311::Endpoint::Schema::Comma;
use parent 'Data::Rx::CommonType::EasyNew';

use Carp ();

sub type_uri {
    'tag:wiki.open311.org,GeoReport_v2:rx/comma',
}

sub guts_from_arg {
    my ($class, $arg, $rx) = @_;
    $arg ||= {};

    my $contents = delete $arg->{contents}
        or Carp::croak "No contents for comma-separated list";
    my $trim = delete $arg->{trim};
    if (my @unexpected = keys %$arg) {
        Carp::croak sprintf "Unknown arguments %s in constructing %s",
            (join ',' => @unexpected), $class->type_uri;
    }

    return {
        trim => $trim,
        str_schema => $rx->make_schema('//str'),
        subschema => $rx->make_schema( $contents ),
    };
}

sub assert_valid {
    my ($self, $value) = @_;

    $self->{str_schema}->assert_valid( $value );

    my @values = split ',' => $value;

    my $subschema = $self->{subschema};
    my $trim = $self->{trim};

    for my $subvalue (@values) {

        if ($self->{trim}) {
            $subvalue =~s/^\s*//;
            $subvalue =~s/\s*$//;
        }

        $subschema->assert_valid( $subvalue );
    }

    return 1;
}

1;