| 1 | package Email::MIME::Kit::Validator::Rx; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 2 | $Email::MIME::Kit::Validator::Rx::VERSION = '0.200001'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | with 'Email::MIME::Kit::Role::Validator'; | |||||||||||||||||||||||||||
| 7 | # ABSTRACT: validate assembly stash with Rx (from JSON in kit) | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Data::Rx 0.007; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Data::Rx::TypeBundle::Perl 0.005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has prefix => ( | ||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has type_plugins => ( | ||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 25 | 3 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | has rx => ( | |||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | isa => class_type('Data::Rx'), | ||||||||||||||||||||||||||||
| 31 | lazy => 1, | ||||||||||||||||||||||||||||
| 32 | init_arg => undef, | ||||||||||||||||||||||||||||
| 33 | builder => 'build_default_rx_object', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub build_default_rx_object { | ||||||||||||||||||||||||||||
| 37 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_default_rx_object: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 3 | my $rx = Data::Rx->new({ | |||||||||||||||||||||||||||
| 39 | prefix => $self->prefix, | ||||||||||||||||||||||||||||
| 40 | }); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | for my $plugin ($self->all_default_type_plugins, @{ $self->type_plugins }) { | |||||||||||||||||||||||||||
| 43 | 3 | ▶ | eval "require $plugin; 1" or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 3 | $rx->register_type_plugin($plugin); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | my $prefix = $self->prefix; | |||||||||||||||||||||||||||
| 48 | 3 | for my $key (keys %$prefix) { | |||||||||||||||||||||||||||
| 49 | 0 | $rx->add_prefix($key, $prefix->{ $key }); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | return $rx; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub all_default_type_plugins { | ||||||||||||||||||||||||||||
| 56 | # shamlessly stolen from Moose::Object::BUILDALL -- rjbs, 2009-03-06 | ||||||||||||||||||||||||||||
| 57 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
all_default_type_plugins: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 3 | my @plugins; | |||||||||||||||||||||||||||
| 59 | 3 | for my $method ( | |||||||||||||||||||||||||||
| 60 | reverse | ||||||||||||||||||||||||||||
| 61 | $self->meta->find_all_methods_by_name('accumulate_default_type_plugins') | ||||||||||||||||||||||||||||
| 62 | ) { | ||||||||||||||||||||||||||||
| 63 | 3 | push @plugins, $method->{code}->execute($self); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | return @plugins; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub accumulate_default_type_plugins { | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | return ('Data::Rx::TypeBundle::Perl'); | ||||||||||||||||||||||||||
|
Subroutine
accumulate_default_type_plugins: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | has schema => ( | |||||||||||||||||||||||||||
| 74 | reader => 'schema', | ||||||||||||||||||||||||||||
| 75 | writer => '_set_schema', | ||||||||||||||||||||||||||||
| 76 | isa => 'Object', # It'd be nice to have a better TC -- rjbs, 2009-03-06 | ||||||||||||||||||||||||||||
| 77 | init_arg => undef, | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | has schema_struct => ( | |||||||||||||||||||||||||||
| 81 | reader => '_schema_struct', | ||||||||||||||||||||||||||||
| 82 | init_arg => 'schema', | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | has schema_path => ( | |||||||||||||||||||||||||||
| 86 | reader => '_schema_path', | ||||||||||||||||||||||||||||
| 87 | writer => '_set_schema_path', | ||||||||||||||||||||||||||||
| 88 | isa => 'Str', | ||||||||||||||||||||||||||||
| 89 | init_arg => 'path', | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | has combine => ( | ||||||||||||||||||||||||||||
| 93 | is => 'ro', | ||||||||||||||||||||||||||||
| 94 | initializer => sub { | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | my ($self, $value, $set) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | confess "invalid combine logic: $value" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value eq "all"
MC/DC0%0/2
defined $value and $value eq "all"
defined $value
$value eq "all"
Truth table33%1/3
A and B
A= defined $valueB= $value eq "all"
| |||||||||||||||||||||||||||||
| 97 | unless defined $value and $value eq 'all'; | ||||||||||||||||||||||||||||
| 98 | 1 | $set->($value); | |||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | 2 | ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub BUILD { | ||||||||||||||||||||||||||||
| 103 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | $self->_do_goofy_schema_initialization; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub _do_goofy_schema_initialization { | ||||||||||||||||||||||||||||
| 109 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_do_goofy_schema_initialization: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | my @paths = grep { defined } ref $self->_schema_path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3 | ? @{ $self->_schema_path } | |||||||||||||||||||||||||||
| 113 | : $self->_schema_path; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | my @structs = grep { defined } (ref $self->_schema_struct eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | ? @{ $self->_schema_struct } | |||||||||||||||||||||||||||
| 117 | : $self->_schema_struct; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | confess("multiple schemata provided but no combine logic given") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@paths + @structs > 1 and not $self->combine
MC/DC0%0/2
@paths + @structs > 1 and not $self->combine
@paths + @structs > 1
not $self->combine
Truth table66%2/3
A and not(B)
A= @paths + @structs > 1B= not $self->combine
| |||||||||||||||||||||||||||||
| 120 | if @paths + @structs > 1 and ! $self->combine; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | ▶ | @paths = ('rx.json') unless @paths or @structs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@paths or @structs
MC/DC100%2/2
@paths or @structs
@paths
@structs
Truth table100%3/3
A or B
A= @pathsB= @structs
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | for my $path (@paths) { | |||||||||||||||||||||||||||
| 125 | # Sure, someday we can add another decoder layer here to allow schemata in | ||||||||||||||||||||||||||||
| 126 | # YAML. Whatever. -- rjbs, 2009-03-06 | ||||||||||||||||||||||||||||
| 127 | 2 | my $rx_json_ref = $self->kit->get_kit_entry($path); | |||||||||||||||||||||||||||
| 128 | 2 | my $rx_data = JSON->new->decode($$rx_json_ref); | |||||||||||||||||||||||||||
| 129 | 2 | push @structs, $rx_data; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 3 | ▶ | my $schema = @structs > 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | ? $self->rx->make_schema({ type => '//all', of => \@structs }) | ||||||||||||||||||||||||||||
| 134 | : $self->rx->make_schema($structs[0]); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 3 | $self->_set_schema($schema); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub validate { | ||||||||||||||||||||||||||||
| 140 | 9 | ▶ | my ($self, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | try { | ||||||||||||||||||||||||||||
| 143 | 9 | ▶ | $self->schema->assert_valid($stash); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | } catch { | ||||||||||||||||||||||||||||
| 145 | 3 | ▶ | Carp::confess("assembly parameters don't pass validation: $_"); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | 9 | }; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 6 | return 1; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 2 | ▶ | no Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 154 | 2 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =pod | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 NAME | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Email::MIME::Kit::Validator::Rx - validate assembly stash with Rx (from JSON in kit) | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 VERSION | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | version 0.200001 | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Email::MIME::Kit::Validator::Rx is a Validator plugin for Email::MIME::Kit that | ||||||||||||||||||||||||||||
| 171 | allows an Rx schema to be used to validate kit assembly data. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | A simple mkit's manifest might include the following: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | "renderer" : "TT", | ||||||||||||||||||||||||||||
| 177 | "validator": "Rx", | ||||||||||||||||||||||||||||
| 178 | "header" : [ ... mail headers ... ], | ||||||||||||||||||||||||||||
| 179 | "type" : "text/plain", | ||||||||||||||||||||||||||||
| 180 | "path" : "path/to/template.txt" | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | In this simple configuration, the use of "Rx" as the validator will load the | ||||||||||||||||||||||||||||
| 184 | plugin in its simplest configuration. It will look for a file called | ||||||||||||||||||||||||||||
| 185 | F<rx.json> in the kit and will load its contents (as JSON) and use them as a | ||||||||||||||||||||||||||||
| 186 | schema to validate the data passed to the it's C<assemble> method. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | More complex configurations are simple. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | This configuration supplies an alternate filename for the JSON file: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | "validator": [ "Rx", { "path": "rx-schema.json" } ], | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | This configuration supplies the schema definition inline: | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | "validator": [ | ||||||||||||||||||||||||||||
| 197 | "Rx", | ||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||
| 199 | "schema": { | ||||||||||||||||||||||||||||
| 200 | "type" : "//rec", | ||||||||||||||||||||||||||||
| 201 | "required": { | ||||||||||||||||||||||||||||
| 202 | "subject": "//str", | ||||||||||||||||||||||||||||
| 203 | "rcpt" : { "type": "/perl/obj", "isa": "Email::Address" } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | ] | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Notice, above, the C</perl/> prefix. By default, | ||||||||||||||||||||||||||||
| 210 | L<Data::Rx::TypeBundle::Perl|Data::Rx::TypeBundle::Perl> is loaded along with | ||||||||||||||||||||||||||||
| 211 | the core types. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | If a C<combine> argument is given, multiple schema definitions may be provided. | ||||||||||||||||||||||||||||
| 214 | They will be combined with the logic named by the combine argument. In this | ||||||||||||||||||||||||||||
| 215 | release, only "all" is valid, and will require all schemata to match. Here is | ||||||||||||||||||||||||||||
| 216 | an example: | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | "validator": [ | ||||||||||||||||||||||||||||
| 219 | "Rx", | ||||||||||||||||||||||||||||
| 220 | { | ||||||||||||||||||||||||||||
| 221 | "combine": "all", | ||||||||||||||||||||||||||||
| 222 | "path" : "rx.json", | ||||||||||||||||||||||||||||
| 223 | "schema" : [ | ||||||||||||||||||||||||||||
| 224 | { "type": "//rec", "rest": "//any", "required": { "foo": "//int" } }, | ||||||||||||||||||||||||||||
| 225 | { "type": "//rec", "rest": "//any", "required": { "bar": "//int" } }, | ||||||||||||||||||||||||||||
| 226 | ] | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | ] | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | This definition will create an C<//all> schema with three entries: the schema | ||||||||||||||||||||||||||||
| 231 | found in F<rx.json> and the two schemata given in the array value of C<schema>. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Ricardo SIGNES <rjbs@cpan.org> | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | This software is copyright (c) 2013 by Ricardo SIGNES. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 242 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||