File Coverage

blib/lib/Poz.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Poz;
2 11     11   1332525 use 5.032;
  11         35  
3 11     11   50 use strict;
  11         18  
  11         320  
4 11     11   47 use warnings;
  11         15  
  11         547  
5 11     11   4730 use Poz::Builder;
  11         40  
  11         443  
6 11     11   71 use Exporter 'import';
  11         19  
  11         416  
7 11     11   54 use Carp;
  11         21  
  11         2915  
8              
9             our $VERSION = "0.16";
10              
11             our @EXPORT_OK = qw/z/;
12             our %EXPORT_TAGS = (all => \@EXPORT_OK);
13              
14             $Carp::Internal{'Poz'}++;
15             $Carp::Internal{'Poz::Builder'}++;
16             $Carp::Internal{'Poz::Types'}++;
17             $Carp::Internal{'Poz::Types::scalar'}++;
18             $Carp::Internal{'Poz::Types::null'}++;
19             $Carp::Internal{'Poz::Types::string'}++;
20             $Carp::Internal{'Poz::Types::number'}++;
21             $Carp::Internal{'Poz::Types::object'}++;
22             $Carp::Internal{'Poz::Types::array'}++;
23             $Carp::Internal{'Poz::Types::enum'}++;
24             $Carp::Internal{'Poz::Types::union'}++;
25             $Carp::Internal{'Poz::Types::is'}++;
26              
27             sub z {
28 169     169 1 2724498 return Poz::Builder->new;
29             }
30              
31             1;
32             __END__
33              
34             =encoding utf-8
35              
36             =head1 NAME
37              
38             Poz - A simple, composable, and extensible data validation library for Perl.
39              
40             =head1 SYNOPSIS
41              
42             use Poz qw/z/;
43             use Data::UUID;
44             use Time::Piece;
45             my $bookSchema = z->object({
46             id => z->string->uuid->default(sub { Data::UUID->new->create_str }),
47             title => z->string,
48             author => z->string->default("Anonymous"),
49             published => z->date,
50             created_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
51             updated_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
52             })->as("My::Book");
53              
54             my $book = $bookSchema->parse({
55             title => "Spidering Hacks",
56             author => "Kevin Hemenway",
57             published => "2003-10-01",
58             }) or die "Invalid book data";
59             $book->isa("My::Book"); # true
60              
61             my ($otherBook, $err) = $bookSchema->safe_parse({
62             title => "Eric Sink on the Business of Software",
63             author => "Eric Sink",
64             published => "2006-0i-01",
65             });
66             $otherBook; # undef
67             $err; # [{key => "", error => "Not a date"}]
68              
69             my $bookOrNumberSchema = z->union($bookSchema, z->number);
70             my $bookOrNumber = $bookOrNumberSchema->parse(123);
71             $bookOrNumber = $bookOrNumberSchema->parse({
72             title => "Perl Best Practices",
73             date => "2005-07-01",
74             author => "Damian Conway",
75             });
76              
77             my $bookArraySchema = z->array($z->is("My::Book"));
78             my $book1 = $bookSchema->parse({title => "Perl Best Practices", author => "Damian Conway", published => "2005-07-01"});
79             my $book2 = $bookSchema->parse({title => "Spidering Hacks", author => "Kevin Hemenway", published => "2003-10-01"});
80             my $bookArray = $bookArraySchema->parse([$book1, $book2]);
81              
82             # or use Poz as class builder
83             {
84             package My::Class;
85             use Poz qw/z/;
86             z->object({
87             name => z->string,
88             age => z->number,
89             })->constructor;
90             }
91             my $instance = My::Class->new(
92             name => 'Alice',
93             age => 20,
94             ); # bless({name => 'Alice', age => 20}, 'My::Class');
95            
96             =head1 DESCRIPTION
97              
98             Poz is a simple, composable, and extensible data validation library for Perl. It is inspired heavily from Zod L<https://zod.dev/> in TypeScript.
99              
100             =head1 EXPORTS
101              
102             =head2 z
103              
104             use Poz qw/z/;
105             my $builder = z;
106              
107             Returns a new instance of Poz::Builder.
108              
109             =head1 METHODS
110              
111             =head2 z->object($schema)
112              
113             my $schema = z->object({
114             id => z->string->uuid->default(sub { Data::UUID->new->create_str }),
115             title => z->string,
116             author => z->string->default("Anonymous"),
117             published => z->date,
118             created_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
119             updated_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
120             })->as("My::Book");
121              
122             Creates a new schema object.
123              
124             =head2 z->string
125              
126             my $schema = z->string;
127              
128             Creates a new string schema object.
129              
130             =head2 z->number
131              
132             my $schema = z->number;
133              
134             Creates a new number schema object.
135              
136             =head2 z->date
137              
138             my $schema = z->date;
139              
140             Creates a new date schema object.
141              
142             =head2 z->object
143              
144             my $schema = z->object($schema);
145              
146             Creates a new object schema object.
147              
148             =head2 z->object(...)->constructor
149              
150             package My::Class;
151             use Poz qw/z/;
152             z->object({
153             name => z->string,
154             age => z->number,
155             })->constructor;
156              
157             Creates a constructor method with Poz validation in your class.
158              
159             =head2 z->array
160              
161             my $schema = z->array($schema);
162              
163             Creates a new array schema object.
164              
165             =head2 z->enum
166              
167             my $schema = z->enum(@values);
168              
169             Creates a new enum schema object.
170              
171             =head2 z->union
172              
173             my $schema = z->union(@schemas);
174              
175             Creates a new union schema object.
176              
177             =head1 SEE ALSO
178              
179             =over 4
180              
181             =item L<Zod|https://zod.dev/>
182              
183             =item L<Poz::Builder>
184              
185             =item L<Poz::Types>
186              
187             =item L<Poz::Types::null>
188              
189             =item L<Poz::Types::string>
190              
191             =item L<Poz::Types::number>
192              
193             =item L<Poz::Types::object>
194              
195             =item L<Poz::Types::array>
196              
197             =item L<Poz::Types::enum>
198              
199             =item L<Poz::Types::union>
200              
201             =item L<Poz::Types::is>
202              
203             =back
204              
205             =head1 HOW TO CONTRIBUTE
206              
207             If you want to contribute to Poz, you can follow the steps below:
208              
209             =over 4
210              
211             =item 1. Prepare: Install cpanm and Minilla
212              
213             $ curl -L https://cpanmin.us | perl - --sudo App::cpanminus
214             $ cpanm Minilla
215              
216             =item 2. Fork: Please fork the repository on GitHub.
217              
218             The Repository on GitHub: L<https://github.com/ytnobody/p5-Poz>
219              
220             =item 3. Clone: Clone the repository.
221              
222             $ git clone
223              
224             =item 4. Branch: Create a feature branch from the main branch.
225              
226             $ git checkout -b feature-branch main
227              
228             =item 5. Code: Write your code and tests, then build.
229              
230             $ minil build
231              
232             =item 6. Test: Run the tests.
233              
234             $ minil test
235              
236             =item 7. Commit: Commit your changes.
237              
238             $ git commit -am "Add some feature"
239              
240             =item 8. Push: Push to your branch.
241            
242             $ git push origin feature-branch
243              
244             =item 9. Pull Request: Create a new Pull Request on GitHub.
245              
246             =back
247              
248             =head1 LICENSE
249              
250             Copyright (C) ytnobody.
251              
252             This library is free software; you can redistribute it and/or modify
253             it under the same terms as Perl itself.
254              
255             =head1 AUTHOR
256              
257             ytnobody E<lt>ytnobody@gmail.comE<gt>
258              
259             =cut
260