NAME
Type::Simple - simple type validation system for Perl
VERSION
Version 0.02
SYNOPSIS
use Type::Simple qw(:all);
# simple values
validate( Int(), 123 ); # -> true
validate( Str(), 'xyz' ); # -> false
# array and hash references
validate( ArrayRef(Int()), [ 1, 2, 3 ] ); # -> true
validate( HashRef(Bool()), { foo => 1, bar => 0 } ); # -> true
# hash references with specific keys and value types
validate(
HashRefWith( foo => Int(), bar => Int() ),
{ foo => 1 },
); # -> false, because you didn't provide key "bar"
validate(
HashRefWith( foo => Int(), bar => Maybe(Int()) ),
{ foo => 1 },
); # -> true, because "bar" is Maybe()
Check the test suite for many more examples!
You can pass your own validation functions as code references:
my $greater_than_one = sub { $_[0] > 1 };
my $less_than_ten = sub { $_[0] < 10 };
validate( $greater_than_one, 50 ); # -> true
validate( $less_than_ten, 50 ); # -> false
It's possible to combine and modify tests using the boolean functions Type::Simple::AND()
, Type::Simple::OR()
and Type::Simple::NOT()
:
validate(
Type::Simple::OR( CodeRef(), RegexpRef() ),
$code_or_regexp,
);
validate(
Type::Simple::AND(
Num(),
$greater_than_one,
$less_than_ten
),
$number
);
validate(
Type::Simple::AND(
Num(),
Type::Simple::NOT(Int()),
),
$non_integer_number
);
DESCRIPTION
Any
Bool
Maybe(`a)
Undef
Defined
Value
Str
Alpha
Alnum
Ascii
Num
Int
Print
Punct
Space
Word
Ref
ScalarRef
ArrayRef(`a)
HashRef(`a)
HashRefWith( k1 => `a, k2 => `b, ... )
CodeRef
RegexpRef
Object
EXPORT
None by default.
All the subroutines below can be exported:
SUBROUTINES
validate( type, value)
Try to validate a value using a type. Example:
validate( Num(), 123 ); # -> true
validate( Num(), 'x' ); # -> false
The validation functions are the following:
Any()
Anything.
Bool()
Perl boolean values: 1
, 0
, ''
and undef
.
Maybe(`a)
Type `a or undef
.
Undef()
undef
.
Defined()
Defined value. (Not undef
)
Value()
Number or string. (Not references)
Str()
String.
Since numbers can be stringified, it will also accept numbers. If you want a non-numeric string, you can use:
Type::Simple::AND(
Str(),
Type::Simple::NOT(Num()),
);
Alpha()
A string of alphabetical characters ([A-Za-z]
).
Alnum()
A string of alphanumeric characters ([A-Za-z0-9]
)
Ascii()
A string of characters in the ASCII character set.
Num()
Looks like a number.
Int()
An integer.
Print()
A string of printable characters, including spaces.
Punct()
A string of non-alphanumeric, non-space characters (<[-!"#$%&'()*+,./:;<=
?@[\\\]^_`{|}~]>>).
Space()
A string of whitespace characters (equivalent to \s
).
Word()
A string of word characters ([A-Za-z0-9_]
, equivalent to \w
).
Ref()
A reference.
ScalarRef()
A scalar reference.
ArrayRef(`a)
An array reference.
If you specify `a, the array elements should be of type `a.
HashRef(`a)
A hash reference.
If you specify `a, all values should be of type `a.
HashRefWith( k1 => `a, k2 => `b, ... )
A hash reference with a given set of keys and value types.
Attention: keys MUST be strings!
HashRefWith( foo => Int() ); # good
HashRefWith( Str() => Int() ); # bad! Key should be a string, not a CODE reference
CodeRef()
A code reference.
RegexpRef()
A regexp reference.
Object()
A blessed object.
AUTHOR
Nelson Ferraz, <nferraz at gmail.com>
BUGS
Please report any bugs or feature requests to https://github.com/nferraz/type-simple/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Type::Simple
You can also look for information at:
GitHub
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2017 Nelson Ferraz.
This program is distributed under the MIT (X11) License: http://www.opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.