NAME
Venus::Assert - Assert Class
ABSTRACT
Assert Class for Perl 5
SYNOPSIS
package main;
use Venus::Assert;
my $assert = Venus::Assert->new('Example');
# $assert->coercion(float => sub { sprintf('%.2f', $_->value) });
# $assert->constraint(float => sub { $_->value > 1 });
# $assert->check;
DESCRIPTION
This package provides a mechanism for asserting type constraints and coercions on data.
ATTRIBUTES
This package has the following attributes:
message
message(Str)
This attribute is read-write, accepts (Str)
values, and is optional.
name
name(Str)
This attribute is read-write, accepts (Str)
values, and is optional.
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
check
check(Any $data) (Bool)
The check method returns true or false if the data provided passes the registered constraints.
Since 1.23
- check example 1
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $check = $assert->check; # 0
- check example 2
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $check = $assert->check('0.01'); # 0
- check example 3
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $check = $assert->check('1.01'); # 1
- check example 4
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $check = $assert->check(time); # 0
coerce
coerce(Any $data) (Any)
The coerce method returns the coerced data if the data provided matches any of the registered coercions.
Since 1.23
- coerce example 1
-
# given: synopsis package main; $assert->coercion(float => sub { sprintf('%.2f', $_->value) }); my $coerce = $assert->coerce; # undef
- coerce example 2
-
# given: synopsis package main; $assert->coercion(float => sub { sprintf('%.2f', $_->value) }); my $coerce = $assert->coerce('1.01'); # "1.01"
- coerce example 3
-
# given: synopsis package main; $assert->coercion(float => sub { sprintf('%.2f', $_->value) }); my $coerce = $assert->coerce('1.00001'); # "1.00"
- coerce example 4
-
# given: synopsis package main; $assert->coercion(float => sub { sprintf('%.2f', $_->value) }); my $coerce = $assert->coerce('hello world'); # "hello world"
coercion
coercion(Str $type, CodeRef $code) (Object)
The coercion method returns registers a coercion based on the type provided.
Since 1.23
- coercion example 1
-
# given: synopsis package main; my $coercion = $assert->coercion(float => sub { sprintf('%.2f', $_->value) }); # bless(..., "Venus::Assert")
coercions
coercions() (Match)
The coercions method returns the registered coercions as a Venus::Match object.
Since 1.23
- coercions example 1
-
# given: synopsis package main; my $coercions = $assert->coercions; # bless(..., "Venus::Match")
constraint
constraint(Str $type, CodeRef $code) (Object)
The constraint method returns registers a constraint based on the type provided.
Since 1.23
- constraint example 1
-
# given: synopsis package main; my $constraint = $assert->constraint(float => sub { $_->value > 1 }); # bless(..., "Venus::Assert")
constraints
constraints() (Match)
The constraints method returns the registered constraints as a Venus::Match object.
Since 1.23
- constraints example 1
-
# given: synopsis package main; my $constraints = $assert->constraints; # bless(..., "Venus::Match")
validate
validate(Any $data) (Any)
The validate method returns the data provided if the data provided passes the registered constraints, or throws an exception.
Since 1.23
- validate example 1
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $result = $assert->validate; # Exception! (isa Venus::Assert::Error)
- validate example 2
-
# given: synopsis package main; $assert->constraint(float => sub { $_->value > 1 }); my $result = $assert->validate('0.01'); # Exception! (isa Venus::Assert::Error)