Steven Haryanto > Data-Schema > Data::Schema::Type::Array



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.136   Source  


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':

unique => 0 or 1

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.

of => SCHEMA

Aliases: all_elements, all_elems, all_elem

Requires that every element of the array validates to the specified schema.

some_of => [[TYPE, MIN, MAX], [TYPE, MIN, MAX], ...]

Requires that some elements be of certain type. TYPE is the name of the type, MIN and MAX are numbers, -1 means unlimited.

Example (in YAML):

 [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.

elements_regex => {REGEX=>SCHEMA, REGEX2=>SCHEMA2, ...]

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.

Example (in YAML):

 - 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 <>


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.

syntax highlighting: