Venus::Data
Data Class
Data Class for Perl 5
method: error method: errors method: new method: renew method: shorthand method: valid method: validate
package main;
use Venus::Data;
my $data = Venus::Data->new;
# bless({}, 'Venus::Data')
This package provides a value object for encapsulating data validation. It represents a single immutable validation attempt, ensuring unvalidated data cannot be observed. Validation runs at most once per instance, with all observable outcomes flowing from that validation. The big idea is that the schema (or ruleset) is a contract, and if the validate was success you can be certain that the data (or value) is valid and conforms with the schema.
Venus::Kind::Utility
The error method returns the first validation error as an arrayref in the format [path, [error_type, args]], or undef if no errors exist. This is a convenience method for accessing the first error when you don't need the complete error list. Call /validate or /valid first to ensure validation has run.
error() (arrayref)
{ since => '4.15', }
=example-1 error
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
name => undef,
},
ruleset => [
{
selector => 'name',
presence => 'required',
executes => ['string']
},
],
);
$data->validate;
my $error = $data->error;
# ['name', ['required', []]]
The errors method returns an arrayref of all validation errors. Each error is an arrayref with the format [path, [error_type, args]] where path indicates which field failed and error_type describes the failure (e.g., 'required', 'type', etc). Returns an empty arrayref if validation succeeded or hasn't run yet.
errors() (within[arrayref, arrayref])
{ since => '4.15', }
=example-1 errors
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
name => undef,
age => 'invalid'
},
ruleset => [
{
selector => 'name',
presence => 'required',
executes => ['string']
},
{
selector => 'age',
presence => 'required',
executes => ['number']
},
],
);
$data->validate;
my $errors = $data->errors;
# [
# ['name', ['required', []]],
# ['age', ['number', []]],
# ]
The new method constructs an instance of the package.
new(any @args) (Venus::Data)
{ since => '4.15', }
The renew method creates a new instance with updated arguments while preserving the ruleset from the current instance. This is the best way to "update" the value while maintaining the ruleset. The new instance will have its validation state reset and will need to be validated again.
renew(any @args) (object)
{ since => '4.15', }
=example-1 renew
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
name => 'Example',
},
ruleset => [
{
selector => 'name',
presence => 'required',
executes => ['string']
},
],
);
my $renewed = $data->renew(value => {name => 'Updated'});
# bless({...}, 'Venus::Data')
The shorthand method accepts an arrayref or hashref of shorthand notation and sets the ruleset on the instance using "shorthand" in Venus::Schema. This provides a concise way to define validation rules. Keys can have suffixes to indicate presence: ! for (explicit) required, ? (explicit) for optional, * for (explicit) present (i.e., must exist but can be null), and no suffix means (implicit) required. Keys using dot notation (e.g., website.url) result in arrayref selectors for nested path validation. Returns the invocant for method chaining.
shorthand(arrayref | hashref $data) (Venus::Data)
{ since => '4.15', }
=example-1 shorthand
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
fname => 'Elliot',
lname => 'Alderson',
},
);
$data->shorthand([
'fname!' => 'string',
'lname!' => 'string',
]);
my $valid = $data->valid;
# 1
The valid method returns a boolean indicating whether the data is valid. Triggers validation on first call if not already validated. Subsequent calls return the cached validation state without re-validating. This is the primary way to check if data passed validation.
valid() (boolean)
{ since => '4.15', }
=example-1 valid
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
name => 'Example',
},
ruleset => [
{
selector => 'name',
presence => 'required',
executes => ['string']
},
],
);
my $valid = $data->valid;
# 1
The validate method performs validation of the value against the ruleset and returns the validated (and potentially modified) value on success, or undef on failure. Validation runs at most once per instance, and subsequent calls return cached results. The returned value may differ from the original due to transformations applied during validation (e.g., "trim", "strip", "lowercase", etc). After validation, check /valid to determine success/failure and /errors to get validation errors.
validate() (any)
{ since => '4.15', }
=example-1 validate
package main;
use Venus::Data;
my $data = Venus::Data->new(
value => {
name => ' Example ',
},
ruleset => [
{
selector => 'name',
presence => 'required',
executes => ['string', 'trim']
},
],
);
my $validated = $data->validate;
# {name => 'Example'}
t/Venus.t: present: authors t/Venus.t: present: license
43 POD Errors
The following errors were encountered while parsing the POD:
- Around line 14:
Unknown directive: =name
- Around line 22:
Unknown directive: =tagline
- Around line 30:
Unknown directive: =abstract
- Around line 38:
Unknown directive: =includes
- Around line 52:
Unknown directive: =synopsis
- Around line 72:
Unknown directive: =description
- Around line 85:
Unknown directive: =inherits
- Around line 93:
Unknown directive: =method
- Around line 101:
Unknown directive: =signature
- Around line 105:
Unknown directive: =metadata
- Around line 173:
=cut found outside a pod block. Skipping to next block.
- Around line 208:
=cut found outside a pod block. Skipping to next block.
- Around line 221:
Unknown directive: =method
- Around line 229:
Unknown directive: =signature
- Around line 233:
Unknown directive: =metadata
- Around line 317:
=cut found outside a pod block. Skipping to next block.
- Around line 362:
=cut found outside a pod block. Skipping to next block.
- Around line 375:
Unknown directive: =method
- Around line 379:
Unknown directive: =signature
- Around line 383:
Unknown directive: =metadata
- Around line 401:
=cut found outside a pod block. Skipping to next block.
- Around line 411:
Unknown directive: =method
- Around line 418:
Unknown directive: =signature
- Around line 422:
Unknown directive: =metadata
- Around line 494:
=cut found outside a pod block. Skipping to next block.
- Around line 506:
Unknown directive: =method
- Around line 517:
Unknown directive: =signature
- Around line 521:
Unknown directive: =metadata
- Around line 580:
=cut found outside a pod block. Skipping to next block.
- Around line 616:
=cut found outside a pod block. Skipping to next block.
- Around line 651:
=cut found outside a pod block. Skipping to next block.
- Around line 662:
Unknown directive: =method
- Around line 669:
Unknown directive: =signature
- Around line 673:
Unknown directive: =metadata
- Around line 735:
=cut found outside a pod block. Skipping to next block.
- Around line 773:
=cut found outside a pod block. Skipping to next block.
- Around line 783:
Unknown directive: =method
- Around line 793:
Unknown directive: =signature
- Around line 797:
Unknown directive: =metadata
- Around line 860:
=cut found outside a pod block. Skipping to next block.
- Around line 897:
=cut found outside a pod block. Skipping to next block.
- Around line 965:
=cut found outside a pod block. Skipping to next block.
- Around line 981:
Unknown directive: =partials