* Created Validate.pm and created Validate->is_uuid for validating UUID strings.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent 0a712f4a9c
commit b571fa043e
  1. 15
      AN/Tools.pm
  2. 2
      AN/Tools/Get.pm
  3. 106
      AN/Tools/Validate.pm

@ -36,6 +36,7 @@ use AN::Tools::Get;
use AN::Tools::Log; use AN::Tools::Log;
use AN::Tools::Storage; use AN::Tools::Storage;
use AN::Tools::Words; use AN::Tools::Words;
use AN::Tools::Validate;
=pod =pod
@ -105,6 +106,7 @@ sub new
LOG => AN::Tools::Log->new(), LOG => AN::Tools::Log->new(),
STORAGE => AN::Tools::Storage->new(), STORAGE => AN::Tools::Storage->new(),
WORDS => AN::Tools::Words->new(), WORDS => AN::Tools::Words->new(),
VALIDATE => AN::Tools::Validate->new(),
}, },
DATA => {}, DATA => {},
ENV_VALUES => { ENV_VALUES => {
@ -128,6 +130,7 @@ sub new
$an->Log->parent($an); $an->Log->parent($an);
$an->Storage->parent($an); $an->Storage->parent($an);
$an->Words->parent($an); $an->Words->parent($an);
$an->Validate->parent($an);
# Set some system paths and system default variables # Set some system paths and system default variables
$an->_set_paths; $an->_set_paths;
@ -323,6 +326,18 @@ sub Words
return ($self->{HANDLE}{WORDS}); return ($self->{HANDLE}{WORDS});
} }
=head2 Validate
Access the C<Validate.pm> methods via 'C<< $an->Validate->method >>'.
=cut
sub Validate
{
my $self = shift;
return ($self->{HANDLE}{VALIDATE});
}
=head1 Private Functions; =head1 Private Functions;

@ -23,7 +23,7 @@ my $THIS_FILE = "Get.pm";
AN::Tools::Get AN::Tools::Get
Provides all methods related to logging. Provides all methods related to getting access to frequently used data.
=head1 SYNOPSIS =head1 SYNOPSIS

@ -0,0 +1,106 @@
package AN::Tools::Validate;
#
# This module contains methods used to validate types of data.
#
use strict;
use warnings;
use Data::Dumper;
our $VERSION = "3.0.0";
my $THIS_FILE = "Validate.pm";
### Methods;
# is_uuid
=pod
=encoding utf8
=head1 NAME
AN::Tools::Validate
Provides all methods related to data validation.
=head1 SYNOPSIS
use AN::Tools;
# Validate a common object handle on all AN::Tools modules.
my $an = AN::Tools->new();
# Access to methods using '$an->Validate->X'.
#
# Example using 'is_uuid()';
if ($an->Validate->is_uuid({uuid => $string}))
{
print "The UUID: [$string] is valid!\n";
}
=head1 METHODS
Methods in this module;
=cut
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
return ($self);
}
# Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more
# sense in this case to think of it as a parent.
sub parent
{
my $self = shift;
my $parent = shift;
$self->{HANDLE}{TOOLS} = $parent if $parent;
return ($self->{HANDLE}{TOOLS});
}
#############################################################################################################
# Public methods #
#############################################################################################################
=head2 is_uuid
This method takes a UUID string and returns 'C<< 1 >>' if it is a valid UUID string. Otherwise it returns 'C<< 0 >>'.
NOTE: This method is strict and will only validate UUIDs that are lower case!
if ($an->Validate->is_uuid({uuid => $string}))
{
print "The UUID: [$string] is valid!\n";
}
=head2 Parameters;
=head3 uuid (required)
This is the UUID to verify.
=cut
sub is_uuid
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : 0;
my $valid = 0;
if (($uuid) && ($uuid =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/))
{
$valid = 1;
}
return($valid);
}
Loading…
Cancel
Save