From e795a0fa50205d7756615b8d536c62ee4c345eb6 Mon Sep 17 00:00:00 2001 From: Packit Date: Sep 16 2020 15:18:57 +0000 Subject: perl-Params-Check-0.38 base --- diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..e9a1cce --- /dev/null +++ b/CHANGES @@ -0,0 +1,96 @@ +Changes for 0.38 Thu Jun 20 10:43:37 2013 +============================================ +* Typo fixes by David Steinbrunner + +Changes for 0.36 Fri Apr 27 22:57:02 2012 +============================================ +* More speed enhancements by Vincent Pit + +Changes for 0.34 Wed Apr 25 13:51:31 2012 +============================================ +* check() now works fasteh thanks to + Vincent Pit + +Changes for 0.32 Mon Aug 8 10:53:36 2011 +============================================ +* Removed unnecessary use Data::Dumper + +Changes for 0.30 Mon Jul 25 14:44:40 2011 +============================================ +* Resolve [rt #69626] reported by Diab Jerius, + WARNINGS_FATAL should apply to all check() + failures now. + +Changes for 0.28 Fri Jan 7 21:52:28 2011 +============================================ +* Apply blead patch from Peter Acklam + +Changes for 0.26 Thu Mar 1 12:05:08 2007 +============================================ + +* Set install_dirs to 'perl' if perl >= 5.9.5 +* Address #23824: Bug concering the loss of the + last_error message when checking recursively. + +Changes for 0.25 Wed Jul 5 17:13:07 2006 +============================================ + +* Apply patch from #20299 that implements the + $Params::Check::CALLER_DEPTH variable. +* Add a warning if the store => variable + is not a reference. + +Changes for 0.24 Thu Mar 2 13:04:27 2006 +============================================ + +* Fix issue where allow() wouldn't shortcut + after the first successful match (#17364) + +Changes for 0.22 Thu Nov 11 11:11:33 2004 +============================================ + +* Make error reporting of invalid keys contain + the stringified version of the disallowed + value. + +Changes for 0.21 Fri Jun 18 18:38:21 2004 +============================================ + +* Make template defaults of '' (empty string) + work as they should. + +Changes for 0.20 Thu Jun 17 19:33:41 2004 +============================================ + +* ground up rewrite of both code and tests + to improve performance + +Changes for 0.07 Thu Feb 25 11:14:48 2004 +============================================ + +* quell a 'use of undefined variable' warning + when checking 'undef' values + +Changes for 0.06 Thu Feb 19 15:14:48 2004 +============================================ + +* add template option 'defined' + +Changes for 0.05 Tue Feb 10 18:18:23 2004 +============================================ + +* allow nested lists in allow() + +Changes for 0.04 Thu Sep 18 13:41:48 2003 +============================================ + +* Use carp to get a better caller perspective +* add the last_error() routine +* add the allow() routine + + +Changes for 0.03 Wed May 14 14:33:17 2003 +============================================ + +* Verbosity's default setting depends on $^W +* New global flag to not change the casing of keys diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..cc17767 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,8 @@ +CHANGES +lib/Params/Check.pm +Makefile.PL +MANIFEST This list of files +README +t/01_Params-Check.t +META.yml Module YAML meta-data (added by MakeMaker) +META.json Module JSON meta-data (added by MakeMaker) diff --git a/META.json b/META.json new file mode 100644 index 0000000..0be93aa --- /dev/null +++ b/META.json @@ -0,0 +1,47 @@ +{ + "abstract" : "Templated based param validation", + "author" : [ + "Jos Boumans " + ], + "dynamic_config" : 1, + "generated_by" : "ExtUtils::MakeMaker version 6.68, CPAN::Meta::Converter version 2.131560", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : "2" + }, + "name" : "Params-Check", + "no_index" : { + "directory" : [ + "t", + "inc" + ] + }, + "prereqs" : { + "build" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "runtime" : { + "requires" : { + "Locale::Maketext::Simple" : "0", + "Test::More" : "0" + } + } + }, + "release_status" : "stable", + "resources" : { + "repository" : { + "url" : "https://github.com/jib/params-check" + } + }, + "version" : "0.38" +} diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..359075f --- /dev/null +++ b/META.yml @@ -0,0 +1,25 @@ +--- +abstract: 'Templated based param validation' +author: + - 'Jos Boumans ' +build_requires: + ExtUtils::MakeMaker: 0 +configure_requires: + ExtUtils::MakeMaker: 0 +dynamic_config: 1 +generated_by: 'ExtUtils::MakeMaker version 6.68, CPAN::Meta::Converter version 2.131560' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: 1.4 +name: Params-Check +no_index: + directory: + - t + - inc +requires: + Locale::Maketext::Simple: 0 + Test::More: 0 +resources: + repository: https://github.com/jib/params-check +version: 0.38 diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..79cfd5e --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,44 @@ +use ExtUtils::MakeMaker; +use strict; + +WriteMakefile1( + LICENSE => 'perl', + META_MERGE => { + resources => { + repository => 'https://github.com/jib/params-check', + }, + }, + NAME => 'Params::Check', + VERSION_FROM => 'lib/Params/Check.pm', # finds $VERSION + dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz' }, + PREREQ_PM => { + 'Test::More' => 0, + 'Locale::Maketext::Simple' => 0, + }, + INSTALLDIRS => ( $] >= 5.009005 ? 'perl' : 'site' ), + AUTHOR => 'Jos Boumans ', + ABSTRACT => 'Templated based param validation' +); + +sub WriteMakefile1 { #Written by Alexandr Ciornii, version 0.21. Added by eumm-upgrade. + my %params=@_; + my $eumm_version=$ExtUtils::MakeMaker::VERSION; + $eumm_version=eval $eumm_version; + die "EXTRA_META is deprecated" if exists $params{EXTRA_META}; + die "License not specified" if not exists $params{LICENSE}; + if ($params{BUILD_REQUIRES} and $eumm_version < 6.5503) { + #EUMM 6.5502 has problems with BUILD_REQUIRES + $params{PREREQ_PM}={ %{$params{PREREQ_PM} || {}} , %{$params{BUILD_REQUIRES}} }; + delete $params{BUILD_REQUIRES}; + } + delete $params{CONFIGURE_REQUIRES} if $eumm_version < 6.52; + delete $params{MIN_PERL_VERSION} if $eumm_version < 6.48; + delete $params{META_MERGE} if $eumm_version < 6.46; + delete $params{META_ADD} if $eumm_version < 6.46; + delete $params{LICENSE} if $eumm_version < 6.31; + delete $params{AUTHOR} if $] < 5.005; + delete $params{ABSTRACT_FROM} if $] < 5.005; + delete $params{BINARY_LOCATION} if $] < 5.005; + + WriteMakefile(%params); +} diff --git a/README b/README new file mode 100644 index 0000000..59396ec --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +This is the README file for Params::Check, a perl module for powerful +template based param validation + +Please refer to 'perldoc Params::Check' after installation for details. + +##################################################################### + +* Description + +Params::Check + + Allows for generic input checking and validating using a powerfull + templating system, providing default values and so on. + +##################################################################### + +* Installation + +Params::Check follows the standard perl module install process + +perl Makefile.PL +make +make test +make install + +The module uses no C or XS parts, so no c-compiler is required. + +###################################################################### + +AUTHOR + This module by Jos Boumans . + +COPYRIGHT + This module is copyright (c) 2002 Jos Boumans . All + rights reserved. + + This library is free software; you may redistribute and/or modify it + under the same terms as Perl itself. diff --git a/lib/Params/Check.pm b/lib/Params/Check.pm new file mode 100644 index 0000000..1239442 --- /dev/null +++ b/lib/Params/Check.pm @@ -0,0 +1,695 @@ +package Params::Check; + +use strict; + +use Carp qw[carp croak]; +use Locale::Maketext::Simple Style => 'gettext'; + +BEGIN { + use Exporter (); + use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE $ALLOW_UNKNOWN + $STRICT_TYPE $STRIP_LEADING_DASHES $NO_DUPLICATES + $PRESERVE_CASE $ONLY_ALLOW_DEFINED $WARNINGS_FATAL + $SANITY_CHECK_TEMPLATE $CALLER_DEPTH $_ERROR_STRING + ]; + + @ISA = qw[ Exporter ]; + @EXPORT_OK = qw[check allow last_error]; + + $VERSION = '0.38'; + $VERBOSE = $^W ? 1 : 0; + $NO_DUPLICATES = 0; + $STRIP_LEADING_DASHES = 0; + $STRICT_TYPE = 0; + $ALLOW_UNKNOWN = 0; + $PRESERVE_CASE = 0; + $ONLY_ALLOW_DEFINED = 0; + $SANITY_CHECK_TEMPLATE = 1; + $WARNINGS_FATAL = 0; + $CALLER_DEPTH = 0; +} + +my %known_keys = map { $_ => 1 } + qw| required allow default strict_type no_override + store defined |; + +=pod + +=head1 NAME + +Params::Check - A generic input parsing/checking mechanism. + +=head1 SYNOPSIS + + use Params::Check qw[check allow last_error]; + + sub fill_personal_info { + my %hash = @_; + my $x; + + my $tmpl = { + firstname => { required => 1, defined => 1 }, + lastname => { required => 1, store => \$x }, + gender => { required => 1, + allow => [qr/M/i, qr/F/i], + }, + married => { allow => [0,1] }, + age => { default => 21, + allow => qr/^\d+$/, + }, + + phone => { allow => [ sub { return 1 if /$valid_re/ }, + '1-800-PERL' ] + }, + id_list => { default => [], + strict_type => 1 + }, + employer => { default => 'NSA', no_override => 1 }, + }; + + ### check() returns a hashref of parsed args on success ### + my $parsed_args = check( $tmpl, \%hash, $VERBOSE ) + or die qw[Could not parse arguments!]; + + ... other code here ... + } + + my $ok = allow( $colour, [qw|blue green yellow|] ); + + my $error = Params::Check::last_error(); + + +=head1 DESCRIPTION + +Params::Check is a generic input parsing/checking mechanism. + +It allows you to validate input via a template. The only requirement +is that the arguments must be named. + +Params::Check can do the following things for you: + +=over 4 + +=item * + +Convert all keys to lowercase + +=item * + +Check if all required arguments have been provided + +=item * + +Set arguments that have not been provided to the default + +=item * + +Weed out arguments that are not supported and warn about them to the +user + +=item * + +Validate the arguments given by the user based on strings, regexes, +lists or even subroutines + +=item * + +Enforce type integrity if required + +=back + +Most of Params::Check's power comes from its template, which we'll +discuss below: + +=head1 Template + +As you can see in the synopsis, based on your template, the arguments +provided will be validated. + +The template can take a different set of rules per key that is used. + +The following rules are available: + +=over 4 + +=item default + +This is the default value if none was provided by the user. +This is also the type C will look at when checking type +integrity (see below). + +=item required + +A boolean flag that indicates if this argument was a required +argument. If marked as required and not provided, check() will fail. + +=item strict_type + +This does a C check on the argument provided. The C of the +argument must be the same as the C of the default value for this +check to pass. + +This is very useful if you insist on taking an array reference as +argument for example. + +=item defined + +If this template key is true, enforces that if this key is provided by +user input, its value is C. This just means that the user is +not allowed to pass C as a value for this key and is equivalent +to: + allow => sub { defined $_[0] && OTHER TESTS } + +=item no_override + +This allows you to specify C in your template. ie, they +keys that are not allowed to be altered by the user. It pretty much +allows you to keep all your C data in one place; the +C template. + +=item store + +This allows you to pass a reference to a scalar, in which the data +will be stored: + + my $x; + my $args = check(foo => { default => 1, store => \$x }, $input); + +This is basically shorthand for saying: + + my $args = check( { foo => { default => 1 }, $input ); + my $x = $args->{foo}; + +You can alter the global variable $Params::Check::NO_DUPLICATES to +control whether the C'd key will still be present in your +result set. See the L section below. + +=item allow + +A set of criteria used to validate a particular piece of data if it +has to adhere to particular rules. + +See the C function for details. + +=back + +=head1 Functions + +=head2 check( \%tmpl, \%args, [$verbose] ); + +This function is not exported by default, so you'll have to ask for it +via: + + use Params::Check qw[check]; + +or use its fully qualified name instead. + +C takes a list of arguments, as follows: + +=over 4 + +=item Template + +This is a hash reference which contains a template as explained in the +C and C