Data::Sah::Type::HasElems - Specification for types that have the notion of elements
version 0.04
This is the role for types that have the notion of elements/length. It provides clauses like max_len, len, len_between, all_elems, etc. It is used by 'array', 'hash', and also 'str'.
Role consumer must provide method 'superclause_has_elems' which will receive the same %args as clause methods, but with additional key: -which (either 'max_len', 'min_len', 'len', 'len_between', 'has_any', 'has_all', 'has_none', 'has', 'hasnt').
Requires that the data have at most LEN elements.
Example:
[str, {req=>1, max_len=>10}] # define a string with at most 10 characters
Requires that the data have at least LEN elements.
[array, {min_len=>1}] # define an array with at least one element
A convenience clause that combines min_len and max_len.
Example, the two schemas below are equivalent:
[str, {len_between=>[1, 10]}] [str, {min_len=>1, max_len=>10}]
Requires that the data have exactly LEN elements.
Requires that the data contain the element.
Examples:
# requires that array has element x [array => {has => x}] # requires that array has elements x, y, and z [array => {'has&' => [x, y, z]}] # requires that array does not have element x [array => {'!has' => x}]
Requires that every element of the data validate to the specified schema.
Note: filters applied by SCHEMA to elements will be preserved.
[array => {all_elems => 'int'}]
The above specifies an array of ints.
[hash => {all_elems => [str => { match => '^[A-Za-z0-9]+$' }]}]
The above specifies hash with alphanumeric-only values.
Specify inter-element dependencies. If all elements at indexes which match REGEX1 match SCHEMA1, then all elements at indexes which match REGEX2 must match SCHEMA2.
[hash => {elem_deps => [ [ password => 'str*', password_confirmation => 'str*' ] ]}]
The above says: key 'password_confirmation' is required if 'password' is set.
[hash => {elem_deps => [ [ province => ['str*', {is => 'Outside US'}], zipcode => [str => {forbidden=>1}] ], [ province => ['str*', {not => 'Outside US'}], zipcode => [str => {required=>1}] ] ]}]
The above says: if province is set to 'Outside US', then zipcode must not be specified. Otherwise if province is set to US states, zipcode is required.
[array => {elem_deps => [ [ '^0$', ['str*' => {in => ['int', 'integer']}], '[1-9]', ['hash*' => {keys_in => [qw/is not min max/]}] ], [ '^0$', ['str*' => {in => ['str', 'string']}], '[1-9]', ['hash*' => {keys_in => [qw/is not min max min_len max_len/]}]], [ '^0$', ['str*' => {in => ['bool', 'boolean']}], '[1-9]', ['hash*' => {keys_in => [qw/is not/]}] ], ]}]
The above says: if first element of array is a text with value 'int'/'integer', then the following elements must be hash with specified keys. A similar rule is there for first element being 'str'/'string' and 'bool'/'boolean'.
Example valid array:
['str', {min_len=>0, max_len=>1}, {is=>'a', isnt=>'b'}]
Example invalid array (key 'min_len' is not allowed):
['int', {min_len=>0, max_len=>1}, {is=>'a', isnt=>'b'}]
Note: You need to be careful with undef, because it matches all schema unless req=>1 (or the shortcut 'foo*') is specified.
Steven Haryanto <stevenharyanto@gmail.com>
This software is copyright (c) 2012 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Data::Sah, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Sah
CPAN shell
perl -MCPAN -e shell install Data::Sah
For more information on module installation, please visit the detailed CPAN module installation guide.