Subject: | Make it possible to add restrictions to hash keys |
Date: | Wed, 7 Oct 2020 16:25:28 +0200 |
To: | bug-Validate-Simple [...] rt.cpan.org |
From: | Marc Cousin <cousinmarc [...] gmail.com> |
Hi, I'm trying to validate a configuration like this
{
connection_string => 'dbname=xxxx port=5433',
db_options => {
peopleask => {
source_slotname => 'db1'
},
rh2 => {
source_slotname => 'db2'
},
sae => {
source_slotname => 'db3'
}
}, debug => 0, # Default
}
The thing I'm having trouble is that I want to force the "source_slotname" key to exist in this hash.
I didn't find another way to do this but:
my $db_options_specs =
{
source_slotname =>
{
type => 'string',
required => 0,
}
};
my $db_options_validator = Validate::Simple->new($db_options_specs);
# Do the configuration validation
my $conf_specs =
{
connection_string => {
type => 'string',
required => 1,
},
db_options => {
type => 'hash',
required => 0,
of =>
{ # List of dbs with the option
type => 'hash',
of =>
{ # Options per db
type => 'string',
required => 1
},
callback => sub { my $val = $db_options_validator->validate($_[0]);
if ($val) {return 1}
else {die join "\n", $db_options_validator->errors()}
},
}
}
};
my $validator = Validate::Simple->new($conf_specs);
unless ($validator->validate($config))
{
print join "\n", $validator->errors();
die "Validation of configuration failed";
}
What I would have liked would be to write something like
# Do the configuration validation
my $conf_specs =
{
connection_string => {
type => 'string',
required => 1,
},
db_options => {
type => 'hash',
required => 0,
of =>
{ # List of dbs with the option
type => 'hash',
of =>
{
source_slotname =>
{
type => 'string',
required => 0,
}
}
}
};
Or something like that. I don't think it's possible right now, but it would be quite neat. Or is it possible and I missed something ?
Regards
Message body not shown because it is not plain text.