Steven Haryanto > Data-Schema-0.136 > Data::Schema::Type::HasElement



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.136   Source  


Data::Schema::Type::HasElement - Role for types that have the notion of elements


version 0.136


    use Data::Schema;


This is the role for types that have the notion of length. It provides attributes like maxlen, length, length_between, all_elements, etc. It is used by 'str', 'array', and also 'hash'.

Role consumer must provide methods:

* '_length()' which returns the length;

* '_element(idx)' which returns the element at idx;

* '_indexes()' which returns all element indexes.


max_len => LEN

Aliases: maxlen, max_length, maxlength

Requires that the data have at most LEN elements.

min_len => LEN

Aliases: minlen, min_length, minlength

Requires that the data have at least LEN elements.

len_between => [MIN, MAX]

Aliases: length_between

A convenience attribute that combines minlen and maxlen.

len => LEN

Aliases: length

Requires that the data have exactly LEN elements.

all_elements => SCHEMA

Aliases: all_element, all_elems, all_elem

Requires that every element of the data validate to the specified schema.

Examples (in YAML):

 [array, {all_elements: int}]

The above specifies an array of ints.

 [hash, {all_elements: [str: {match: '^[A-Za-z0-9]+$'}]}]

The above specifies hash with alphanumeric-only values.

element_deps => [[REGEX1 => SCHEMA1, REGEX1 => SCHEMA2], ...]

Aliases: element_dep, elem_deps, elem_dep

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.

Examples (in YAML):

 - hash
 - elem_deps: [[ password, [str, {set: 1}], password_confirmation, [str, {set: 1}] ]]

The above says: key 'password_confirmation' is required if 'password' is set.

 - hash
 - elem_deps: [[ province, [str, {set: 1, is: 'Outside US'}],
                 zipcode,  [str, {set: 0}] ],
               [ province, [str, {set: 1, not: 'Outside US'}],
                 zipcode,  [str, {set: 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, {set: 1, one_of: [int, integer]}], 
        '[1-9]', [hash, {set: 1, allowed_keys: [is, not, min, max]}]]
     - ['^0$',   [str, {set: 1, one_of: [str, string ]}], 
        '[1-9]', [hash, {set: 1, allowed_keys: [is, not, min, max, minlen, maxlen]}]]
     - ['^0$',   [str, {set: 1, one_of: [bool        ]}], 
        '[1-9]', [hash, {set: 1, allowed_keys: [is, not]}]]

The above says: if first element of array is int/integer, then the following elements must be hash with. And so on if first element is str/string, or bool.

Note: You need to be careful with undef, because it matches all schema unless set=>1/required=>1 is specified.


  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: