Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Config::Validate: problem with a key type

by Hue-Bond (Priest)
on Mar 22, 2010 at 16:25 UTC ( #830111=perlquestion: print w/ replies, xml ) Need Help??
Hue-Bond has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

I'm reading a configuration file using Config::General and validating it via Config::Validate. The goal is to reject the configuration and die if there are any errors in it.

One of the requirements I want to impose is that some configuration keys shouldn't be repeated. When I repeat a key, Config::General creates an array to hold all the given values. I expect Config::Validate to reject that array (since it's not a plain string) but that's not happening. Funnily, if I tell Config::Validate that I want an integer instead of a string, it does die as desired.

Here is some code to show the problem:

#!/opt/QNDperl5.10/bin/perl use warnings; use strict; use 5.010; use Data::Dumper; use Config::General; use Config::Validate; my $schema = { section => { type => 'nested', child => { key => { ## change this to see the code die if 'integer' is use +d type => 'string', #type => 'integer', }, }, }, }; my $cfgfile = <<'EOF'; <section> key Foo key Bar </section> EOF my $config = Config::General->new (-String => $cfgfile); print Dumper { $config->getall }; ## you can see the array that Confi +g::General creates Config::Validate->new (schema => $schema)->validate (config => $config +); say "Still alive!";

How do I reject such a configuration?

Update: I just reported this bug.

--
 David Serrano
 (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Comment on Config::Validate: problem with a key type
Download Code
Re: Config::Validate: problem with a key type
by spazm (Monk) on Mar 22, 2010 at 17:02 UTC
    Quoting the perldoc:
    string

    The string type does no validation if no addition restrictions are specified. Valid options are:

    * min
    Minimum length
    * max
    Maximum length
    * regex
    String must match the regex provided.

      Adding those additional restrictions has no effect. This is my current $schema:

      my $schema = { section => { type => 'nested', child => { key => { type => 'string', regex => qr/^ ARRAY \( 0x\p{HexDigit}{1,8} \) $/x, }, }, }, };

      The code still runs to completion. If I make the regex different (e.g. by changing "ARRAY" into "ARAY"), the code complains about the data not matching the schema. Looks like a bug to me...

      Thank you for giving me the clue!

      --
       David Serrano
       (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Re: Config::Validate: problem with a key type
by zwon (Monsignor) on Mar 22, 2010 at 18:44 UTC

    You can specify callback for the parameter and check it there:

    my $schema = { section => { type => 'nested', child => { key => { type => 'string', callback => sub { die 'Should be a scalar' if ref $_[1 +] eq 'ARRAY' }, }, }, }, };
    Also you can add your own type if you want to check multiple parameters.

    Update: and here's the example:

    use warnings; use strict; use 5.010; use Data::Dumper; use Config::General; use Config::Validate qw(mkpath); my $schema = { section => { type => 'nested', child => { key => { type => 'MyString', }, }, }, }; my $cfgfile = <<'EOF'; <section> key Foo key Bar </section> EOF my $config = Config::General->new( -String => $cfgfile ); print Dumper { $config->getall }; ## you can see the array that Con +fig::General creates my $validate = Config::Validate->new( schema => $schema ); $validate->add_type( name => 'MyString', validate => \&is_string ); $validate->validate( config => $config ); say "Still alive!"; sub is_string { return unless ref $_[1]; die "Parameter " . mkpath( $_[3] ) . " should be a scalar!"; }

      I have several keys, each of them with its own callback. That's what I'll do. Thank you!

      --
       David Serrano
       (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://830111]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-26 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls