File: 98-03-document-example-nested.t

package info (click to toggle)
libvalidation-class-perl 7.900057-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 355
  • sloc: perl: 21,493; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use utf8;
use strict;
use warnings;
use Test::More;

{

    package T;

    use Validation::Class;

    field  'state'  => { state => 1 };
    field  'string' => { mixin => ':str' };

    document 'location' => {
        'id'       => 'string',
        'type'     => 'string',
        'name'     => 'string',
        'company'  => 'string',
        'address1' => 'string',
        'address2' => 'string',
        'city'     => 'string',
        'state'    => 'state',
        'zip'      => 'string'
    };

    document 'user' => {
        'id'          => 'string',
        'type'        => 'string',
        'name'        => 'string',
        'company'     => 'string',
        'login'       => 'string',
        'email'       => 'string',
        'locations.@' => 'location'
    };

    package main;

    my $class;

    eval { $class = T->new; };

    ok "T" eq ref $class, "T instantiated";

    my $documents = $class->prototype->documents;

    ok "Validation::Class::Mapping" eq ref $documents, "T documents hash registered as setting";

    ok 2 == keys %{$documents}, "T has 1 registered document";

    my $user = $documents->{user};

    ok 7 == keys %{$user}, "T user document has 3 mappings";

    can_ok $class, 'validate_document';

    my $data = {
        "id"        => 1234,
        "type"      => "Master",
        "name"      => "Root",
        "company"   => "System, LLC",
        "login"     => "root",
        "email"     => "root\@localhost",
        "locations" => [
            {
                "id"       => 9876,
                "type"     => "Node",
                "name"     => "DevBox",
                "company"  => "System, LLC",
                "address1" => "123 Street Road",
                "address2" => "Suite 2",
                "city"     => "SINCITY",
                "state"    => "NO",
                "zip"      => "00000"
            }
        ]
    };

    ok ! $class->validate_document(user => $data), "T document (user) not valid";
    ok $class->errors_to_string =~ /locations\.0\.state/, "T proper error message set";

    $class->prototype->documents->{location}->{state} = 'string';

    ok $class->validate_document(user => $data), "T document (user) validated";

}

done_testing;