summaryrefslogtreecommitdiffstats
path: root/t/more-basic.t
blob: ceb904a070281dda68eac6e23e852968846c9de1 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

use Moose::Util::TypeConstraints 'find_type_constraint';

use MooseX::Validation::Doctypes;

doctype 'Location' => {
    id      => 'Str',
    city    => 'Str',
    state   => 'Str',
    country => 'Str',
    zipcode => 'Int',
};

doctype 'Person' => {
    id    => 'Str',
    name  => {
        first_name => 'Str',
        last_name  => 'Str',
    },
    title   => 'Str',
    friends => 'ArrayRef[Person]',
    address => 'Maybe[Location]'
};


{
    my $person = find_type_constraint('Person');
    isa_ok($person, 'Moose::Meta::TypeConstraint');
    isa_ok($person, 'MooseX::Meta::TypeConstraint::Doctype');

    is_deeply(
        $person->doctype,
        {
            id    => 'Str',
            name  => {
                first_name => 'Str',
                last_name  => 'Str',
            },
            title   => 'Str',
            friends => 'ArrayRef[Person]',
            address => 'Maybe[Location]'
        },
        "got the right doctype"
    );

    {
        my $errors = $person->validate({
            id    => '17382-QA',
            name  => {
                first_name => 'Bob',
                last_name  => 'Smith',
            },
            title => 'CIO',
            friends => [],
            address => {
                id      => 'My House',
                city    => 'Anytown',
                state   => 'IL',
                country => 'USA',
                zipcode => '12345'
            }
        });

        is($errors, undef, "no errors");
    }

    {
        my $errors = $person->validate({
            id    => '17382-QA',
            name  => {
                first_name => 'Bob',
                last_name  => 'Smith',
            },
            title => 'CIO',
            friends => [],
            favorite_food => 'ice cream',
        });
        isa_ok($errors, 'MooseX::Validation::Doctypes::Errors');
        is_deeply(
            $errors->extra_data,
            { favorite_food => 'ice cream' },
            "got the right extra data"
        );
        is($errors->errors, undef, "no errors");
    }

    {
        my $errors = $person->validate({
            id    => '17382-QA',
            name  => {
                first_name => 'Bob',
                last_name  => 'Smith',
            },
            friends => [],
            address => {
                id      => 'My House',
                city    => 'Anytown',
                state   => 'IL',
                country => 'USA',
                zipcode => '12345'
            }
        });
        isa_ok($errors, 'MooseX::Validation::Doctypes::Errors');
        is($errors->extra_data, undef, "no extra data");
        is_deeply(
            $errors->errors,
            { title => "invalid value undef for 'title'" },
            "got the right errors"
        );
    }
}

done_testing;