Kernel::System::DataTypes

NAME

Kernel::System::DataTypes – Provides data type constraints and subroutines.

DESCRIPTION

This module provides type constraints that may be used for Moose attribute types or also directly in non-Moose code.

The type constraints are defined in Kernel::System::DataTypes::Definition but should only be used via this module.

A declared type constraint like:

    declare(
        'StrWithData',
        parent => t('Str'),
        where  => sub { length $_[0] > 0 },
    );

can either be instantiated by it's name via the t() function, after the use statement of this module:

    use Kernel::System::DataTypes;

    [...]

    # moose attributes
    has 'MyStringAttribute' => (
        is  => 'rw',
        isa => t('StrWithData'),
    );

or to validate values inside non-Moose code:

    use Kernel::System::DataTypes;

    my $Bool = is_StrWithData($MyStringValue);

All declared types as well as all builtin types are automatically exported and usable via t() or a related validation subroutines with 'is_' as a prefix. For more information about available type methods, please refer to Specio::Constraint::Simple.

For more information about builtin types and declaration of custom types, please refer to Specio.

Boolean and numeric type constraints

String type constraints

Hash / Array type constraints

PUBLIC INTERFACE

Bool

Validates data to determine booleans. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('Bool'),
    );

    my $Result = t('Bool')->value_is_valid(1);

    my $Result = is_Bool(1);

Int

Validates data to determine integers. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('Int'),
    );

    my $Result = t('Int')->value_is_valid(123);

    my $Result = is_Int(123);

PositiveInt

Validates data to determine positive integers.

Inherits from parent type: "Int".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('PositiveInt'),
    );

    my $Result = t('PositiveInt')->value_is_valid(123);

    my $Result = is_PositiveInt(123);

NegativeInt

Validates data to determine negative integers.

Inherits from parent type: "Int".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('NegativeInt'),
    );

    my $Result = t('NegativeInt')->value_is_valid(-123);

    my $Result = is_NegativeInt(-123);

Num

Validates data to determine numbers. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('Num'),
    );

    my $Result = t('Num')->value_is_valid(1.23);

    my $Result = is_Num(1.23);

PositiveNum

Validates data to determine positive numbers.

Inherits from parent type: "Num".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('PositiveNum'),
    );

    my $Result = t('PositiveNum')->value_is_valid(1.23);

    my $Result = is_PositiveNum(1.23);

NegativeNum

Validates data to determine negative numbers.

Inherits from parent type: "Num".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('NegativeNum'),
    );

    my $Result = t('NegativeNum')->value_is_valid(-1.23);

    my $Result = is_NegativeNum(-1.23);

Str

Validates data to determine strings. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('Str'),
    );

    my $Result = t('Str')->value_is_valid('My valid string');

    my $Result = is_Str(''); # even empty strings are valid!

StrWithData

Validates data to determine strings with characters.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('StrWithData'),
    );

    my $Result = t('StrWithData')->value_is_valid('test');

    my $Result = is_StrWithData('test');

PerlPackage

Validates data to determine string values with perl package paths.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('PerlPackage'),
    );

    my $Result = t('PerlPackage')->value_is_valid('MyPackage');

    my $Result = is_PerlPackage('My::Package');

MD5

Validates data to determine string values with MD5 hashes.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('MD5'),
    );

    my $Result = t('MD5')->value_is_valid('d41d8cd98f00b204e9800998ecf8427e');

    my $Result = is_MD5('d41d8cd98f00b204e9800998ecf8427e');

SHA1

Validates data to determine string values with SHA1 hashes.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('SHA1'),
    );

    my $Result = t('SHA1')->value_is_valid('da39a3ee5e6b4b0d3255bfef95601890afd80709');

    my $Result = is_SHA1('da39a3ee5e6b4b0d3255bfef95601890afd80709');

SHA256

Validates data to determine string values with SHA256 hashes.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('SHA256'),
    );

    my $Result = t('SHA256')->value_is_valid('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');

    my $Result = is_SHA256('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');

UUID

Validates data to determine UUID values.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('UUID'),
    );

    my $Result = t('UUID')->value_is_valid('c945728e-2324-11e8-9b56-9fdee36e124b');

    my $Result = is_UUID('CA761232-ED42-11CE-BACD-00AA0057B223');

IPv4

Validates data to determine IPv4 addresses.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('IPv4'),
    );

    my $Result = t('IPv4')->value_is_valid('192.168.1.1');

    my $Result = is_IPv4('127.0.0.1');

IPv6

Validates data to determine IPv6 addresses.

Inherits from parent type: "Str".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('IPv6'),
    );

    my $Result = t('IPv6')->value_is_valid('2001:db8:a0b:12f0::1');

    my $Result = is_IPv6('::1');

HashRef

Validates data to determine hash references. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('HashRef'),
    );

    my $Result = t('HashRef')->value_is_valid( { Test => 'My valid hash reference' } );

    my $Result = is_HashRef( {} ); # even empty hash references are valid!

ArrayRef

Validates data to determine array references. This type belongs to the builtin types. For more information about builtin types, please refer to Specio.

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('ArrayRef'),
    );

    my $Result = t('ArrayRef')->value_is_valid( [ 'My', 'valid', 'array', 'reference' ] );

    my $Result = is_ArrayRef( [] ); # even empty array references are valid!

HashRefWithData

Validates data to determine hash references with payload.

Inherits from parent type: "HashRef".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('HashRefWithData'),
    );

    my $Result = t('HashRefWithData')->value_is_valid( { a => 1 } );

    my $Result = is_HashRefWithData( { test => [1,2,3] } );

ArrayRefWithData

Validates data to determine array references with payload.

Inherits from parent type: "ArrayRef".

returns 1 if data matches criteria or undef otherwise

    has 'MyAttribute' => (
        is      => 'rw',
        isa     => t('ArrayRefWithData'),
    );

    my $Result = t('ArrayRefWithData')->value_is_valid( [1, 2, 3] );

    my $Result = is_ArrayRefWithData( [ { a => 1 }, { b => 2 } ] );
Scroll to Top