.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Struct 3pm" .TH Struct 3pm "2021-01-07" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Data::Validate::Struct \- Validate recursive Hash Structures .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 8 \& use Data::Validate::Struct; \& my $validator = new Data::Validate::Struct($reference); \& if ( $validator\->validate($config_hash_reference) ) { \& print "valid\en"; \& } \& else { \& print "invalid " . $validator\->errstr() . "\en"; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module validates a config hash reference against a given hash structure in contrast to Data::Validate in which you have to check each value separately using certain methods. .PP This hash could be the result of a config parser or just any hash structure. Eg. the hash returned by XML::Simple could be validated using this module. You may also use it to validate \&\s-1CGI\s0 input, just fetch the input data from \s-1CGI,\s0 map it to a hash and validate it. .PP Data::Validate::Struct uses some of the methods exported by Data::Validate, so you need to install it too. .SH "PREDEFINED BUILTIN DATA TYPES" .IX Header "PREDEFINED BUILTIN DATA TYPES" .IP "\fBint\fR" 4 .IX Item "int" Match a simple integer number. .IP "\fBrange(a\-b)\fR" 4 .IX Item "range(a-b)" Match a simple integer number in a range between a and b. Eg: .Sp .Vb 1 \& { loginport => \*(Aqrange(22\-23)\*(Aq } .Ve .IP "\fBhex\fR" 4 .IX Item "hex" Match a hex value. .IP "\fBoct\fR" 4 .IX Item "oct" Match an octagonal value. .IP "\fBnumber\fR" 4 .IX Item "number" Match a decimal number, it may contain , or . and may be signed. .IP "\fBword\fR" 4 .IX Item "word" Match a single word, _ and \- are tolerated. .IP "\fBline\fR" 4 .IX Item "line" Match a line of text \- no newlines are allowed. .IP "\fBtext\fR" 4 .IX Item "text" Match a whole text(blob) including newlines. This expression is very loosy, consider it as an alias to \fBany\fR. .IP "\fBregex\fR" 4 .IX Item "regex" Match a perl regex using the operator \fBqr()\fR. Valid examples include: .Sp .Vb 3 \& qr/[0\-9]+/ \& qr([^%]*) \& qr{\ew+(\ed+?)} .Ve .Sp Please note, that this doesn't mean you can provide here a regex against config options must match. .Sp Instead this means that the config options contains a regex. .Sp eg: .Sp .Vb 3 \& $cfg = { \& grp = qr/root|wheel/ \& }; .Ve .Sp \&\fBregex\fR would match the content of the variable 'grp' in this example. .Sp To add your own rules for validation, use the \fB\fBtype()\fB\fR method, see below. .IP "\fBuri\fR" 4 .IX Item "uri" Match an internet \s-1URI.\s0 .IP "\fBipv4\fR" 4 .IX Item "ipv4" Match an IPv4 address. .IP "\fBcidrv4\fR" 4 .IX Item "cidrv4" The same as above including cidr netmask (/24), IPv4 only, eg: .Sp .Vb 1 \& 10.2.123.0/23 .Ve .Sp Note: shortcuts are not supported for the moment, eg: .Sp .Vb 1 \& 10.10/16 .Ve .Sp will fail while it is still a valid IPv4 cidr notation for a network address (short for 10.10.0.0/16). Must be fixed in Regex::Common. .IP "\fBipv6\fR" 4 .IX Item "ipv6" Match an IPv6 address. Some examples: .Sp .Vb 5 \& 3ffe:1900:4545:3:200:f8ff:fe21:67cf \& fe80:0:0:0:200:f8ff:fe21:67cf \& fe80::200:f8ff:fe21:67cf \& ff02:0:0:0:0:0:0:1 \& ff02::1 .Ve .IP "\fBcidrv6\fR" 4 .IX Item "cidrv6" The same as above including cidr netmask (/64), IPv6 only, eg: .Sp .Vb 2 \& 2001:db8:dead:beef::1/64 \& 2001:db8::/32 .Ve .IP "\fBquoted\fR" 4 .IX Item "quoted" Match a text quoted with single quotes, eg: .Sp .Vb 1 \& \*(Aqbarbara is sexy\*(Aq .Ve .IP "\fBhostname\fR" 4 .IX Item "hostname" Match a valid hostname, it must qualify to the definitions in \s-1RFC 2396.\s0 .IP "\fBresolvablehost\fR" 4 .IX Item "resolvablehost" Match a hostname resolvable via dns lookup. Will fail if no dns is available at runtime. .IP "\fBpath\fR" 4 .IX Item "path" Match a valid absolute path, it won't do a \fBstat()\fR system call. This will work on any operating system at runtime. So this one: .Sp .Vb 1 \& C:\eTemp .Ve .Sp will return \s-1TRUE\s0 if running on \s-1WIN32,\s0 but \s-1FALSE\s0 on FreeBSD! .IP "\fBfileexists\fR" 4 .IX Item "fileexists" Look if value is a file which exists. Does a \fBstat()\fR system call. .IP "\fBuser\fR" 4 .IX Item "user" Looks if the given value is an existent user. Does a \fBgetpwnam()\fR system call. .IP "\fBgroup\fR" 4 .IX Item "group" Looks if the given value is an existent group. Does a \fBgetgrnam()\fR system call. .IP "\fBport\fR" 4 .IX Item "port" Match a valid tcp/udp port. Must be a digit between 0 and 65535. .IP "\fBvars\fR" 4 .IX Item "vars" Matches a string of text containing variables (perl style variables though) eg: .Sp .Vb 3 \& $user is $attribute \& I am $(years) old \& Missing ${points} points to succeed .Ve .SH "MIXED TYPES" .IX Header "MIXED TYPES" If there is an element which could match more than one type, this can be matched by using the pipe sign \f(CW\*(C`|\*(C'\fR to separate the types. .PP .Vb 1 \& { name => \*(Aqint | number\*(Aq } .Ve .PP There is no limit on the number of types that can be checked for, and the check is done in the sequence written (first the type 'int', and then \&'number' in the example above). .SH "OPTIONAL ITEMS" .IX Header "OPTIONAL ITEMS" If there is an element which is optional in the hash, you can use the type 'optional' in the type. The 'optional' type can also be mixed with ordinary types, like: .PP .Vb 1 \& { name => \*(Aqtext | optional\*(Aq } .Ve .PP The type 'optional' can be placed anywhere in the type string. .SH "NEGATIVE MATCHING" .IX Header "NEGATIVE MATCHING" In some rare situations you might require a negative match. So a test shall return \s-1TRUE\s0 if a particular value does \s-1NOT\s0 match the given type. This might be useful to prevent certain things. .PP To achieve this, you just have to prepend one of the below mentioned types with the keyword \fBno\fR. .PP Example: .PP .Vb 1 \& $ref = { path => \*(Aqnovars\*(Aq } .Ve .PP This returns \s-1TRUE\s0 if the value of the given config hash does \s-1NOT\s0 contain \s-1ANY\s0 variables. .SH "VALIDATOR STRUCTURE" .IX Header "VALIDATOR STRUCTURE" The expected structure must be a standard perl hash reference. This hash may look like the config you are validating but instead of real-live values it contains \fBtypes\fR that define of what type a given value has to be. .PP In addition the hash may be deeply nested. In this case the validated config must be nested the same way as the reference hash. .PP Example: .PP .Vb 1 \& $reference = { user => \*(Aqword\*(Aq, uid => \*(Aqint\*(Aq }; .Ve .PP The following config would be validated successful: .PP .Vb 1 \& $config = { user => \*(AqHansDampf\*(Aq, uid => 92 }; .Ve .PP this one not: .PP .Vb 5 \& $config = { user => \*(AqHans Dampf\*(Aq, uid => \*(Aqnine\*(Aq }; \& ^ ^^^^ \& | | \& | +\-\-\-\-\- is not a number \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- space not allowed .Ve .PP For easier writing of references you yould use a configuration file parser like Config::General or Config::Any, just write the definition using the syntax of such a module, get the hash of it and use this hash as validation reference. .SH "NESTED HASH STRUCTURES" .IX Header "NESTED HASH STRUCTURES" You can also match against nested structures. \fBData::Validate::Struct\fR iterates into the given config hash the same way as the reference hash looks like. .PP If the config hash doesn't match the reference structure, perl will throw an error, which \fBData::Validate::Struct\fR catches and returns \s-1FALSE.\s0 .PP Given the following reference hash: .PP .Vb 9 \& $ref = { \& \*(Aqb1\*(Aq => { \& \*(Aqb2\*(Aq => { \& \*(Aqb3\*(Aq => { \& \*(Aqitem\*(Aq => \*(Aqint\*(Aq \& } \& } \& } \& } .Ve .PP Now if you validate it against the following config hash it will return \s-1TRUE:\s0 .PP .Vb 9 \& $cfg = { \& \*(Aqb1\*(Aq => { \& \*(Aqb2\*(Aq => { \& \*(Aqb3\*(Aq => { \& \*(Aqitem\*(Aq => \*(Aq100\*(Aq \& } \& } \& } \& } .Ve .PP If you validate it for example against this hash, it will return \s-1FALSE:\s0 .PP .Vb 7 \& $cfg = { \& \*(Aqb1\*(Aq => { \& \*(Aqb2\*(Aq => { \& \*(Aqitem\*(Aq => \*(Aq100\*(Aq \& } \& } \& } .Ve .SH "SUBROUTINES/METHODS" .IX Header "SUBROUTINES/METHODS" .IP "\fBvalidate($config)\fR" 4 .IX Item "validate($config)" \&\f(CW$config\fR must be a hash reference you'd like to validate. .Sp It returns a true value if the given structure looks valid. .Sp If the return value is false (0), then the error message will be written to the variable $!. .IP "\fBtype(%types)\fR" 4 .IX Item "type(%types)" You can enhance the validator by adding your own rules. Just add one or more new types using a simple hash using the \fB\fBtype()\fB\fR method. Values in this hash can be regexes or anonymous subs. .Sp \&\f(CW\*(C`type\*(C'\fR does accept either a hash (\f(CW%hash\fR), a hash ref (\f(CW%$hash\fR) or a list of key/values (\f(CW\*(C`key => value\*(C'\fR) as input. .Sp For details see \*(L"\s-1CUSTOM VALIDATORS\*(R"\s0. .IP "\fB\fBdebug()\fB\fR" 4 .IX Item "debug()" Enables debug output which gets printed to \s-1STDERR.\s0 .IP "\fBerrors\fR" 4 .IX Item "errors" Returns an array ref with the errors found when validating the hash. Each error is on the format ' doesn't match at ', where is a comma separated tree view depicting where in the the error occurred. .IP "\fB\fBerrstr()\fB\fR" 4 .IX Item "errstr()" Returns the last error, which is useful to notify the user about what happened. The format is like in \*(L"errors\*(R". .SH "EXPORTED FUNCTIONS" .IX Header "EXPORTED FUNCTIONS" .SS "add_validators" .IX Subsection "add_validators" This is a class function which adds types not per object but globally for each instance of Data::Validate::Struct. .PP .Vb 3 \& use Data::Validate::Struct qw(add_validators); \& add_validators( name => .. ); \& my $v = Data::Validate::Struct\->new(..); .Ve .PP Parameters to \fBadd_validators\fR are the same as of the \&\fBtype\fR method. .PP For details see \*(L"\s-1CUSTOM VALIDATORS\*(R"\s0. .SH "CUSTOM VALIDATORS" .IX Header "CUSTOM VALIDATORS" You can add your own validators, which maybe regular expressions or anonymous subs. Validators can be added using the \fB\fBtype()\fB\fR method or globally using the \fB\fBadd_validators()\fB\fR function. .SS "\s-1CUSTOM REGEX VALIDATORS\s0" .IX Subsection "CUSTOM REGEX VALIDATORS" If you add a validator which is just a regular expressions, it will evaluated as is. This is the most simplest way to customize validation. .PP Sample: .PP .Vb 4 \& use Data::Validate::Struct qw(add_validators); \& add_validators(address => qr(^\ew+\es\es*\ed+$)); \& my $v = Data::Validate::Struct\->new({place => \*(Aqaddress\*(Aq}); \& $v\->validate({place => \*(AqLivermore 19\*(Aq}); .Ve .PP Regexes will be executed exactly as given. No flags or ^ or $ will be used by the module. Eg. if you want to match the whole value from beginning to the end, add ^ and $, like you can see in our 'address' example above. .SS "\s-1CUSTOM VALIDATOR FUNCTIONS\s0" .IX Subsection "CUSTOM VALIDATOR FUNCTIONS" If the validator is a coderef, it will be executed as a sub. .PP Example: .PP .Vb 8 \& use Data::Validate::Struct qw(add_validators); \& add_validators( \& list => sub { \& my $list = shift; \& my @list = split /\es*,\es*/, $list; \& return scalar @list > 1; \& }, \& ); .Ve .PP In this example we add a new type 'list', which is really simple. 'list' is a subroutine which gets called during evaluation for each option which you define as type 'list'. .PP Such a subroutine must return a true value in order to produce a match. It receives the following arguments: .IP "\(bu" 4 value to be evaluated .IP "\(bu" 4 unparsed arguments, if defined in the reference .IP "\(bu" 4 array of parsed arguments, tokenized by , and \- .PP That way you may define a type which accepts an arbitrary number of arguments, which makes the type customizable. Sample: .PP .Vb 2 \& # new validator \& $v4 = Data::Validate::Struct\->new({ list => nwords(4) }); \& \& # define type \*(Aqnwords\*(Aq with support for 1 argument \& $v4\->type( \& nwords => sub { \& my($val, $ignore, $count) = @_; \& return (scalar(split /\es+/, $val) == $count) ? 1 : 0; \& }, \& ); \& \& # validate \& $v4\->validate({ list => \*(Aqthese are four words\*(Aq }); .Ve .SS "\s-1CUSTOM VALIDATORS USING A GRAMMAR\s0" .IX Subsection "CUSTOM VALIDATORS USING A GRAMMAR" Sometimes you want to be more flexible, in such cases you may use a parser generator to validate input. This is no feature of Data::Validate::Struct, you will just write a custom code ref validator, which then uses the grammar. .PP Here's a complete example using Parse::RecDescent: .PP .Vb 2 \& use Parse::RecDescent; \& use Data::Validate::Struct qw(add_validators); \& \& my $grammar = q{ \& line: expr(s) \& expr: number operator number \& number: int | float \& int: /\ed+/ \& float: /\ed*\e\e.\ed+/ \& operator: \*(Aq+\*(Aq | \*(Aq\-\*(Aq | \*(Aq*\*(Aq | \*(Aq/\*(Aq \& }; \& \& my $parse = Parse::RecDescent\->new($grammar); \& \& add_validators(calc => sub { defined $parse\->line($_[0]) ? 1 : 0; }); \& \& my $val = Data::Validate::Struct\->new({line => \*(Aqcalc\*(Aq}); \& \& if ($val\->validate({line => "@ARGV"})) { \& my $r; \& eval "\e$r = @ARGV"; \& print "$r\en"; \& } \& else { \& print "syntax error\en"; \& } .Ve .PP Now you can use it as follows: .PP .Vb 2 \& ./mycalc 54 + 100 \- .1 \& 153.9 \& \& ./mycalc 8^2 \& syntax error .Ve .SS "\s-1NEGATED VALIDATOR\s0" .IX Subsection "NEGATED VALIDATOR" A negative/reverse match is automatically added as well, see \&\*(L"\s-1NEGATIVE MATCHING\*(R"\s0. .SH "EXAMPLES" .IX Header "EXAMPLES" Take a look to \fIt/run.t\fR for lots of examples. .SH "CONFIGURATION AND ENVIRONMENT" .IX Header "CONFIGURATION AND ENVIRONMENT" No environment variables will be used. .SH "SEE ALSO" .IX Header "SEE ALSO" I recommend you to read the following documentations, which are supplied with perl: .PP perlreftut Perl references short introduction. .PP perlref Perl references, the rest of the story. .PP perldsc Perl data structures intro. .PP perllol Perl data structures: arrays of arrays. .PP Data::Validate common data validation methods. .PP Data::Validate::IP common data validation methods for IP-addresses. .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright (c) 2007\-2015 T. v.Dein .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" Some implementation details as well as the \s-1API\s0 may change in the future. This will no more happen if entering a stable release (starting with 1.00). .PP To submit use . .SH "INCOMPATIBILITIES" .IX Header "INCOMPATIBILITIES" None known. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" To debug Data::Validate::Struct use \fB\fBdebug()\fB\fR or the perl debugger, see perldebug. .PP For example to debug the regex matching during processing try this: .PP .Vb 1 \& perl \-Mre=debug yourscript.pl .Ve .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" Data::Validate::Struct depends on the module Data::Validate, Data::Validate:IP, Regexp::Common, File::Spec and File::stat. .SH "AUTHORS" .IX Header "AUTHORS" T. v.Dein .PP Per Carlson .PP Thanks to David Cantrell for his helpful hints. .SH "VERSION" .IX Header "VERSION" 0.10