The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Type::Simple - simple type validation system for Perl

VERSION

Version 0.01

SYNOPSIS

    use Type::Simple qw(:all);

    # simple values
    validate( Int(), 123 );     # -> true
    validate( Str(), 'xyz' );   # -> false

    # compound values
    validate( ArrayRef(Int()), [ 1, 2, 3 ] );            # -> true
    validate( HashRef(Bool()), { foo => 1, bar => 0 } ); # -> true

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
        Undef
        Defined
            Value
                Str
                    Alpha
                    Alnum
                    Ascii
                    Num
                        Int
                    Print
                    Punct
                    Space
                    Word
            Ref
                ScalarRef
                ArrayRef
                HashRef
                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, the hash values should be of type `a.

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:

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.