Hi,

I've come up with a utility class for Params::Validate that provides
some mechanism to setup inheritable parameter validation for subroutines.

My main problem was that using Params::Validate, a lot of the code out
in CPAN uses lexicals to store the validation specs, therefore making
subclassing hard:

   package Parent;
   use Params::Validate;
   my %FooValidate = (...);
   sub foo { validate(@_, \%FooValidate) }

   package Child;
   use base qw(Parent);
   use Params::Validate;
   sub foo { # overrides Parent
      # the only way to re-use validation from parent class is to
      # call $self->SUPER::foo(...)
   }

What I wanted was some way to flexibly inherit/manage Params::Validate
specs, so I created this module, tentatively named
Params::Validate::Inheritable

  package MyClass;
  use Params::Validate::Inheritable;
  __PACKAGE__->set_pv_spec(foo => { type => Params::Validate::HASHREF() });

  sub foo
  {
      my $self = shift;

      # validate @_ according to set_pv_spec() above
      my %args = $self->validate_args([EMAIL PROTECTED]);
      ...
  }

  package MySubClass;
  use base qw(MyClass);

  __PACKAGE__->set_pv_spec(foo => \%newspec);

  sub foo
  {
      my $self = shift;
      # now validate @_ according to %newspec, not MyClass' spec for foo
      my %args = $self->validate_args([EMAIL PROTECTED]);

      # you can call the super class' method as well, and it will
      # use the correct spec
      $self->SUPER::validate_args(...);
  }


The module is tentatively named Params::Validate::Inheritable, but Dave
Rolsky and I neither like that particular idea. To quote Dave:

   I think P::V::Inheritable isn't quite right for classes that
   are intended to be generic superclasses, or to add methods to
   other classes.

   In fact, it's really a role-type class, and something like
   Class::Validating might be better.

On top of Dave's suggestion I'm thinking towards the line of
Class::ValidateArgs, Class::Validation, Class::ParamValidation, etc.

Please let me know if you have any comments.

regards,
--d

Reply via email to