1Type::Tiny::Manual::UsiUnsgeWritChoOntthreirb(u3t)ed PerTlypDeo:c:uTmiennyt:a:tMiaonnual::UsingWithOther(3)
2
3
4

NAME

6       Type::Tiny::Manual::UsingWithOther - how to use Type::Tiny and
7       Type::Library with other OO frameworks
8

DESCRIPTION

10   Class::InsideOut
11       You want Class::InsideOut 1.13 or above, which has support for blessed
12       and overloaded objects (including Type::Tiny type constraints) for the
13       "get_hook" and "set_hook" options.
14
15          {
16             package Person;
17
18             use Class::InsideOut qw( public );
19             use Types::Standard qw( Str Int );
20             use Type::Utils qw( declare as where inline_as coerce from );
21
22             public name => my %_name, {
23                set_hook => Str,
24             };
25
26             my $PositiveInt = declare
27                as        Int,
28                where     {  $_ > 0  },
29                inline_as { "$_ =~ /^[0-9]+\$/ and $_ > 0" };
30
31             coerce $PositiveInt, from Int, q{ abs $_ };
32
33             public age => my %_age, {
34                set_hook => sub { $_ = $PositiveInt->assert_coerce($_) },
35             };
36
37             sub get_older {
38                my $self = shift;
39                my ($years) = @_;
40                $PositiveInt->assert_valid($years);
41                $self->_set_age($self->age + $years);
42             }
43          }
44
45       I probably need to make coercions a little prettier.
46
47       See also: "t/25_accessor_hooks_typetiny.t" and "t/Object/HookedTT.pm"
48       in the Class::InsideOut test suite; and the Class-InsideOut integration
49       tests <https://github.com/tobyink/p5-type-
50       tiny/tree/master/t/30-integration/Class-InsideOut> in the Type::Tiny
51       test suite.
52
53   Params::Check and Object::Accessor
54       The Params::Check "allow()" function, the "allow" option for the
55       Params::Check "check()" function, and the input validation mechanism
56       for Object::Accessor all work in the same way, which is basically a
57       limited pure-Perl implementation of the smart match operator. While
58       this doesn't directly support Type::Tiny constraints, it does support
59       coderefs.  You can use Type::Tiny's "compiled_check" method to obtain a
60       suitable coderef.
61
62       Param::Check example:
63
64          my $tmpl = {
65             name => { allow => Str->compiled_check },
66             age  => { allow => Int->compiled_check },
67          };
68          check($tmpl, { name => "Bob", age => 32 })
69             or die Params::Check::last_error();
70
71       Object::Accessor example:
72
73          my $obj = Object::Accessor->new;
74          $obj->mk_accessors(
75             { name => Str->compiled_check },
76             { age  => Int->compiled_check },
77          );
78
79       Caveat: Object::Accessor doesn't die when a value fails to meet its
80       type constraint; instead it outputs a warning to STDERR. This behaviour
81       can be changed by setting "$Object::Accessor::FATAL = 1".
82
83       See also: The Object-Accessor integration tests
84       <https://github.com/tobyink/p5-type-
85       tiny/tree/master/t/30-integration/Object-Accessor> in the Type::Tiny
86       test suite.
87
88   Validation::Class::Simple
89       You want Validation::Class::Simple 7.900017 or above.
90
91       The "to_TypeTiny" function from Types::TypeTiny can be used to create a
92       Type::Tiny type constraint from a Validation::Class::Simple object (and
93       probably from Validation::Class, but this is untested).
94
95          use Types::TypeTiny qw( to_TypeTiny );
96          use Validation::Class::Simple;
97
98          my $type = to_TypeTiny Validation::Class::Simple->new(
99             fields => {
100                name => {
101                   required => 1,
102                   pattern  => qr{^\w+(\s\w+)*$},
103                   filters  => ["trim", "strip"],
104                },
105                email => { required => 1, email => 1 },
106                pass  => { required => 1, min_length => 6 },
107             },
108          );
109
110          # true
111          $type->check({
112             name   => "Toby Inkster",
113             email  => "tobyink@cpan.org",
114             pass   => "foobar",
115          });
116
117          # false
118          $type->check({
119             name   => "Toby Inkster ",    # trailing whitespace
120             email  => "tobyink@cpan.org",
121             pass   => "foobar",
122          });
123
124          # coercion from HashRef uses the filters defined above
125          my $fixed = $type->coerce({
126             name   => "Toby Inkster ",    # trailing whitespace
127             email  => "tobyink@cpan.org",
128             pass   => "foobar",
129          });
130
131          # true
132          $type->check($fixed);
133
134       Type constraints built with Validation::Class::Simple are not
135       inlinable, so won't be as fast as "Dict" from Types::Standard, but the
136       filters are a pretty useful feature. (Note that filters are explicitly
137       ignored for type constraint checking, and only come into play for
138       coercion.)
139
140       See also: The Validation-Class-Simple integration tests
141       <https://github.com/tobyink/p5-type-
142       tiny/tree/master/t/30-integration/Validation-Class-Simple> in the
143       Type::Tiny test suite.
144

AUTHOR

146       Toby Inkster <tobyink@cpan.org>.
147
149       This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
150
151       This is free software; you can redistribute it and/or modify it under
152       the same terms as the Perl 5 programming language system itself.
153

DISCLAIMER OF WARRANTIES

155       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
156       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
157       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
158
159
160
161perl v5.28.1                      2019-01-T0y8pe::Tiny::Manual::UsingWithOther(3)
Impressum