NAME
MooX::ValidateSubs - Validating sub routines via Type::Tiny.
VERSION
Version 1.012007
SYNOPSIS
package Welcome::To::A::World::Of::Types;
use Moo;
use MooX::ValidateSubs;
use Types::Standard qw/Str ArrayRef HashRef/;
validate_subs (
hello_world => {
params => {
one => [ Str, 1 ], # 1 means I'm optional
two => [ ArrayRef ],
three => [ HashRef, 'before_add_me' ],
},
returns => {
one => [ Str, 1 ], # 1 means I'm optional
two => [ ArrayRef ],
three => [ HashRef ],
four => [ Str, 'add_on' ],
},
},
goodbye_world => { params => [ [Str], [ArrayRef], [HashRef] ] },
spec => {
params => {
one => [ Str, sub { 'one' } ],
two => [ Str, sub { 'two' } ],
three => [ Str, sub { 'three' } ],
},
keys => [qw/three two one/]
},
indexed => {
params => [ [Str, sub { 'one' }], [Str, sub { 'two' }], [Str, sub { 'three' }] ],
},
);
sub before_add_me {
return {
okay => 'fine',
};
}
sub add_on {
return 'sad face';
}
sub hello_world {
my ($self, %args) = @_;
# $args{one} # optional string
# $args{two} # valid arrayref
# $args{three} # valid hashref
if ( ... # some condition ... ) {
$args{four} = 'may or may not get set here';
}
return %args;
}
$self->indexed(2 => 'changed'); # one, two, changed
Exports
validate_subs
I'm a key/value list, my keys should reference a sub routine, My value must now be a hash reference that can contain two *optional* keys (params and returns).
validate_subs (
hash_example => {
params => { ... },
returns => { ... },
},
array_example => {
params => [ ... ],
returns => [ ... ],
}
);
Both params and returns value can either be an array reference of array references, that indicates we are going to be validating either an Array or an Array Reference,
array_example => {
params => [ [Str], [HashRef], [Str] ],
returns => [ [Str], [HashRef] ],
},
...
sub array_example {
my ($self) = shift;
return (shift, shift);
}
Or a hash reference with array reference values, when validating either a Hash or Hash reference.
hash_example => {
params => {
one => [ Str ],
two => [ HashRef ],
},
returns => {
one => [ Str ],
two => [ HashRef ],
three => [ Str ],
},
},
...
sub hash_example {
my ($self, %hash) = @_;
$hash{three} = 'add a key';
return %hash;
}
The array references must always have a first index that is a code reference, you can optionally pass a second index that can either be 1, which indicates *optional*, a scalar that reference a subroutine/attribute available to *self* or a code reference that gets used to fill a default value if one was not passed.
params => {
one => [ Str, sub { 'Hello World' } ],
two => [ Str, 'basics' ],
},
returns => [ [Str], [Str], [Str, 'say_goodbye'],
....
has basics => (
is => 'ro',
default => sub { "How are you" },
);
sub say_goodbye {
my ($self) = shift;
return 'In a rush, goodbye.';
}
sub example {
my ($self, %hash) = @_;
return values %hash;
}
...
Breaking things
I decided to make some breaking changes so 0.07++ syntax is not compatible with 0.06--. I also changed from *before* to *around* as I want to modify $args. If you prefer the *before* approach it can currently be found here - https://github.com/ThisUsedToBeAnEmail/MooX-TypeParams.
AUTHOR
Robert Acock, <thisusedtobeanemail at gmail.com>
BUGS
Please report any bugs or feature requests to bug-moox-validatesubs at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooX-ValidateSubs. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc MooX::ValidateSubs
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2017 Robert Acock.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.