Venus::Assert - Assert Class
Assert Class for Perl 5
package main; use Venus::Assert; my $assert = Venus::Assert->new('Float'); # $assert->accept('float'); # $assert->format(sub{sprintf('%.2f', $_)}); # $assert->result(123.456); # 123.46
This package provides a mechanism for asserting type constraints and coercions on data. Type constraints are handled via Venus::Constraint, and coercions are handled via Venus::Coercion, using Venus::Check to perform data type validations.
This package has the following attributes:
name(string $data) (string)
The name attribute is read-write, accepts (string) values, and is optional.
(string)
Since 1.40
1.40
# given: synopsis package main; my $set_name = $assert->name("Example"); # "Example"
# given: synopsis # given: example-1 name package main; my $get_name = $assert->name; # "Example"
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Buildable
This package provides the following methods:
accept(string $name, any @args) (Venus::Assert)
The accept method registers a condition via "check" based on the arguments provided. The built-in types are defined as methods in Venus::Check.
# given: synopsis package main; $assert = $assert->accept('float'); # bless(..., "Venus::Assert") # $assert->valid; # false # $assert->valid(1.01); # true
# given: synopsis package main; $assert = $assert->accept('number'); # bless(..., "Venus::Assert") # $assert->valid(1.01); # false # $assert->valid(1_01); # true
# given: synopsis package Example1; sub new { bless {}; } package Example2; sub new { bless {}; } package main; $assert = $assert->accept('object'); # bless(..., "Venus::Assert") # $assert->valid; # false # $assert->valid(qr//); # false # $assert->valid(Example1->new); # true # $assert->valid(Example2->new); # true
# given: synopsis package Example1; sub new { bless {}; } package Example2; sub new { bless {}; } package main; $assert = $assert->accept('Example1'); # bless(..., "Venus::Assert") # $assert->valid; # false # $assert->valid(qr//); # false # $assert->valid(Example1->new); # true # $assert->valid(Example2->new); # false
check(Venus::Check $data) (Venus::Check)
The check method gets or sets the Venus::Check object used for performing runtime data type validation.
Since 3.55
3.55
# given: synopsis package main; my $check = $assert->check(Venus::Check->new); # bless(..., 'Venus::Check')
# given: synopsis package main; $assert->check(Venus::Check->new); my $check = $assert->check; # bless(..., 'Venus::Check')
clear() (Venus::Assert)
The clear method resets the "check", "constraint", and "coercion" attributes and returns the invocant.
# given: synopsis package main; $assert->accept('string'); $assert = $assert->clear; # bless(..., "Venus::Assert")
coerce(any $data) (any)
The coerce method dispatches to the "coercion" object and returns the result of the "result" in Venus::Coercion operation.
# given: synopsis package main; $assert->accept('float'); $assert->format(sub{sprintf('%.2f', $_)}); my $coerce = $assert->coerce(123.456); # 123.46
# given: synopsis package main; $assert->accept('string'); $assert->format(sub{ucfirst lc $_}); my $coerce = $assert->coerce('heLLo'); # "Hello"
coercion(Venus::Coercion $data) (Venus::Coercion)
The coercion method gets or sets the Venus::Coercion object used for performing runtime data type coercions.
# given: synopsis package main; my $coercion = $assert->coercion(Venus::Coercion->new); # bless(..., 'Venus::Coercion')
# given: synopsis package main; $assert->coercion(Venus::Coercion->new); my $coercion = $assert->coercion; # bless(..., 'Venus::Coercion')
conditions() (Venus::Assert)
The conditions method is an object construction hook that allows subclasses to configure the object on construction setting up constraints and coercions and returning the invocant.
# given: synopsis package main; $assert = $assert->conditions; # bless(..., 'Venus::Assert')
package Example::Type::PositveNumber; use base 'Venus::Assert'; sub conditions { my ($self) = @_; $self->accept('number', sub { $_ >= 0 }); return $self; } package main; my $assert = Example::Type::PositveNumber->new; # $assert->valid(0); # true # $assert->valid(1); # true # $assert->valid(-1); # false
constraint(Venus::Constraint $data) (Venus::Constraint)
The constraint method gets or sets the Venus::Constraint object used for performing runtime data type constraints.
# given: synopsis package main; my $constraint = $assert->constraint(Venus::Constraint->new); # bless(..., 'Venus::Constraint')
# given: synopsis package main; $assert->constraint(Venus::Constraint->new); my $constraint = $assert->constraint; # bless(..., 'Venus::Constraint')
ensure(coderef $code) (Venus::Assert)
The ensure method registers a custom (not built-in) constraint condition and returns the invocant.
# given: synopsis package main; $assert->accept('number'); my $ensure = $assert->ensure(sub { $_ >= 0 }); # bless(.., "Venus::Assert")
expression(string $expr) (Venus::Assert)
The expression method parses a string representation of an type assertion, registers the subexpressions using the "accept" method, and returns the invocant.
Since 1.71
1.71
# given: synopsis package main; $assert = $assert->expression('string'); # bless(..., 'Venus::Assert') # $assert->valid('hello'); # true # $assert->valid(['goodbye']); # false
# given: synopsis package main; $assert = $assert->expression('string | coderef'); # bless(..., 'Venus::Assert') # $assert->valid('hello'); # true # $assert->valid(sub{'hello'}); # true # $assert->valid(['goodbye']); # false
# given: synopsis package main; $assert = $assert->expression('string | coderef | Venus::Assert'); # bless(..., 'Venus::Assert') # $assert->valid('hello'); # true # $assert->valid(sub{'hello'}); # true # $assert->valid($assert); # true # $assert->valid(['goodbye']); # false
# given: synopsis package main; $assert = $assert->expression('Venus::Assert | within[arrayref, Venus::Assert]'); # bless(..., 'Venus::Assert') # $assert->valid('hello'); # false # $assert->valid(sub{'hello'}); # false # $assert->valid($assert); # true # $assert->valid(['goodbye']); # false # $assert->valid([$assert]); # true
# given: synopsis package main; $assert = $assert->expression(' string | within[ arrayref, within[ hashref, string ] ] '); # bless(..., 'Venus::Assert') # $assert->valid('hello'); # true # $assert->valid(sub{'hello'}); # false # $assert->valid($assert); # false # $assert->valid([]); # false # $assert->valid([{'test' => ['okay']}]); # false # $assert->valid([{'test' => 'okay'}]); # true
format(coderef $code) (Venus::Assert)
The format method registers a custom (not built-in) coercion condition and returns the invocant.
# given: synopsis package main; $assert->accept('number'); my $format = $assert->format(sub { sprintf '%.2f', $_ }); # bless(.., "Venus::Assert")
parse(string $expr) (any)
The parse method accepts a string representation of a type assertion and returns a data structure representing one or more method calls to be used for validating the assertion signature.
Since 2.01
2.01
# given: synopsis package main; my $parsed = $assert->parse(''); # ['']
# given: synopsis package main; my $parsed = $assert->parse('any'); # ['any']
# given: synopsis package main; my $parsed = $assert->parse('string | number'); # ['either', 'string', 'number']
# given: synopsis package main; my $parsed = $assert->parse('enum[up,down,left,right]'); # [['enum', 'up', 'down', 'left', 'right']]
# given: synopsis package main; my $parsed = $assert->parse('number | float | boolean'); # ['either', 'number', 'float', 'boolean']
# given: synopsis package main; my $parsed = $assert->parse('Example'); # ['Example']
# given: synopsis package main; my $parsed = $assert->parse('coderef | Venus::Code'); # ['either', 'coderef', 'Venus::Code']
# given: synopsis package main; my $parsed = $assert->parse('tuple[number, arrayref, coderef]'); # [['tuple', 'number', 'arrayref', 'coderef']]
# given: synopsis package main; my $parsed = $assert->parse('tuple[number, within[arrayref, hashref], coderef]'); # [['tuple', 'number', ['within', 'arrayref', 'hashref'], 'coderef']]
# given: synopsis package main; my $parsed = $assert->parse( 'tuple[number, within[arrayref, hashref] | arrayref, coderef]' ); # [ # ['tuple', 'number', # ['either', ['within', 'arrayref', 'hashref'], 'arrayref'], 'coderef'] # ]
# given: synopsis package main; my $parsed = $assert->parse( 'hashkeys["id", number | float, "upvotes", within[arrayref, boolean]]' ); # [[ # 'hashkeys', # 'id', # ['either', 'number', 'float'], # 'upvotes', # ['within', 'arrayref', 'boolean'] # ]]
render(string $into, string $expression) (string)
The render method builds and returns a type expressions suitable for providing to "expression" based on the data provided.
Since 2.55
2.55
# given: synopsis package main; $assert = $assert->render; # undef
# given: synopsis package main; $assert = $assert->render(undef, 'string'); # "string"
# given: synopsis package main; $assert = $assert->render('routines', ['say', 'say_pretty']); # 'routines["say", "say_pretty"]'
# given: synopsis package main; $assert = $assert->render('hashkeys', {id => 'number', name => 'string'}); # 'hashkeys["id", number, "name", string]'
# given: synopsis package main; $assert = $assert->render('hashkeys', { id => 'number', profile => { level => 'string', }, }); # 'hashkeys["id", number, "profile", hashkeys["level", string]]'
result(any $data) (any)
The result method validates the value provided against the registered constraints and if valid returns the result of the value having any registered coercions applied. If the value is invalid an exception from Venus::Check will be thrown.
# given: synopsis package main; $assert->accept('number')->format(sub{sprintf '%.2f', $_}); my $result = $assert->result(1); # "1.00"
# given: synopsis package main; $assert->accept('number')->format(sub{sprintf '%.2f', $_}); my $result = $assert->result('hello'); # Exception! (isa Venus::Check::Error) (see error_on_coded)
valid(any $data) (any)
The valid method dispatches to the "constraint" object and returns the result of the "result" in Venus::Constraint operation.
# given: synopsis package main; $assert->accept('float'); $assert->ensure(sub{$_ >= 1}); my $valid = $assert->valid('1.00'); # true
# given: synopsis package main; $assert->accept('float'); $assert->ensure(sub{$_ >= 1}); my $valid = $assert->valid('0.99'); # false
validate(any $data) (any)
The validate method validates the value provided against the registered constraints and if valid returns the value. If the value is invalid an exception from Venus::Check will be thrown.
# given: synopsis package main; $assert->accept('number'); my $validate = $assert->validate(1); # 1
# given: synopsis package main; $assert->accept('number'); my $validate = $assert->validate('hello'); # Exception! (isa Venus::Check::Error) (see error_on_coded)
validator(any @args) (coderef)
The validator method returns a coderef which calls the "validate" method with the invocant when called.
# given: synopsis package main; $assert->accept('string'); my $validator = $assert->validator; # sub{...} # my $result = $validator->(); # Exception! (isa Venus::Check::Error) (see error_on_coded)
# given: synopsis package main; $assert->accept('string'); my $validator = $assert->validator; # sub{...} # my $result = $validator->('hello'); # "hello"
Awncorp, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2000, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.