Skip to content

ytnobody/p5-Poz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Actions Status

NAME

Poz - A simple, composable, and extensible data validation library for Perl.

SYNOPSIS

use Poz qw/z/;
use Data::UUID;
use Time::Piece;
my $bookSchema = z->object({
    id         => z->string->uuid->default(sub { Data::UUID->new->create_str }),   
    title      => z->string,
    author     => z->string->default("Anonymous"),
    published  => z->date,
    created_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
    updated_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
})->as("My::Book");

my $book = $bookSchema->parse({
    title     => "Spidering Hacks",
    author    => "Kevin Hemenway",
    published => "2003-10-01",
}) or die "Invalid book data";
$book->isa("My::Book"); # true

my ($otherBook, $err) = $bookSchema->safe_parse({
    title => "Eric Sink on the Business of Software",
    author => "Eric Sink",
    published => "2006-0i-01",
});
$otherBook; # undef
$err; # [{key => "", error => "Not a date"}]

my $bookOrNumberSchema = z->union($bookSchema, z->number);
my $bookOrNumber = $bookOrNumberSchema->parse(123);
$bookOrNumber = $bookOrNumberSchema->parse({
    title => "Perl Best Practices",
    date => "2005-07-01",
    author => "Damian Conway",
}); 

my $bookArraySchema = z->array($z->is("My::Book"));
my $book1 = $bookSchema->parse({title => "Perl Best Practices", author => "Damian Conway", published => "2005-07-01"});
my $book2 = $bookSchema->parse({title => "Spidering Hacks", author => "Kevin Hemenway", published => "2003-10-01"});
my $bookArray = $bookArraySchema->parse([$book1, $book2]);

# or use Poz as class builder    
{
    package My::Class;
    use Poz qw/z/;
    z->object({
        name => z->string,
        age => z->number,
    })->constructor;
}
my $instance = My::Class->new(
    name => 'Alice',
    age => 20,
); # bless({name => 'Alice', age => 20}, 'My::Class');

DESCRIPTION

Poz is a simple, composable, and extensible data validation library for Perl. It is inspired heavily from Zod https://zod.dev/ in TypeScript.

EXPORTS

z

use Poz qw/z/;
my $builder = z;

Returns a new instance of Poz::Builder.

METHODS

z->object($schema)

my $schema = z->object({
    id         => z->string->uuid->default(sub { Data::UUID->new->create_str }),   
    title      => z->string,
    author     => z->string->default("Anonymous"),
    published  => z->date,
    created_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
    updated_at => z->date->default(sub { Time::Piece::localtime()->strftime('%Y-%m-%d') }),
})->as("My::Book");

Creates a new schema object.

z->string

my $schema = z->string;

Creates a new string schema object.

z->number

my $schema = z->number;

Creates a new number schema object.

z->date

my $schema = z->date;

Creates a new date schema object.

z->object

my $schema = z->object($schema);

Creates a new object schema object.

z->object(...)->constructor

package My::Class;
use Poz qw/z/;
z->object({
    name => z->string,
    age => z->number,
})->constructor;

Creates a constructor method with Poz validation in your class.

z->array

my $schema = z->array($schema);

Creates a new array schema object.

z->enum

my $schema = z->enum(@values);

Creates a new enum schema object.

z->union

my $schema = z->union(@schemas);

Creates a new union schema object.

SEE ALSO

HOW TO CONTRIBUTE

If you want to contribute to Poz, you can follow the steps below:

    1. Prepare: Install cpanm and Minilla

      $ curl -L https://cpanmin.us | perl - --sudo App::cpanminus $ cpanm Minilla

    1. Fork: Please fork the repository on GitHub.

    The Repository on GitHub: https://github.com/ytnobody/p5-Poz

    1. Clone: Clone the repository.

      $ git clone

    1. Branch: Create a feature branch from the main branch.

      $ git checkout -b feature-branch main

    1. Code: Write your code and tests, then build.

      $ minil build

    1. Test: Run the tests.

      $ minil test

    1. Commit: Commit your changes.

      $ git commit -am "Add some feature"

    1. Push: Push to your branch.

      $ git push origin feature-branch

    1. Pull Request: Create a new Pull Request on GitHub.

LICENSE

Copyright (C) ytnobody.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

ytnobody [email protected]

About

data validator that inspired from zod

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages