Skip Menu |

This queue is for tickets about the Validate-Simple CPAN distribution.

Report information
The Basics
Id: 133483
Status: open
Priority: 0/
Queue: Validate-Simple

People
Owner: Nobody in particular
Requestors: cousinmarc [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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
Download signature.asc
application/pgp-signature 488b

Message body not shown because it is not plain text.

On Wed Oct 07 10:25:46 2020, cousinmarc@gmail.com wrote: Show quoted text
> 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
Hi, Yes, I have plans to implement two more features: 1. A type "spec": { foo => { type => 'spec', of => $inner_spec, } } or { foo => { type => 'spec', of => Validate::Simple->new( $inner_spec ), } } 2. Two more options for 'hash' type: * allowed_keys * required_keys I feel like internally both features can transformed into another V::S object. At the moment I do not have a use case for these features in my project, so these features are still in TODO-list. Regards, Andrei.
Subject: Re: [rt.cpan.org #133483] Make it possible to add restrictions to hash keys
Date: Thu, 8 Oct 2020 09:54:00 +0200
To: bug-Validate-Simple [...] rt.cpan.org
From: Marc Cousin <cousinmarc [...] gmail.com>
Ok, I'll manage like I did for now, thanks for the great tool anyway.
Download signature.asc
application/pgp-signature 488b

Message body not shown because it is not plain text.