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

Download:
Data-Schema-0.136.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.136   Source  

NAME ^

Data::Schema::Type::Base - Base class for Data::Schema type handler

VERSION ^

version 0.136

SYNOPSIS ^

    use Data::Schema;

DESCRIPTION ^

This is the base class for all type handlers. Normally you wouldn't use this type but one of its subclasses.

METHODS ^

handle_pre_check_attrs($data)

This method is called by handle_type() before checking type attributes. It should return true if checking passes or false if checking fails. By default it does nothing but returns true. Override this method if you want to add additional checking.

sort_attr_keys($attrhash)

Return attribute keys in execute order. Some type might need some attributes to be executed first (because they have some side-effect, etc), e.g. DST::Hash overrides this to put 'allow_extra_keys' first, because later 'keys' needs to know the state of 'allow_extra_keys'.

Otherwise, you can just return the keys in any/default order (the default implementation).

handle_type($data, $attrhash, ...)

Check data against type (and all type attributes). Returns 1 if success, 0 if fails. You normally do not need to override this method. This method is called by the validator (Data::Schema object).

Also handle the 'required' and 'forbidden' (and their alias: 'set') attributes, these are special so they're handled here. All the other attributes are handled using 'handle_attr_XXX' methods.

english($attrhash, ...)

Show an English representation of this data type.

short_english()

Show an English representation of this data type.

TYPE ATTRIBUTES ^

Attributes that are handled by the Base:

required

Aliases: set=>1

If set to 1, require that data be defined. Otherwise, allow undef (the default behaviour).

By default, undef will pass even elaborate schema:

 ds_validate(undef, "int"); # valid
 ds_validate(undef, [int => {min=>0, max=>10, divisible_by=>3}]); # valid!

However:

 ds_validate(undef, [int=>{required=>1}]); # invalid

This behaviour's rationale is much like NULLs in SQL: we *can't* validate something that is unknown/unset.

forbidden

Aliases: set=>0

This is the opposite of required, requiring that data be not defined (i.e. undef).

 ds_validate(1, [int=>{forbidden=>1}]); # invalid
 ds_validate(undef, [int=>{forbidden=>1}]); # valid

set

Alias for required or forbidden. set=>1 equals required=>1, while set=>0 equals forbidden=>1.

default

Supply a default value.

 ds_validate(undef, [int => {required=>1}]); # invalid, data undefined
 ds_validate(undef, [int => {required=>1, default=>3}]); # valid

AUTHOR ^

  Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE ^

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: