Data::Schema::Type::Array - Type handler for arrays ('array')
version 0.136
use Data::Schema;
This is the handler for arrays (or arrayrefs, to be exact).
Example schema (in YAML syntax):
[array, { minlen: 1, maxlen: 3, elem_regex: {'.*': int} }]
The above schema says that the array must have one to three elements, and all elements must be integers.
Example valid data:
[1, 2]
Example invalid data:
[] # too short [1,2,3,4] # too long ['x'] # element not integer
Arrays are Comparable and HasElement, so you can consult the docs for those roles for available attributes. In addition to these, there are other attributes for 'array':
If unique is 1, require that the array values be unique (like in a set). If unique is 0, require that there are duplicates in the array.
Note: currently the implementation uses List::MoreUtils' uniq().
Aliases: element, elems, elem
Requires that each element of the array validates to the specified schemas.
Example (in YAML):
[array, {elements: [ int, str, [int, {min: 0}] ]}]
The above example states that the array must have an int as the first element, string as the second, and positive integer as the third.
Aliases: all_elements, all_elems, all_elem
Requires that every element of the array validates to the specified schema.
Requires that some elements be of certain type. TYPE is the name of the type, MIN and MAX are numbers, -1 means unlimited.
[array, {some_of: [ [int, 1, -1], [str, 3, 3], [float, 0, 1] ]}]
The above requires that the array contains at least one integer, exactly three strings, and at most one floating number.
Aliases: element_regex, elems_regex, elem_regex
Similar to elements, but instead of specifying schema for each element, this attribute allows us to specify using regexes which elements we want to specify schema for.
- array - elements_regex: '[02468]$': [int, {minex: 0}] '[13579]$': [int, {maxex: 0}]
The above example states that the array should have as its elements positive and negative integer interspersed, e.g. [1, -2, 3, -1, ...].
Steven Haryanto <stevenharyanto@gmail.com>
This software is copyright (c) 2009 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::Schema, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Schema
CPAN shell
perl -MCPAN -e shell install Data::Schema
For more information on module installation, please visit the detailed CPAN module installation guide.