NAME
Poz::Types::object - A module for handling structured data with type validation
SYNOPSIS
use Poz qw/z/;
# Schema for a person, cast to Some::Class when valid
my $object = z->object({
name => z->string,
age => z->number,
})->as('Some::Class');
my $data = {
name => 'John Doe',
age => 30,
};
my $parsed_data = $object->parse($data); # isa Some::Class
# Schema for a person, validate that the data is an instance of Some::Class
my $another_object = z->object({
name => z->string,
age => z->number,
})->is('Another::Class');
my $other = bless {
name => 'John Doe',
age => 30,
}, 'Another::Class';
my $someone = bless {
name => 'Jane Doe',
age => 25,
}, 'Some::Class';
my $parsed_data = $another_object->parse($other); # isa Another::Class
my $someone_else = $another_object->parse($someone); # throws an exception, because not an instance of Another::Class
DESCRIPTION
Poz::Types::object is a module for handling structured data with type validation. It allows you to define a structure with specific types and validate data against this structure.
METHODS
as
$object->as($typename);
Sets the class name to bless the parsed data into. The $typename
parameter should be a string representing the class name.
is
$object->is($typename);
Validates that the parsed data is an instance of the given class. The $typename
parameter should be a string representing the class name.
parse
my $parsed_data = $object->parse($data);
Parses and validates the given data against the structure. If the data is valid, it returns the parsed data. If the data is invalid, it throws an exception with the validation errors.
safe_parse
my ($valid, $errors) = $object->safe_parse($data);
Parses and validates the given data against the structure. If the data is valid, it returns the parsed data and undef for errors. If the data is invalid, it returns undef for valid data and an array reference of errors.
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 <ytnobody@gmail.com>