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

Name

Data::Validation::Constraints - Test data values for conformance with constraints

Synopsis

   use Data::Validation::Constraints;

   %config = ( method => $method, %{ $self->constraints->{ $id } || {} } );

   $constraint_ref = Data::Validation::Constraints->new_from_method( %config );

   $bool = $constraint_ref->validate( $value );

Description

Tests a single data value for conformance with a constraint

Configuration and Environment

Defines the following attributes:

allowed

An array reference of permitted values used by "isAllowed"

max_length

Used by "isValidLength". The length of the supplied value must be numerically less than this

max_value

Used by "isBetweenValues".

method

Name of the constraint to apply. Required

min_length

Used by "isValidLength".

min_value

Used by "isBetweenValues".

pattern

Used by "isMathchingRegex" as the pattern to match the supplied value against

required

If true then undefined values are not allowed regardless of what other validation would be done

type

If isMatchingType matches against this value

type_libraries

A list of type libraries to add to the registry. Defaults to; Unexpected::Types

type_registry

Lazily evaluated instance of Type::Registry to which the type_libraries have been added

value

Used by the "isEqualTo" method as the other value in the comparison

Subroutines/Methods

new_from_method

A class method which implements a factory pattern using the method attribute to select the subclass

validate

Called by Data::Validation::check_field this method implements tests for a null input value so that individual validation methods don't have to. It calls either a built in validation method or validate which should have been overridden in a factory subclass. An exception is thrown if the data value is not acceptable

isAllowed

Is the the value in the $self->allowed list of values

isBetweenValues

Test to see if the supplied value is numerically greater than $self->min_value and less than $self->max_value

isEqualTo

Test to see if the supplied value is equal to $self->value. Calls isValidNumber on both values to determine the type of comparison to perform

isHexadecimal

Tests to see if the value matches the regular expression for a hexadecimal number

isMandatory

Undefined and null values are not allowed

isMatchingRegex

Does the supplied value match the pattern? The pattern defaults to $self->pattern

isMatchingType

Does the supplied value pass the type constraint check? The constraint defaults to $self->type

isPrintable

Is the supplied value entirely composed of printable characters?

isSimpleText

Simple text is defined as matching the pattern '\A [a-zA-Z0-9_ \-\.]+ \z'

isValidHostname

Calls gethostbyname on the supplied value

isValidIdentifier

Identifiers must match the pattern '\A [a-zA-Z_] \w* \z'

isValidInteger

Tests to see if the supplied value is an integer

isValidLength

Tests to see if the length of the supplied value is greater than $self->min_length and less than $self->max_length

isValidNumber

Return true if the supplied value looks_like_number

isValidText

Text is defined as any string matching the pattern '\A [ !%&\(\)\*\+\,\-\./0-9:;=\?@A-Z\[\]_a-z\|\~]+ \z'

isValidTime

Matches against a the pattern '\A \d\d : \d\d (?: : \d\d )? \z'

External Constraints

Each of these constraint subclasses implements the required validate method

Date

If the str2time method in the Class::Usul::Time module can parse the supplied value then it is deemed to be a valid date

Email

If the address method in the Email::Valid module can parse the supplied value then it is deemed to be a valid email address

Password

Currently implements a minimum password length of six characters and that the password contain at least one non alphabetic character

Path

Screen out these characters: ; & * { } and space

Postcode

Tests to see if the supplied value matches one of the approved patterns for a valid postcode

URL

Call the request method in HTTP::Tiny to test if a URL is accessible

Diagnostics

None

Dependencies

charnames
Moo
Unexpected

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There is no POD coverage test because the subclasses docs are in here instead

The Data::Validation::Constraints::Date module requires the module Class::Usul::Time and this is not listed as prerequisite as it would create a circular dependency

Please report problems to the address below. Patches are welcome

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2016 Peter Flanigan. All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE