|
NAMEConfig::Validator - schema based configuration validationSYNOPSISuse Config::Validator; # simple usage $validator = Config::Validator->new({ type => "list(integer)" }); $validator->validate([ 1, 2 ]); # OK $validator->validate([ 1, 2.3 ]); # FAIL $validator->validate({ 1, 2 }); # FAIL # advanced usage $validator = Config::Validator->new( octet => { type => "integer", min => 0, max => 255, }, color => { type => "struct", fields => { red => { type => "valid(octet)" }, green => { type => "valid(octet)" }, blue => { type => "valid(octet)" }, }, }, ); $validator->validate( { red => 23, green => 47, blue => 6 }, "color"); # OK $validator->validate( { red => 23, green => 470, blue => 6 }, "color"); # FAIL $validator->validate( { red => 23, green => 47, lbue => 6 }, "color"); # FAIL DESCRIPTIONThis module allows to perform schema based configuration validation.The idea is to define in a schema what valid data is. This schema can be used to create a validator object that can in turn be used to make sure that some data indeed conforms to the schema. Although the primary focus is on "configuration" (for instance as provided by modules like Config::General) and, to a lesser extent, "options" (for instance as provided by modules like Getopt::Long), this module can in fact validate any data structure. METHODSThe following methods are available:
FUNCTIONSThe following convenient functions are available:
SCHEMASA schema is simply a structure (i.e. a hash reference) with the following fields (all of them being optional except the first one):
As an example, the following schema describe what a valid schema is: { type => "struct", fields => { type => { type => "list?(valid(type))" }, subtype => { type => "valid(schema)", optional => "true" }, fields => { type => "table(valid(schema))", optional => "true" }, optional => { type => "boolean", optional => "true" }, min => { type => "number", optional => "true" }, max => { type => "number", optional => "true" }, match => { type => "regexp", optional => "true" }, check => { type => "code", optional => "true" }, }, } NAMED SCHEMASFor convenience and self-reference, schemas can be named.To use named schemas, give them along with their names to the new() method: $validator = Config::Validator->new( name1 => { ... schema1 ... }, name2 => { ... schema2 ... }, ); You can then refer to them in the validate() method: $validator->validate($data, "name1"); If you don't need named schemas, you can use the simpler form: $validator = Config::Validator->new({ ... schema ... }); $validator->validate($data); TYPESHere are the different types that can be used:
EXAMPLESCONFIGURATION VALIDATIONThis module works well with Config::General. In particular, the "list?(X)" type matches the way Config::General merges blocks.For instance, one could use the following code: use Config::General qw(ParseConfig); use Config::Validator; $validator = Config::Validator->new( service => { type => "struct", fields => { port => { type => "integer", min => 0, max => 65535 }, proto => { type => "string" }, }, }, host => { type => "struct", fields => { name => { type => "string", match => qr/^\w+$/ }, service => { type => "list?(valid(service))" }, }, }, ); %cfg = ParseConfig(-ConfigFile => $path, -CComments => 0); $validator->validate($cfg{host}, "host"); This would work with: <host> name = foo <service> port = 80 proto = http </service> </host> where $cfg{host}{service} is the service hash but also with: <host> name = foo <service> port = 80 proto = http </service> <service> port = 443 proto = https </service> </host> where $cfg{host}{service} is the list of service hashes. OPTIONS VALIDATIONThis module interacts nicely with Getopt::Long: the options() method can be used to convert a schema into a list of Getopt::Long options.Here is a simple example: use Config::Validator; use Getopt::Long qw(GetOptions); use Pod::Usage qw(pod2usage); $validator = Config::Validator->new({ type => "struct", fields => { debug => { type => "boolean", optional => "true", }, proto => { type => "string", match => qr/^\w+$/, }, port => { type => "integer", min => 0, max => 65535, }, }, }); @options = $validator->options(); GetOptions(\%cfg, @options) or pod2usage(2); $validator->validate(\%cfg); ADVANCED VALIDATIONThis module can also be used to combine configuration and options validation using the same schema. The idea is to:
In some situations, it may make sense to consider the configuration data as a tree and prefer: <incoming> uri = foo://host1:1234 </incoming> <outgoing> uri = foo://host2:2345 </outgoing> to: incoming-uri = foo://host1:1234 outgoing-uri = foo://host2:2345 The options() method flatten the schema to get a list of command line options and the treeify() function transform flat options (as returned by Getopt::Long) into a deep tree so that it matches the schema. Then the treeval() function can conveniently access the value of an option. See the bundled examples for complete working programs illustrating some of the possibilities of this module. AUTHORLionel Cons <http://cern.ch/lionel.cons>Copyright (C) CERN 2012-2015
Visit the GSP FreeBSD Man Page Interface. |