Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Config::Validate: problem with a key type

by zwon (Abbot)
on Mar 22, 2010 at 18:44 UTC ( [id://830142]=note: print w/replies, xml ) Need Help??


in reply to Config::Validate: problem with a key type

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!"; }

Replies are listed 'Best First'.
Re^2: Config::Validate: problem with a key type
by Hue-Bond (Priest) on Mar 22, 2010 at 19:27 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://830142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found